• Create Account

# Convert floating point operation to integer

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Conoktra  Members   -  Reputation: 127

Like
0Likes
Like

Posted 16 February 2012 - 01:20 AM

Hello,

I have a forumlae, "((a / b) * (max - min)) + min", that needs to be converted to integer math rather than floating point. max and min are unsigned integers, and (a / b) results in a float (b is always greater than a, so a/b always ranges from 0.0 to 1.0).

How would I go about calculating the same result without using floating point math? I am sure the solution is simple, but I am at a total loss for how to do this.

Thanks .

### #2clb  Members   -  Reputation: 1634

Like
0Likes
Like

Posted 16 February 2012 - 01:44 AM

In integer math dealing with fractions of one, it is always best to do the multiplication first, then followed by the division, i.e. a * (max - min) / b + min. Also, depending on the logicyou are after, be wary of one-off errors that can occur, i.e. evaluate whether you would instead like to have a * (max - min + 1) / b + min.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #3NightCreature83  Crossbones+   -  Reputation: 2021

Like
0Likes
Like

Posted 16 February 2012 - 07:26 AM

In integer math dealing with fractions of one, it is always best to do the multiplication first, then followed by the division, i.e. a * (max - min) / b + min. Also, depending on the logicyou are after, be wary of one-off errors that can occur, i.e. evaluate whether you would instead like to have a * (max - min + 1) / b + min.

if you multiply min at the end with b and then divide the whole equation you will end up with all products and addition operators being done in integer math, and the division by b will then give you the resulting float value.
You should see this as a maths equation nd then just apply the normal rules for addition and division, b*min/b = min. With that last addition you will postpone the eventual move into a float untill the very end and should still give you the same result.

Resulting equation:
(a * (max - min) + min * b) / b
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

### #4Conoktra  Members   -  Reputation: 127

Like
0Likes
Like

Posted 16 February 2012 - 10:00 AM

Hehe, I knew it was something simple. Thanks guys, I appreciate it .

### #5Antheus  Members   -  Reputation: 2381

Like
0Likes
Like

Posted 16 February 2012 - 10:12 AM

Nobody seems to have mentioned integer overflow. Make sure to check for that.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS