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.
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
- 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.
- 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.
- math.sin, math.cos and math.tan read their argument as radians: convert degrees with degrees / 360.0 * 2 * math.pi.
- 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.
- 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').
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.
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.
Tracing a Square Root
What do you think happens?
What happens when Python evaluates math.sqrt(2) / 2.0?
Reveal 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.
import math
result = math.sqrt(2) / 2.0
print(result)- 1Python evaluates
math.sqrt(2)by passing the value 2 into the square-root function. - 2
math.sqrt()computes the square root of the non-negative input and returns the result. - 3The returned result is divided by 2.0.
- 4The final value is stored in result and then passed to
print().
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.
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.
- 1Access the constant
Read
math.pias the approximation of π supplied by the math module. Do not add parentheses because it is accessed as a value. - 2Call the function
Pass a non-negative number to
math.sqrt()using parentheses. The function computes and returns its square root. - 3Combine the results
Use the returned square-root value and
math.piin the surrounding arithmetic expression. - 4Verify the relationship
Compare or inspect the calculated result to check whether it behaves as expected for the mathematical relationship being modeled.
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.
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 likevalue = math.pi()Why it failsmath.piis a variable that provides a value, so it is accessed without parentheses.Do this insteadWrite value = math.pi.Common mistake Using math.sqrt without passing an input.
What it looks likevalue = math.sqrtWhy it failsmath.sqrtis the function itself. To compute a square root, the function must be called with parentheses and a number as its argument.Do this insteadWrite value = math.sqrt(number).Common mistake Assuming the function call is the final stage of a larger expression.
What it looks likeresult = math.sqrt(2) / 2.0Why it failsThe value returned by
math.sqrt(2)still flows into the division by 2.0.Do this insteadTrace 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 likeUsing math.pi as though it contained every decimal digit of the mathematical constant.
Why it failsmath.piprovides a floating-point approximation accurate to about 15 digits.Do this insteadUse math.pi as the math module's precise approximation for calculations and remember its stated precision.
Practice the Value Flow
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.piormath.sqrt(). - Use parentheses when calling
math.sqrt(). - Use
math.piwithout parentheses. - Trace the expression in stages instead of treating it as one indivisible operation.
Key Takeaways
- 1
math.piis a math-module variable containing an approximation of π accurate to about 15 digits. - 2Access
math.piwithout parentheses. - 3Call
math.sqrt()with parentheses and a non-negative number as its argument. - 4The input flows into
math.sqrt(), the function returns a square-root value, and later arithmetic can use that returned value. - 5Combining
math.piandmath.sqrt()supports geometry, physics, and engineering calculations.
At a glance
math.piprovides a floating-point approximation of π accurate to about 15 digits.- A constant such as
math.piis accessed, while a function such asmath.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.piandmath.sqrt()together helps verify and perform mathematical calculations.