diego9403
diego9403

Reputation: 153

C++ Type of variables - value

I am the beginner, but I think same important thinks I should learn as soon as it possible. So I have a code:

float fl=8.28888888888888888888883E-5;
cout<<"The value = "<<fl<<endl;

But my .exe file after run show:

8.2888887845911086e-005

I suspected the numbers to limit of the type and rest will be the zero, but I saw digits, which are random. Maybe it gives digits from memory after varible? Could you explain me how it does works?

Upvotes: 0

Views: 194

Answers (4)

Adham Gamal
Adham Gamal

Reputation: 795

each data type has range after this range all number is from memory or rubbish so you have to know this ranges and deal with it when you write code. you can know this ranges from here or here

Upvotes: 0

Shadowwolf
Shadowwolf

Reputation: 983

What you're seeing is a result of the fact that you cannot exactly represent a floating-point number in memory. Because of this, floats will be stored as the nearest value that can be stored in memory. A float usually has 24 bits used to represent the mantissa, which translates to about 6 decimal places (this is implementation defined though, so you shouldn't rely on this). When printing more than 6 decimal digits, you'll notice your value isn't stored in memory as the value you intended, and you'll see random digits.

So to recap, the problem you encountered is caused by the fact that base-10 decimal numbers cannot be represented in memory, instead the closest number to it is stored and this number will then be used.

Upvotes: 1

Pascal Cuoq
Pascal Cuoq

Reputation: 80355

I suspected the numbers to limit of the type and rest will be the zero

Yes, this is exactly what happens, but it happens in binary. This program will show it by using the hexadecimal printing format %a:

#include <stdio.h>

int main(int c, char *v[]) {
  float fl = 8.28888888888888888888883E-5;

  printf("%a\n%a\n", 8.28888888888888888888883E-5, fl);
}

It shows:

0x1.5ba94449649e2p-14
0x1.5ba944p-14

In these results, 0x1.5ba94449649e2p-14 is the hexadecimal representation of the double closest to 8.28888888888888888888883*10-5, and 0x1.5ba944p-14 is the representation of the conversion to float of that number. As you can see, the conversion simply truncated the last digits (in this case. The conversion is done according to the rounding mode, and when the rounding goes up instead of down, it changes one or more of the last digits).

When you observe what happens in decimal, the fact that float and double are binary floating-point formats on your computer means that there are extra digits in the representation of the value.

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283863

I suspected the numbers to limit of the type and rest will be the zero

That is what happens internally. Excess bits beyond what the type can store are lost.

But that's in the binary representation. When you convert it to decimal, you can get trailing non-zero digits.

Example:

0b0.00100 is 0.125 in decimal

Upvotes: 2

Related Questions