# Accuracy of float numbers

**URL:** https://discourse.processing.org/t/accuracy-of-float-numbers/22096
**Category:** Coding Questions
**Created:** [June 23, 2020, 2:06pm UTC](https://discourse.processing.org/t/accuracy-of-float-numbers/22096 "2020-06-23T14:06:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [June 23, 2020, 2:06pm UTC](https://discourse.processing.org/t/accuracy-of-float-numbers/22096/1 "2020-06-23T14:06:47Z")

</div>

The map function should produce a float (see code below). If one looks at the first 10 results, with i first and then i mapped between 85.0 and 170.0, we have:  
85 85.0  
86 87.0  
87 89.0  
88 91.0  
89 93.0  
90 95.0  
91 97.0  
92 99.0  
93 101.0  
94 103.0

BUT by my calculations, we should have:

85 85.0000  
86 87.0060  
87 88.9950  
88 91.0010  
89 93.0070  
90 94.9960  
91 97.0020  
92 99.0080  
93 100.9970  
94 103.0030

How do I get the more accurate result? I have tried workarounds such as converting to string and then converting back to float.

```auto
float threshold1 = 85.0;
float threshold2 = 170.0; // threshold2 > threshold1

void setup() {
}

void draw() { 

  for (int i = 85; i < 170; i++ ) {

    float m = map(i, threshold1, threshold2, threshold1, 255.0);
    //
    println(i, m); // error checking
  }
  
  exit();
}

```

@kll @GoToLoop @jeremydouglass

---

<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 23, 2020, 3:05pm UTC](https://discourse.processing.org/t/accuracy-of-float-numbers/22096/2 "2020-06-23T15:05:47Z")

</div>

> [@paulstgeorge](#):
>
> BUT by my calculations, we should have:

I’m not understanding this.

For ever step that your input (85-170) increases, your output (85-255) should increase by 2, because that is the ratio between the two ranges:

(255-85) / (170-85) = 2.0  
170 / 85 = 2.0  
2 / 1 = 2.0

That means only integer answers.

85 85  
86 87  
87 89

Am I misunderstanding?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 23, 2020, 5:10pm UTC](https://discourse.processing.org/t/accuracy-of-float-numbers/22096/3 "2020-06-23T17:10:58Z")

</div>

You calculations are wrong take `i = 94` we get

Parametric position in first range t = (94-85)/(170 - 85) = 0.1058823529

The value in the new range is v = 85 + t \* (255-85) = 103

It is _not_ 103.0030 as you claim - the computer is right. 😌

---

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [June 23, 2020, 5:12pm UTC](https://discourse.processing.org/t/accuracy-of-float-numbers/22096/4 "2020-06-23T17:12:44Z")

</div>

Ah, so that’s how map() works. Thank you for the very clear explanation. My calculations were wrong so my expectations were wrong
