Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   casting bug (http://www.velocityreviews.com/forums/t287858-casting-bug.html)

sergio 01-01-2005 08:15 PM

casting bug
 
hi, i've been proggraming for years, and have no ideia what the bug is
in this simple code, does someone has a clue? I'm including all the
code (20 lines).

the output is:
2
2
3
3

instead of the expected:
3
3
3
3

#include <iostream.h>
#include <math.h>

template <class T>
T f(T index,T N)
{
double t=(log(8)/log(2));
//at this point of the code t=3, but after the cast it turns to
2!

return (T)t;
}

int main()
{
cout<<f<int>(8,2)<<endl;
cout<<f<short>(8,2)<<endl;
cout<<f<float>(8,2)<<endl;
cout<<f<double>(8,2)<<endl;

return 0;
}

thanks for any help


Jack Klein 01-01-2005 09:13 PM

Re: casting bug
 
On 1 Jan 2005 12:15:02 -0800, "sergio" <sergiozin2@hotmail.com> wrote
in comp.lang.c++:

> hi, i've been proggraming for years, and have no ideia what the bug is
> in this simple code, does someone has a clue? I'm including all the
> code (20 lines).
>
> the output is:
> 2
> 2
> 3
> 3
>
> instead of the expected:
> 3
> 3
> 3
> 3
>
> #include <iostream.h>
> #include <math.h>
>
> template <class T>
> T f(T index,T N)
> {
> double t=(log(8)/log(2));
> //at this point of the code t=3, but after the cast it turns to
> 2!
>
> return (T)t;
> }
>
> int main()
> {
> cout<<f<int>(8,2)<<endl;
> cout<<f<short>(8,2)<<endl;
> cout<<f<float>(8,2)<<endl;
> cout<<f<double>(8,2)<<endl;
>
> return 0;
> }
>
> thanks for any help


The calculations used by the log() function do not produce exact
results. Instead you get slightly inexact results. When you divide
the two inexact results, you get an inexact answer, and one that is
less than exactly 3.0. Conversion of a floating point value to an
integer type throws away any fractional part.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Mike Wahler 01-01-2005 09:21 PM

Re: casting bug
 

"sergio" <sergiozin2@hotmail.com> wrote in message
news:1104610502.242333.288790@z14g2000cwz.googlegr oups.com...
> hi, i've been proggraming for years, and have no ideia what the bug is
> in this simple code, does someone has a clue? I'm including all the
> code (20 lines).
>
> the output is:
> 2
> 2
> 3
> 3
>
> instead of the expected:
> 3
> 3
> 3
> 3
>
> #include <iostream.h>
> #include <math.h>
>
> template <class T>
> T f(T index,T N)
> {
> double t=(log(8)/log(2));
> //at this point of the code t=3, but after the cast it turns to
> 2!


Actually, it's very likely not exactly 3.0 (but very close).
On my compiler, 't' is equal to 2.9999999999999996. So of
course converting that value to an integer type will discard
the fractional portion, yielding 2. Most floating point
values are not exactly representable in binary; see:
http://www.parashift.com/c++-faq-lit...html#faq-29.16
http://www.parashift.com/c++-faq-lit...html#faq-29.17
http://docs.sun.com/source/806-3568/ncg_goldberg.html

> return (T)t;
> }
>
> int main()
> {
> cout<<f<int>(8,2)<<endl;
> cout<<f<short>(8,2)<<endl;
> cout<<f<float>(8,2)<<endl;
> cout<<f<double>(8,2)<<endl;
>
> return 0;
> }


-Mike



Ron Natalie 01-01-2005 11:06 PM

Re: casting bug
 
sergio wrote:

> double t=(log(8)/log(2));
> //at this point of the code t=3, but after the cast it turns to


No, at this point t is some floating point value close to 3.0.
The conversion to an integer type discards the fractional part,
so if the value is slightly less than 3.0, it will end up as 2.
You probably want to round when converting to int.


All times are GMT. The time now is 12:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.