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: 2D FFT of images - comp.soft-sys.matlabRound-off error? How big are the imaginary parts compared to the conjugate? e.g., are ... > Hi there, > > I have a few questions relating to the use of the fft2 function ... Algorithm behind "sum" command? - comp.soft-sys.matlab... sum" command is implemented by any algorithm that is robust against round-off error? ... Post Question | Groups ... Converting RGB / YBR back and forth - comp.compressionSo basically I can reformulate my question into: - what are the correct equation ... CBCR components to RGB components in the range [0..255], subject to roundoff error. Welford algorithm for computing standard deviation - comp ...All of the single pass methods of computing variance have problems with round-off error ... Here is the volume in question: >> >> I feel that you have confused the notions ... Cut a plane through 3D surface - comp.soft-sys.matlab... Post Question | Groups ... not (or suppose they are very very close and it > might plausibly be round-off error ... Rounding off - comp.lang.idl-pvwaveWhy I try to answer a question when I have about 10 different things going on is ... the "sum" command is implemented by any algorithm that is robust against round-off error? cos(pi/2) error in the symbolic toolbox? - comp.soft-sys.matlab ...... Post Question | Groups ... version of Matlab and now I got a strange >> error ... symbolically, so you avoid double precision roundoff ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...... Post Question | Groups | Stream ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 Follow pow/log functions in fixed point - comp.dsp... Post Question | Groups ... this factor every time one had used a log10(), but you will have less round-off error ... PKGADD Error - comp.unix.solarisWindows may round off the size in its display, use checksums or exact byte ... Question On Pkgadd Error - comp.sys.sun.admin... failed - process </usr/bin/cpio -icdumD -C ... Difference Between :"round Off Error And Truncation Error". - Find ...Question Categories. All Categories; Arts and Entertainment; Culture and Religion; Current Events; Education; Family and Friends; Food and Drink; Health and Wellness Round-off error - Wikipedia, the free encyclopediaA round-off error, also called rounding error, is the difference between the calculated approximation of a number and its exact mathematical value. Numerical analysis ... 7/16/2012 8:56:25 AM
|