Simple math question

Get answers to all your basic programming questions. No Ogre questions, please!
drwbns
Orc Shaman
Posts: 788
Joined: Mon Jan 18, 2010 6:06 pm
Location: Costa Mesa, California
x 24

Simple math question

Post by drwbns »

Hey guys, forgive me for posting something that probably has a smiple answer to but I'm confused. Why does the following produce a number such as 103249812 when my "y" is a negative number? even when intersection.getY() is a small number, like -74

Code: Select all

Ogre::Math::Floor((intersection.getY()+4) / 32)
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Simple math question

Post by bstone »

And how does the intersection.getY() declaration look? What type is returned by that method?
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Simple math question

Post by areay »

You so funny bstone :D :D
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Simple math question

Post by bstone »

Am I? I had no idea :shock:
drwbns
Orc Shaman
Posts: 788
Joined: Mon Jan 18, 2010 6:06 pm
Location: Costa Mesa, California
x 24

Re: Simple math question

Post by drwbns »

I found that a simple equation like uint + int works fine but if I do (uint + int) / uint I have to change all vars to ints
User avatar
Faranwath
Halfling
Posts: 93
Joined: Mon Jul 09, 2012 2:19 pm
Location: Cuba
x 7

Re: Simple math question

Post by Faranwath »

drwbns wrote:I found that a simple equation like uint + int works fine but if I do (uint + int) / uint I have to change all vars to ints
In general, when performing a computation involving signed and unsigned integral types you should perform static_casts to unify the types, i.e. work with only one type. The exact rules for integral conversions are somewhat tricky, even implementation-defined -- you can check them at clause 4.7 from the ISO C++ specification.

My advice is to cast everything to the type you need; that way is always safe. You should check, however, that you don't loose precision when performing such conversions.