# Inaccuracy in exponential pow() function

**URL:** <https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817>\
**Category:** Coding Questions\
**Created:** [June 9, 2018, 9:06pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817 "2018-06-09T21:06:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neoumlaut](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neoumlaut](https://discourse.processing.org/u/neoumlaut)\
**Post date:** [June 9, 2018, 9:06pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817/1 "2018-06-09T21:06:30Z")

</div>

I’m working on a program that displays data from an oscilloscope over a serial connection and have run into a problem converting from scientific notation. If I receive a number, for example, 5.72E-1, I then convert it using the pow function as follows:

println(5.72 \* pow(10, -1));

The result I get, instead of being 0.572, is 0.57199997. Is this expected behavior? The answer certainly is close, but this seems like something a modern computer would be able to get exactly right. Am I making a mistake in how I process the data?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 9, 2018, 9:17pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817/2 "2018-06-09T21:17:22Z")

</div>

You are doing nothing wrong. This is a quirk of computers - even modern ones.

Basically, `float` numbers lose some precision because they are trading off exact values for the ability to store so many different numbers. Internally, all numbers are stored in binary, and there are only so many bits reserved for a float to use. One bit is for the sign (+), some other bits are for the exponent (-2). And the rest are for what is called the mantissa - this is the 0.5712.

But you can’t always store “0.5712” in the few bits the mantissa has. So you get rounding errors.

… There are probably a bunch of better explanation about this online.

---

<div class="post-metadata">

**Author:** ![neoumlaut](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neoumlaut](https://discourse.processing.org/u/neoumlaut)\
**Post date:** [June 9, 2018, 9:28pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817/3 "2018-06-09T21:28:11Z")

</div>

Ok that makes sense. Is there a simple way to round the answer to an appropriate value? The amount I would want to round to depends on the length of the original value.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 9, 2018, 10:06pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817/4 "2018-06-09T22:06:07Z")

</div>

Most of the time, you want to round when you want to present your data, which means it gets converted to string, as in the case of the value used in `println()`. Usually in Processing I use [nfs()](https://processing.org/reference/nfs_.html). If you want to use scientific notation, I recommend you check standard ways this is done in Java.

As an example, I could use `nfs(1.0/3.0 ,0 ,2)` to print to decimal points.

Kf

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 10, 2018, 2:50pm UTC](https://discourse.processing.org/t/inaccuracy-in-exponential-pow-function/817/5 "2018-06-10T14:50:58Z")

</div>

Number formatting in Processing are a set of similar-but-not-the-same nf functions. I sometimes find myself reviewing them as a group.

Number formatting:

- [nf()](https://processing.org/reference/nf_.html)
- [nfc()](https://processing.org/reference/nfc_.html)
- [nfp()](https://processing.org/reference/nfp_.html)
- [nfs()](https://processing.org/reference/nfs_.html)
