Concepts / Other Math Module Functions: Trigonometry and Logarithms

Other Math Module Functions: Trigonometry and Logarithms

The division does not happen inside math.sqrt(). The function returns a square-root result first; the separate arithmetic operation then uses that result.

  • 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…

From Mathematical Idea to Python Value

Geometry, physics, and engineering calculations often need both precise mathematical values and reliable mathematical operations. Python's math module provides math.pi for an approximation of π and math.sqrt() for computing square roots. These tools let you use established implementations instead of entering your own approximation or writing your own square-root algorithm.

Definition

math.pi is a variable from the math module containing an approximation of π accurate to about 15 digits. Because it is a variable rather than a function call, you access it without parentheses.

Definition

math.sqrt() is a function that computes the square root of a non-negative number. You call it with parentheses and pass the number as its argument.

Tracing a Square-Root Calculation

What do you think happens?

What should the final value be when Python computes the square root of 2 and then divides that result by 2.0?

  • The original value 2

  • The square root of 2 divided by 2.0

  • The square root of 4

  • The value of pi

Reveal answer
Answer:

The square root of 2 divided by 2.0, approximately 0.7071

Python first sends 2 into math.sqrt(), receives the square root of 2, and then divides that returned value by 2.0.

2input valuemath.sqrt()square-root calculationsquare root of 2returned resultdivide by 2.0arithmetic operationapproximately 0.7071final valuepasses 2returnsused byproduces
How does an input value move through math.sqrt() and then through an arithmetic operation to produce the final result?

What Python Does at Each Step

When Python evaluates math.sqrt(2), the value 2 flows into the function. The function performs the square-root calculation internally and returns the result. Python can then use that returned result in another operation, such as division, or store it in a variable or print it.

Code walkthrough
import math

root = math.sqrt(2)
result = root / 2.0
print(result)
  1. 1
    The import makes the math module available under the name math.
  2. 2
    The call math.sqrt(2) sends 2 into the square-root function.
  3. 3
    The function returns the square root of 2, which is assigned to root.
  4. 4
    The expression root / 2.0 uses that returned value in arithmetic.
  5. 5
    The print statement displays the final result.
  • Imports the math module so its mathematical tools can be accessed.
  • Calls math.sqrt() with 2 and stores the returned square root.
  • Divides the returned square root by 2.0 and stores the new result.
  • Prints the final value.
Output
approximately 0.7071067811865476
Key point

The division does not happen inside math.sqrt(). The function returns a square-root result first; the separate arithmetic operation then uses that result.

Using Pi with a Square Root

You can combine math.pi and math.sqrt() in one calculation when a geometry, physics, or engineering problem requires both. The important part is to track each value: math.pi supplies the approximation of π, math.sqrt() transforms its input into a square root, and arithmetic operations combine the resulting values.

Worked example

Combining the Two Math Tools

Evaluate an expression that multiplies math.pi by the square root of 9.

  1. 1
    Access pi

    math.pi contributes the math module's approximation of π. It is accessed directly, without parentheses.

  2. 2
    Compute the square root

    math.sqrt(9) sends 9 into the square-root function and returns 3.

  3. 3
    Combine the results

    The multiplication operation uses the value supplied by math.pi and the value returned by math.sqrt(9).

Result

The expression produces a value based on the approximately 15-digit value supplied by math.pi and the square-root result 3.

math.piapproximation of pi9non-negative inputmath.sqrt(9)returns 3multiplicationcombines two valuescombined resultuses pi and 3passes 9supplies pireturns 3produces
What values enter the two math tools, and how are their results combined into one calculation?

Mistakes with Values and Functions

  • Common mistake

    Writing parentheses after math.pi

    What it looks like

    Treating math.pi as though it were a function call

    Why it fails

    math.pi is a variable containing a value, not the square-root-style function described by a call with an argument.

    Do this instead

    Access math.pi directly, without parentheses.

  • Common mistake

    Leaving out parentheses when using math.sqrt

    What it looks like

    Referring to math.sqrt without passing a number

    Why it fails

    math.sqrt() is a function and needs an argument so it knows which non-negative number to process.

    Do this instead

    Call the function with parentheses and place the number inside them, as in math.sqrt(9).

  • Common mistake

    Assuming math.sqrt() performs later arithmetic

    What it looks like
    Expecting math.sqrt(2) / 2.0 to be handled entirely by math.sqrt()
    Why it fails

    math.sqrt() computes and returns only the square root. The division is a separate operation performed on the returned value.

    Do this instead

    Trace the calculation in order: input, square-root result, then arithmetic operation.

  • Common mistake

    Typing a short approximation instead of using math.pi

    What it looks like

    Replacing math.pi with a manually entered approximation in a precision-sensitive calculation

    Why it fails

    The math module provides an approximation of π accurate to about 15 digits.

    Do this instead

    Use math.pi when the calculation needs the math module's supplied approximation.

Check Your Understanding

MEDIUM

Explain the value flow in an expression that uses math.pi and math.sqrt(16) together. Identify which item supplies a stored mathematical value, which item performs a function call, what result the function returns, and how an arithmetic operation could use both results.

Hints
  • Remember that math.pi is accessed without parentheses.
  • The argument to math.sqrt() is 16.
  • Trace the calculation after math.sqrt() returns its result.
EASY

Suppose a calculation contains math.sqrt(25) / 5. Describe the two stages of the calculation and explain why the result can be checked using ordinary arithmetic.

Hints
  • First determine what math.sqrt(25) returns.
  • Then apply the division by 5.
  • The returned function value becomes the input to the later arithmetic operation.

Key Takeaways

  1. 1
    Use math.pi as the math module's approximation of π, accurate to about 15 digits, and access it without parentheses.
  2. 2
    Use math.sqrt() with parentheses and a non-negative argument to compute a square root.
  3. 3
    A value flows into math.sqrt(), the function returns a result, and later arithmetic can use that result.
  4. 4
    Combining math.pi and math.sqrt() supports calculations in geometry, physics, and engineering.
  5. 5
    To verify a result, separate the function's returned value from the arithmetic operation that follows it.

At a glance

  • math.pi is a variable containing an approximation of π accurate to about 15 digits, so it is used without parentheses.
  • math.sqrt() computes the square root of a non-negative number and requires parentheses around its argument.
  • Python returns the square-root result before applying later arithmetic such as division or multiplication.
  • Using math.pi and math.sqrt() together helps build precise calculations for geometry, physics, and engineering.

Related concepts

More concepts starting with O