Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: a float = 223.98 prints 223.9799957

  1. #1

    a float = 223.98 prints 223.9799957

    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
    
    }

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    733
    I always get 223.98 when using a double. "%.2f" works even with a float.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,224
    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 by luni; 10-16-2020 at 04:32 PM.

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,914
    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

  5. #5
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    48
    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.

  6. #6
    Thanks to all, i'll try some of the suggestions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •