1

While studying about float datatypes I wrote a program in C++:

float a = 0.3, b = 0.4, c = 0.7;
cout << "a+b= " << (a + b) << endl;
if ((a + b) == c)
{
    cout << "Success..." << endl;
}
else
{
    cout << "Failure..." << endl;
}

The output I received was:

a+b= 0.7
Failure...

I am using visual studio code as my IDE and it facilitates that on hovering the cursor over a declared variable it shows the approximate value assigned to the variable and I realized that for a it was 0.2999999999999999889, for b:0.4000000000000000222 and for c:0.6999999999999999556.

So, my question is that is there a method/a data type which can store the true value of a floating point. And why does the system stores these values in approximate form rather than in the true value.

  • *So, my question is that is there a method/a data type which can store the true value of a floating point.* LAS, double precision – kesarling He-Him Dec 15 '20 at 15:40
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken – TheUndeadFish Dec 15 '20 at 15:41
  • 7
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – kesarling He-Him Dec 15 '20 at 15:41
  • *rather than in the true value* What is this supposed to mean? – super Dec 15 '20 at 15:44
  • 1
    `float`s do store the "true value" of floating points, but not every number is exactly representable as floating point – 463035818_is_not_an_ai Dec 15 '20 at 15:45
  • if you want to do maths with exact numbers then don't use floating point numbers. And almost never you should compare them via `==` – 463035818_is_not_an_ai Dec 15 '20 at 15:46
  • @super by "true value" i meant the exact figures assigned by the user/programmer. – Tushar Bharti Dec 15 '20 at 15:47
  • This is not possible with the floating point types. The proposed duplicate explains how `.1` is always an approximation similar to 1/3 in base 10. – drescherjm Dec 15 '20 at 15:48
  • 1
    it isnt very intuitive, because they are stored as binary, while we use decimals most of the time. But consider that with only decimals it is already a problem to get `(1/3) * 3 == 1` working as expected – 463035818_is_not_an_ai Dec 15 '20 at 15:50
  • 1
    You should also read this: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html "What Every Computer Scientist Should Know About Floating-Point Arithmetic" – U. W. Dec 15 '20 at 15:52
  • 1
    @TusharBharti My question didn't need an answer. The point of the question is that if you start to really think about what that would mean and how it would work in a computer the answer becomes more apparent by itself. – super Dec 15 '20 at 15:53
  • There are big math libraries that can give you more precision than long double at the cost of performance and memory but there is always a limit to the precision. – drescherjm Dec 15 '20 at 15:55

1 Answers1

3

Why does the system assign approximate value to the floating point numbers

In short: Because there are infinitely many fractional numbers while the computer doesn't have infinite memory.

The compromising solution to that is to represent only some of the fractional numbers. And since computer hardware often use binary base, it just so happens that many decimal fractions such as 1/10 are not those that can be represented.

is there a method/a data type which can store the true value of a floating point.

Those values that you see true values stored in the floating point.

If you mean, is there a datatype that can represent 1/10 and the other mentioned fractions accurately, then

  1. No, there is no such built-in type in C++
  2. But such numbers can be represented with more complex structures. A very trivial example is to use int nominator = 1, denominator = 10. A class type can closely emulate operations of an integer type through operator overloads. This particular naïve representation is not efficient due to many values that have duplicate representations.
eerorika
  • 232,697
  • 12
  • 197
  • 326