# Accuracy of float numbers

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.

``````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();
}
``````
1 Like

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

85 85
86 87
87 89

Am I misunderstanding?

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. 2 Likes

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

2 Likes