I have some code that generates random numbers on the Arduino, it then sends these random numbers firstly, using Serial.print(float data,int lenght)
and as a floating point number using the following function
void send_float (float arg)
{
// get access to the float as a byte-array:
byte * data = (byte *) &arg;
// write the data to the serial
Serial.write (data, sizeof (arg));
Serial.println();
}
I then receive this data using python, take the absolute difference of the two values, discard any differences less than the length with which I send the string and output the rest to the screen.
I observe two things, a) Fluctuations of the order with which I send the data (ie. if I send to 6 decimal places then I see fluctuations of ~1e-6), this is reasonable and b) I see fluctuations of absolute value 1e-8 or lower independent of what accuracy I send the string.
Even if I send the string to 11 decimal places, when I compare against the float I see fluctuations up to but not above 1e-8.
Why? This doesn't make any sense. My full code is actually the answer to an old unanswered stack exchange question
-
The precision of a float is around 7.2 decimal digits. en.wikipedia.org/wiki/Single-precision_floating-point_formatBrettFolkins– BrettFolkins2015年02月25日 16:55:11 +00:00Commented Feb 25, 2015 at 16:55
1 Answer 1
This is a somewhat dumb question but, in case anyone else searches it as pointed out:
The precision of a float is around 7.2 decimal digits. en.wikipedia.org/wiki/Single-precision_floating-point_format – BrettAM
So in python the number goes float -> double -> decmial, whereas on the arduino the number goes float -> decimal. The additional step of the double in the case of python causes the discrepancies. But anything after the 7th decimal place is nonesense anyway.