fraction representation in c++
up vote
0
down vote
favorite
fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:
- the first one in float variable
- the second one in double variable
- it's mean that one of them is greater than the other, and the biggest is the double variable
so if we subtract the double from the float, it is expected that the result will be positive
why this code printed ( "Positive" )
float afloat = 0.9;
if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;
while this code printed ("Negative") ??
float afloat = 0.2;
if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;
c++ binary truncation
add a comment |
up vote
0
down vote
favorite
fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:
- the first one in float variable
- the second one in double variable
- it's mean that one of them is greater than the other, and the biggest is the double variable
so if we subtract the double from the float, it is expected that the result will be positive
why this code printed ( "Positive" )
float afloat = 0.9;
if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;
while this code printed ("Negative") ??
float afloat = 0.2;
if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;
c++ binary truncation
3
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
3
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
2
Your hypothesis is incorrect:the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
– Fabio
Nov 10 at 18:25
1
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
– PaulMcKenzie
Nov 10 at 19:12
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:
- the first one in float variable
- the second one in double variable
- it's mean that one of them is greater than the other, and the biggest is the double variable
so if we subtract the double from the float, it is expected that the result will be positive
why this code printed ( "Positive" )
float afloat = 0.9;
if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;
while this code printed ("Negative") ??
float afloat = 0.2;
if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;
c++ binary truncation
fractions (0.9) and (0.2) make a truncation error when representing in float or double
so if we represent 0.9 or 0.2 two times:
- the first one in float variable
- the second one in double variable
- it's mean that one of them is greater than the other, and the biggest is the double variable
so if we subtract the double from the float, it is expected that the result will be positive
why this code printed ( "Positive" )
float afloat = 0.9;
if ( 0.9 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9 - afloat < 0 ) cout << "Negative" << endl;
while this code printed ("Negative") ??
float afloat = 0.2;
if ( 0.2 - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.2 - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.2 - afloat < 0 ) cout << "Negative" << endl;
c++ binary truncation
c++ binary truncation
edited Nov 10 at 18:16
asked Nov 10 at 18:14
Ehab Fawzy
313
313
3
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
3
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
2
Your hypothesis is incorrect:the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
– Fabio
Nov 10 at 18:25
1
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
– PaulMcKenzie
Nov 10 at 19:12
add a comment |
3
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
3
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
2
Your hypothesis is incorrect:the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)
– Fabio
Nov 10 at 18:25
1
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.
– PaulMcKenzie
Nov 10 at 19:12
3
3
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
3
3
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
2
2
Your hypothesis is incorrect:
the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)– Fabio
Nov 10 at 18:25
Your hypothesis is incorrect:
the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)– Fabio
Nov 10 at 18:25
1
1
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.– PaulMcKenzie
Nov 10 at 19:12
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.– PaulMcKenzie
Nov 10 at 19:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
The literal 0.9
is a double precision value approximation to 9/10
, and your declaration
float afloat = 0.9
truncates this value to single precision, thereby losing information. This code
float afloat = 0.9f;
if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;
will generate the output
Equal
even if 0.9f
is not exactly equal to the fraction 9/10
. The sign of the quantization error will differ for different fractions, and for different floating point formats.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The literal 0.9
is a double precision value approximation to 9/10
, and your declaration
float afloat = 0.9
truncates this value to single precision, thereby losing information. This code
float afloat = 0.9f;
if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;
will generate the output
Equal
even if 0.9f
is not exactly equal to the fraction 9/10
. The sign of the quantization error will differ for different fractions, and for different floating point formats.
add a comment |
up vote
1
down vote
The literal 0.9
is a double precision value approximation to 9/10
, and your declaration
float afloat = 0.9
truncates this value to single precision, thereby losing information. This code
float afloat = 0.9f;
if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;
will generate the output
Equal
even if 0.9f
is not exactly equal to the fraction 9/10
. The sign of the quantization error will differ for different fractions, and for different floating point formats.
add a comment |
up vote
1
down vote
up vote
1
down vote
The literal 0.9
is a double precision value approximation to 9/10
, and your declaration
float afloat = 0.9
truncates this value to single precision, thereby losing information. This code
float afloat = 0.9f;
if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;
will generate the output
Equal
even if 0.9f
is not exactly equal to the fraction 9/10
. The sign of the quantization error will differ for different fractions, and for different floating point formats.
The literal 0.9
is a double precision value approximation to 9/10
, and your declaration
float afloat = 0.9
truncates this value to single precision, thereby losing information. This code
float afloat = 0.9f;
if ( 0.9f - afloat > 0 ) cout << "Positive" << endl;
else if ( 0.9f - afloat == 0 ) cout << "Equal" << endl;
else if ( 0.9f - afloat < 0 ) cout << "Negative" << endl;
will generate the output
Equal
even if 0.9f
is not exactly equal to the fraction 9/10
. The sign of the quantization error will differ for different fractions, and for different floating point formats.
edited Nov 10 at 22:11
answered Nov 10 at 22:06
sveinbr
965
965
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241982%2ffraction-representation-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Is floating point math broken?
– Jesper Juhl
Nov 10 at 18:15
3
"it's mean that one of them is greater than the other, and the biggest is the double variable" - Why would this be always true?
– Holt
Nov 10 at 18:23
2
Your hypothesis is incorrect:
the bigger is the double variable
. Search on the web for algorithms to convert decimal numbers to IEEE754 numbers, apply the algorithm step by step manually, and you will understand. Or do the opposite. Look at the binary representation of the IEEE754 numbers in your C program converting them to array of bytes, then convert them manually to decimal numbers using the appropriate formula (see IEEE754 on Wikipedia)– Fabio
Nov 10 at 18:25
1
fractions (0.9) and (0.2)
-- Convert both of those decimal fractions to binary fractions. Then you will see why you are getting the issue you're seeing now. In short, 0.9 and 0.2 cannot be represented exactly as binary floating point numbers.– PaulMcKenzie
Nov 10 at 19:12