Welcome! Log In Create A New Profile

Advanced

Casting error? What the hell?

Posted by Sanoz0r 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Casting error? What the hell?
May 14, 2007 05:57PM
Hey guys. Using pow(double,double) from cmath, pow (a,b) works out exponential a^b. Check this out.

CODE:
for(int i=0; i<5; i++)
    cout << "i=" << i << " : " << (int)pow(10.0, (double)i) << endl;
    
cout << endl;
    
for(int i=0; i<5; i++)
    cout << "i=" << i << " : " << pow(10.0, (double)i) << endl;

OUTPUT:
i=0 : 1
i=1 : 10
i=2 : 99
i=3 : 1000
i=4 : 9999

i=0 : 1
i=1 : 10
i=2 : 100
i=3 : 1000
i=4 : 10000

What the hell?? Can anyone explain the discrepancy?
Re: Casting error? What the hell?
May 15, 2007 02:00PM
Dear Sanoz0r,

I tried your code on my laptop both with and without the casting to int of the returned double from the pow(double, double) function.

In both cases I got:

i=0 : 1
i=1 : 10
i=2 : 100
i=3 : 1000
i=4 : 10000

in other words, I did not get any casting problems.

I don't know if it makes any difference but I have gone back to the 2006 UNISA disk (for both mingw and DevC++ installations) - in fact I posted a link where it can be downloaded for those who are having compiling problems with the 2007 disk. (See the'Dev C++ errors' thread.) Alternatively, perhaps different computers have slightly different ways of doing floating point calculations within their processors.

Even despite my not having casting errors when trying to replicate your problem, I don't much like the idea of casting from double to int like that. This is because floating point arithmatic is imprecise and small differences could crop up and become more pronounced when casting, e.g. 99.99999... (very close to 100) going to 99 when the fractional part is dropped for casting to a variable of type int. I don't think you are alone in experiencing casting problems, e.g. see:


http://www.thescripts.com/forum/thread133037.html

I simply wrote my own power function that works on type int. This way you should not need to worry about casting 'errors' - the function is relatively trivial and uses iteration since that would be faster than recursion.

Perhaps a lecturer could confirm:

1) what could give rise to casting problems - am I on the right track?
2) whether it is/is not good practice to cast the results from double to int?

My thrupence worth.

Richard
Re: Casting error? What the hell?
May 15, 2007 02:50PM
Hey Richard! Thanks for the reply.

You are right about casting from double to int, it will cause problems. What really bothers me is howcome they didn't include a pow(int, int) in cmath??
Re: Casting error? What the hell?
May 15, 2007 03:27PM
I did wonder that myself!

I think that pow(int, int) is relatively straightforward to implement, so perhaps the authors of cmath assume that C++ programmers will simply do it themselves.

Conversely, pow(double, double) is not easy to write, e.g. consider pow(2.0, 2.5). This is equal to pow(2.0,2.0) * pow(2.0, 0.5) or 4.0 * pow(2.0, 0.5), the latter part being the square root of 2.0. This can't be solved simply like pow(int, int) would.

Still I agree that it would be convenient to have a more comprehensive cmath containing even 'trivial' functions.

Richard
Re: Casting error? What the hell?
May 15, 2007 03:43PM
Yeah, it's not that I'm bothered about implementing it (literally takes a couple of seconds), I just find it weird they wouldn't have provisions for such a common math function!

Anyway smiling smiley
Sorry, only registered users may post in this forum.

Click here to login