a float = 223.98 prints 223.9799957

KrisKasprzak

Well-known member
I'm trying to convert a float to char -- long story.
goals

float char
1.2 1.2
23.007 23.007
1234.1 1234.1


but
float n1 = 223.98;
Serial.println(n1, 7); // prints 223.9799957

and
sprintf(dn, "%f", data);
Serial.println(dn); // prints 223.979996


Any thoughts?





Code:
void setup() {

  Serial.begin(9600);
  while (!Serial) {}

  float n1 = 223.98; // double isn't any better

  Serial.println(n1, 7); // prints 223.9799957

  ItBreaks(n1);

}

void loop() {
}

void ItBreaks(float data) {

  char dn[20];
  sprintf(dn, "%f", data);

  Serial.print("Converted to a char:");
  Serial.println(dn); // prints 223.979996

}
 
Are you confused that you don't get 223.98 back or that println and printf give different results?

In the first case:
Most floating point numbers can not be stored correctly in float/double variables. Which is clear since there is an infinite number of floating point numbers but you can only store 2^32 different values in 4 bytes. I recommend to have a look here: https://www.h-schmidt.net/FloatConverter/IEEE754.html. If you enter 223.8 in the converter you get 223.9799957275390625 as actual and correct representation of 223.8. So, your statement float n1 = 223.98 actually stores the value 223.9799957275390625 in your n1 variable which corresponds to the output of printf/println.

In the second case:
your println statement rounds the stored number to 7 digits and sprintf rounds to 6 digits per default. try %.7f which should round to 7 digits in sprintf and you should get the same results (untested...)
 
Last edited:
Is that not what one would expect?
All data are store in binary form, this is also valid for the mantissa. I guess that 223.98 cannot be expressed in binary form (binary exponent and mantissa).
I understand that the times of a base10 computer were only short
 
Here's one suggestion. This
Code:
int n = 7-ceil(log10(data));
  sprintf(dn, "%.*f",n, data);
will convert 7 significant figures, which are all that are accurately stored in a float, but will include trailing zeros. You'll have to remove those.
 
Back
Top