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.

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

Postby omar shaaban » Tue Dec 20, 2011 2:59 pm

cpp 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:
User avatar
omar shaaban
 
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt

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

Postby Radikalizm » Tue Dec 20, 2011 3:04 pm

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
Radikalizm
 
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

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

Postby hendu » Tue Dec 20, 2011 3:39 pm

Remove the int cast and be happy.
hendu
 
Posts: 1556
Joined: Sat Dec 18, 2010 12:53 pm

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

Postby ACE247 » Tue Dec 20, 2011 4:45 pm

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... :)
H8L6L5M4G3H5M7N8S7N9O1R8J1P5M7N9O4P2Q5R6T7U4M3N8X6S5T8W (If you want the secret, why not 'TRY' decrypting it? )
This Door's lock doesn't need a key, the Lock is the key to open the Lock and you don't know how to turn the key...
Link: My Blog :)
User avatar
ACE247
 
Posts: 695
Joined: Tue Mar 16, 2010 12:31 am
Location: Namibia

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

Postby docWild » Tue Dec 20, 2011 6:53 pm

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.
docWild
 
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

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

Postby serengeor » Tue Dec 20, 2011 11:16 pm

this works:
cpp 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).
User avatar
serengeor
 
Posts: 1695
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest