How does round function handle scientific notation?

I’m currently working on a calculator program and am currently dealing with rounding. I want to round to 5 decimal places. Problem is, some functions in my program (for instance, tan(pi/2)) return scientific notation. I’m going to use tan(pi/2) as my example. Without rounding, it returns 1.633123935319537e+16 (this is because pi is given as a constant, not as an irrational number). When I put that through the round function, however, it returns -272241.80841. I’m really confused on how it got a negative number through this.
Another example is cos(pi/2). Without rounding, it returns 6.123233995736766e-17 (this happen for the same reason listed above). Rounding it, it returns -0.0. Admittedly, that’s much closer to what I want. But still not what I want.
How can I fix this?

You are confusing float values with python’s default formatting with user-selected formatted representation. round returns a float, not a representation, that may be different from the input, with loss of information. You almost certainly do not want to do that. If you want to format to 5 decimal places, do that.

>>> format(0.0, '0.5f')
'0.00000'
>>> format(1.633123935319537e+16, '0.5f')
'16331239353195370.00000'

format specs are also used in str.format and f-strings.

For me, on Win10 with 3.12, round(m.tan(m.pi/2), 5) prints as 1.633123935319537e+16, not what you claim.

pi is a constant.

1 Like

I think you need to check your code carefully. You seem to have made a mistake somewhere:

>>> from math import pi, tan
>>> tan(pi/2)
1.633123935319537e+16
>>> round(tan(pi/2), 5)
1.633123935319537e+16

That rather large number is equal to 16331239353195370.0 and is a whole number with no fraction part:

>>> tan(pi/2) % 1.0
0.0

So when you round it to five decimal places, nothing changes.

As for your other example, again you should check your code:

>>> from math import cos
>>> cos(pi/2)
6.123233995736766e-17
>>> round(cos(pi/2), 5)
0.0

Not a negative either.

Python relies on the quality of the maths routines supplied by your platform (operating system and hardware). It is just barely possible that if you are running some odd combination with really bad floating point routines, Python might be returning the bizarre results you saw.

But far more likely is that you have made a mistake somewhere.

Beyond that, most of Terry’s comments about the difference between rounding a float and displaying a float are valid.

Floats in Python have 53 bits of precision. That’s about 16 or 17 decimal digits. (For simplicity, let’s call it 16.) The float 0.0 is actually 0.0000000000000000 even though it is displayed using the minimum number of zeroes needed. You can’t round it to 0.00000 using the round function. But you can use string formatting to display it to five decimal places.

Terry said:

Sure, that’s what Pattydaone said. But the real mathematical π is an irrational number, and tan(π/2) is undefined.

I suspect the OP is rounding the input in addition to the output (possibly because the input is itself the result of some operation).

>>> from math import pi, tan, cos
>>> round(tan(round(pi/2, 5)), 5)
-272241.80841
>>> round(cos(round(pi/2, 5)), 5)
-0.0
2 Likes