Roundoff Error Question

  • Follow


In the following code fragment, will "Yes" always be printed, not only 
for 12.34 but for any variation of x with two digits after the dot?
The issue is that x is internally represented as a floating point binary 
number.

Thank you,
Joe

double x = 12.34; // two digits after the dot
int y = 100*x;
if(y == 1234)
   cout << "Yes" << endl;
else
   cout << "No" << endl;









0
Reply joeh8556 (19) 2/10/2012 12:56:18 PM

On 2/10/2012 7:56 AM, Joseph Hesse wrote:
> In the following code fragment, will "Yes" always be printed, not only
> for 12.34 but for any variation of x with two digits after the dot?

Of course not.

> The issue is that x is internally represented as a floating point binary
> number.

If you think a comment is due to that last statement, methinks the most 
fitting is "Duh!"...

> Thank you,
> Joe
>
> double x = 12.34; // two digits after the dot
> int y = 100*x;
> if(y == 1234)
> cout << "Yes" << endl;
> else
> cout << "No" << endl;

V
-- 
I do not respond to top-posted replies, please don't ask
0
Reply v.bazarov (790) 2/10/2012 1:02:03 PM


"Joseph Hesse"  wrote in message=20
news:ZPadnRtiEaNujqjSnZ2dnUVZ5tqdnZ2d@giganews.com...
>
>In the following code fragment, will "Yes" always be printed, not only =
for=20
>12.34 but for any variation of x with two digits after the dot?

Depends on the value of y.
Have you tried 999999999999999999999999999.34?

>The issue is that x is internally represented as a floating point =
binary=20
>number.

Maybe x is not only represented as a floating point binary, x is a =
floating=20
point binary.

>Thank you,
>Joe
>
>double x =3D 12.34; // two digits after the dot
>int y =3D 100*x;
>if(y =3D=3D 1234)
>   cout << "Yes" << endl;
>else
>   cout << "No" << endl;
>

0
Reply F.Zwarts (271) 2/10/2012 1:23:15 PM

"Fred Zwarts (KVI)"  wrote in message =
news:jh35o3$nd2$1@news.albasani.net...
>
>"Joseph Hesse"  wrote in message=20
>news:ZPadnRtiEaNujqjSnZ2dnUVZ5tqdnZ2d@giganews.com...
>>
>>In the following code fragment, will "Yes" always be printed, not only =
for=20
>>12.34 but for any variation of x with two digits after the dot?
>
>Depends on the value of y.

That should be: Depends on which value is used instead of 1234.
>Have you tried 999999999999999999999999999.34?
>
>>The issue is that x is internally represented as a floating point =
binary=20
>>number.
>
>Maybe x is not only represented as a floating point binary, x is a =
floating=20
>point binary.
>
>>Thank you,
>>Joe
>>
>>double x =3D 12.34; // two digits after the dot
>>int y =3D 100*x;
>>if(y =3D=3D 1234)
>>   cout << "Yes" << endl;
>>else
>>   cout << "No" << endl;
>>

0
Reply F.Zwarts (271) 2/10/2012 1:25:21 PM

Joseph Hesse <joeh@gmail.com> wrote:
> In the following code fragment, will "Yes" always be printed, not only 
> for 12.34 but for any variation of x with two digits after the dot?

  No. In your example you are multiplying a floating point value with
100.0 and then converting it to an int. Even if the result fits inside
an int (which isn't a given) it will be clamped (iow. rounded towards
zero). If the multiplication results in a slight rounding error so that
the result is not exact in the least-significant bits of the floating
point representation, the clamping will make the result be one less than
expected.

  Curiously, there's no function in C++98 to round a floating point value
to the nearest integer. C++11 inherits one from C99 (AFAIK), namely
std::round(). In other words "int(std::round(100*x))" should always give
the correct result (assuming it fits inside an int).

  (You can, of course, write your own round() function, but its
implementation is actually not unambiguous.)
0
Reply nospam270 (2853) 2/11/2012 4:00:12 PM

4 Replies
29 Views

(page loaded in 0.061 seconds)

Similiar Articles:













7/16/2012 8:56:25 AM


Reply: