0

If there's a char* type "37.2", I'd like to change it to float type 37.2, but it's changed to 37.2000008 when I used the atof function. How do I change it to 37.2?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • That's not possible. I think there can't be an exact representation of `37.2` as binary floating point number. – Lukas-T Sep 15 '20 at 09:11
  • 1
    @churill: Correct. 37.2 = 186/5, and 5 is not a power of 2. Binary floating point allows exact representation of numbers in the form `mantissa * 2^exponent`. – MSalters Sep 15 '20 at 10:36
  • `"37.2"` is not a `char*`. It's an array of char. Yes, an array decays into a pointer to its first element in many situations, but it's important to keep in mind that converting text to a numeric value involves **text**. – Pete Becker Sep 15 '20 at 12:45

0 Answers0