# How does round function handle scientific notation?

**URL:** <https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690>\
**Category:** Python Help\
**Created:** [December 5, 2022, 9:03pm UTC](https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690 "2022-12-05T21:03:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pattydaone](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pattydaone/32/6092_2.png) [@pattydaone](https://discuss.python.org/u/pattydaone)\
**Post date:** [December 5, 2022, 9:03pm UTC](https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690/1 "2022-12-05T21:03:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [December 5, 2022, 10:36pm UTC](https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690/2 "2022-12-05T22:36:49Z")

</div>

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.

```auto
>>> 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.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [December 5, 2022, 3:05am UTC](https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690/3 "2022-12-05T03:05:34Z")

</div>

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

> [@](#):
>
> 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

```auto
>>> 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:

```auto
>>> 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:

```auto
>>> 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](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) 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:

> [@](#):
>
> pi _is_ a constant.

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

---

<div class="post-metadata">

**Author:** ![mdickinson](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mdickinson/32/976_2.png) [@mdickinson](https://discuss.python.org/u/mdickinson)\
**Post date:** [December 6, 2022, 9:23am UTC](https://discuss.python.org/t/how-does-round-function-handle-scientific-notation/21690/4 "2022-12-06T09:23:07Z")

</div>

> [@steven.daprano](#):
>
> 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.

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

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

```
