My computer cant do math, can your's do ?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

My computer cant do math, can your's do ?

Post by omar shaaban »

Code: Select all

int main()
{
    long double t=5.0f/30.0f;
long double us=1.0f/30.0f;
 
 
    cout <<int(((t)/us))<< endl;
    return 0;
}
in the code above the answer should be 5 but instead its 4 :shock:
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: My computer cant do math, can your's do ?

Post by Radikalizm »

You're probably having some floating point errors which reduces the result to 4 after the integer conversion
It also depends a lot on the compiler you're using, if I remember correctly the MSVC compiler treats a long double like a regular double for x86 architectures, so you get 64-bit precision instead of 80-bit
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: My computer cant do math, can your's do ?

Post by hendu »

Remove the int cast and be happy.
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: My computer cant do math, can your's do ?

Post by ACE247 »

and remove the f's and the (((t)/us)) -> (t/us) they're just not necessary with this.

Because calculation of a variable put in as a equation already occurs, when the t is declared and stored as 5/30th instead of stored as an 'instruction' of 5.0 divided by 30.
The equation does not look like (((5.0f/30.0f)/(1.0f/30.0f)) but like (0.166.../0.033...)
If I'm correct that is... :)
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: My computer cant do math, can your's do ?

Post by docWild »

Write it out as binary operations, then it makes perfect sense. Check out the IEEE floating point standards for proper explanations. Integer casting and division has its place, but it can also be the source of the worst kind of bug.. that which generates no errors.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: My computer cant do math, can your's do ?

Post by serengeor »

this works:

Code: Select all

#include <iostream>
int main()
{
long double t=5.0/30.0;
long double us=1.0/30.0;
 
 
    std::cout <<(int) ( (double) (t/us) )<< std::endl;
    return 0;
}
Working on game: Marrbles (Currently stopped).
Post Reply