casting bug

S

sergio

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
 
J

Jack Klein

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.
 
M

Mike Wahler

sergio said:
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-lite/newbie.html#faq-29.16
http://www.parashift.com/c++-faq-lite/newbie.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
 
R

Ron Natalie

sergio said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top