Concepts / Floating-Point Precision and Rounding

Floating-Point Precision and Rounding

Because it is a floating-point approximation, it represents π for calculations without being the exact mathematical value with unlimited decimal places.

  • Programming
Interactive lab

Try it: The math Module: Logs, Roots and Angles

What math.log(x), math.log10(x), math.log(x, base), math.sqrt(x) and math.sin / cos / tan really compute: which logarithm base each call uses, why 0 and negative inputs must be checked before the call (ValueError: math domain error), why trigonometric functions need radians, and that math.pi and every result are binary64 approximations (math.log(1000, 10) prints 2.9999999999999996).

How it works

  1. math.log(x) is the natural logarithm (base e); math.log10(x) is base 10; math.log(x, base) computes log(x) / log(base) with two rounded natural logs.
  2. Logarithms need x > 0 and math.sqrt needs x >= 0; otherwise the call raises ValueError: math domain error, so a guard checks the input first.
  3. math.sin, math.cos and math.tan read their argument as radians: convert degrees with degrees / 360.0 * 2 * math.pi.
  4. math.pi is a float value (no parentheses) that approximates pi to about 16 digits, so sin(180 degrees) is 1.2246467991473532e-16, not 0.
  5. Every result is the nearest binary64 value; print shows the shortest repr that reads back as the same float, while the stored value has more digits.

Default run (6 steps): Ready: the program calls math.log10(x). … print(result) shows 3.0.

Simplified: One-call programs over int / float literals you type (|x| up to 1e15). The results come from the browser's IEEE-754 math library: math.sqrt and the radians arithmetic are identical to CPython; log / sin / cos / tan match CPython 3.12 (Windows) exactly for about 91% of the tested inputs and are within 2 units in the last place otherwise, because C math libraries differ in the last bit. Error messages are CPython 3.12's (3.14 says 'expected a positive input').

Educational simulation

Loading the simulation…

Why Precision Matters

Geometry, physics, and engineering calculations often require mathematical values and operations that are more precise than hand-written approximations. Python's math module provides tools for this purpose, including math.pi for the mathematical constant pi and math.sqrt() for computing square roots.

Key point

The important distinction is that math.pi is accessed as a value, while math.sqrt() is called as a function.

The Approximation in math.pi

math.pi is a variable from Python's math module that provides an approximation of the mathematical constant π accurate to about 15 digits. Because it is a floating-point approximation, it represents π for calculations without being the exact mathematical value with unlimited decimal places.

python
πexact mathematical constantmath.piapproximation to about 15digitsapproximated as
How does the floating-point value supplied by math.pi relate to the exact mathematical constant π?

Tracing a Square Root

What do you think happens?

What happens when Python evaluates math.sqrt(2) / 2.0?

  • The value 2 is passed to math.sqrt(), and the returned square-root result is divided by 2.0.

  • The number 2.0 is divided first, and math.sqrt() is applied afterward.

  • math.sqrt() is treated like the value math.pi and is not called.

Reveal answer
Answer:

The value 2 is passed to math.sqrt(), and the returned square-root result is divided by 2.0.

Python calls math.sqrt() with 2 as its input. The function computes the square root internally and returns that result. The returned result then becomes the left side of the division by 2.0.

Code walkthrough
import math

result = math.sqrt(2) / 2.0
print(result)
  1. 1
    Python evaluates math.sqrt(2) by passing the value 2 into the square-root function.
  2. 2
    math.sqrt() computes the square root of the non-negative input and returns the result.
  3. 3
    The returned result is divided by 2.0.
  4. 4
    The final value is stored in result and then passed to print().
2function argumentmath.sqrt()computes a square rootsquare-root resultreturned valuedivide by 2.0later arithmeticresultvalue used by the programpasses intoreturnsbecomes input toproduces
How does an input value move through math.sqrt() and then combine with another arithmetic operation?

Combining Math Tools

A function result can participate in further arithmetic, just like a value stored in a variable. This lets you combine math.pi and math.sqrt() in one calculation. For example, a geometry calculation can use math.pi for a circle-related value and math.sqrt() for a radius or another measurement that involves a square root.

Worked example

Tracing a combined calculation

Use math.pi and math.sqrt() in a calculation where the square-root result is multiplied by a value involving pi.

  1. 1
    Access the constant

    Read math.pi as the approximation of π supplied by the math module. Do not add parentheses because it is accessed as a value.

  2. 2
    Call the function

    Pass a non-negative number to math.sqrt() using parentheses. The function computes and returns its square root.

  3. 3
    Combine the results

    Use the returned square-root value and math.pi in the surrounding arithmetic expression.

  4. 4
    Verify the relationship

    Compare or inspect the calculated result to check whether it behaves as expected for the mathematical relationship being modeled.

Result

The calculation uses a precise math-module approximation for pi and a computed square root rather than manually typed approximations or a user-written square-root algorithm.

python
Key point

When checking a relationship, separate the stages mentally: identify the constant or input, evaluate the function call, and then follow the arithmetic applied to the returned value.

Mistakes with Constants and Functions

  • Common mistake

    Writing math.pi() as though pi were a function.

    What it looks like
    value = math.pi()
    Why it fails

    math.pi is a variable that provides a value, so it is accessed without parentheses.

    Do this instead
    Write value = math.pi.
  • Common mistake

    Using math.sqrt without passing an input.

    What it looks like
    value = math.sqrt
    Why it fails

    math.sqrt is the function itself. To compute a square root, the function must be called with parentheses and a number as its argument.

    Do this instead
    Write value = math.sqrt(number).
  • Common mistake

    Assuming the function call is the final stage of a larger expression.

    What it looks like
    result = math.sqrt(2) / 2.0
    Why it fails

    The value returned by math.sqrt(2) still flows into the division by 2.0.

    Do this instead

    Trace the expression from the function input to the returned value and then through the remaining arithmetic.

  • Common mistake

    Treating math.pi as an unlimited exact representation of π.

    What it looks like

    Using math.pi as though it contained every decimal digit of the mathematical constant.

    Why it fails

    math.pi provides a floating-point approximation accurate to about 15 digits.

    Do this instead

    Use math.pi as the math module's precise approximation for calculations and remember its stated precision.

Practice the Value Flow

MEDIUM

Write a short Python expression that imports math, computes the square root of a non-negative number, combines that result with math.pi using arithmetic, and stores the final value in a variable. Then describe the order in which the input, function result, and arithmetic result are produced.

Hints
  • Use import math before referring to math.pi or math.sqrt().
  • Use parentheses when calling math.sqrt().
  • Use math.pi without parentheses.
  • Trace the expression in stages instead of treating it as one indivisible operation.

Key Takeaways

  1. 1
    math.pi is a math-module variable containing an approximation of π accurate to about 15 digits.
  2. 2
    Access math.pi without parentheses.
  3. 3
    Call math.sqrt() with parentheses and a non-negative number as its argument.
  4. 4
    The input flows into math.sqrt(), the function returns a square-root value, and later arithmetic can use that returned value.
  5. 5
    Combining math.pi and math.sqrt() supports geometry, physics, and engineering calculations.

At a glance

  • math.pi provides a floating-point approximation of π accurate to about 15 digits.
  • A constant such as math.pi is accessed, while a function such as math.sqrt() is called.
  • math.sqrt() receives a non-negative input, computes its square root, and returns the result.
  • Returned function values can flow into additional arithmetic expressions.
  • Using math.pi and math.sqrt() together helps verify and perform mathematical calculations.

Related concepts

More concepts starting with F