-2

Why is the answer of this code snippet 400.002 and not just 400?

#include <iostream>

int main(){
    using namespace std;
    float a = 123.4;
    a = (a - int(a))*1000;
    cout << a << endl;
    return 0;
}

I'm a newbie. Is this due to the accuracy of the float, or is it due to some other reason? What should I do if I just want to keep into consideration of only one decimal place?

Output

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    A decent answer to "what should I do" is currently reading [ask] and [mre]. Copy/pasting text is much easier than capping/uploading/linking, and it follows the rules of the site. – sweenish Aug 26 '23 at 04:55
  • 1
    See: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – ee-4-me Aug 26 '23 at 04:56
  • @斯威尼什语 sorry, I'll pay attention – Bad Athon Aug 26 '23 at 05:27
  • Two things, don't use "C" style casts like `(int)`. Use C++'s `static_cast`. And floating point calculations will have rounding errors because floating point numbers cannot be presented in memory with infinite precission – Pepijn Kramer Aug 26 '23 at 05:27
  • Here is a fun video on floating point numbers and computers : [Tom Scott, Floating point numbers - ComputerPhile](https://www.youtube.com/watch?v=PZRI1IfStY0) – Pepijn Kramer Aug 26 '23 at 05:28
  • @PepijnKramer OK,thank you,it's very useful! – Bad Athon Aug 26 '23 at 05:48
  • See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Jesper Juhl Aug 27 '23 at 01:44

1 Answers1

-1

The floating-point numbers serve as rough approximations of mathematical real numbers. They do not represent the exact value.

So, you can use double-precision floating-point numbers are extremely accurate. As shown in the code snippet:

#include<iostream>
using namespace std;

int main(){
    double a = 123.4;
    a = (a - int(a))*1000;
    cout << a << endl;  // Output 400
    float a2 = 123.4;
    a2 = (a2 - int(a2))*1000;
    cout << a2 << endl; // Output 400.002
    return 0;
}

For more, you can refer the following link: https://learn.microsoft.com/en-us/cpp/build/why-floating-point-numbers-may-lose-precision?view=msvc-170

  • 2
    `double` only reduces the problem, they have exactly the same issues as `float` [`123.4`](https://float.exposed/0x405ed9999999999a) isn't representable by a `double` either – Alan Birtles Aug 26 '23 at 06:49
  • 3
    Re “The floating-point numbers serve as rough approximations of mathematical real numbers. They do not represent the exact value.”: Please do not promote this model. The IEEE-754 standard specifies that each floating-point number represents one real number exactly. It is floating-point operations, not numbers, that are approximations. Each floating-point operation produces the result that is nearest the result of the corresponding real-number-arithmetic operation in the direction chosen by the rounding method in use. This includes conversion of numerals like `123.4` to floating-point. – Eric Postpischil Aug 26 '23 at 11:32