# doing floating operations using integer operations

• 08-14-2006
ammalik
doing floating operations using integer operations
How one can do floating point operations with using only integer operations e.g. finding r in x^2 + y^2 = r^2 with out using any floating point operations

Thanks
• 08-14-2006
OnionKnight
x*x + y*y? Don't think there's any different approach to sqrt() though.
In situations where you need multiplication you can use fractions instead of floats, e.g (x*355)/113 instead of x*pi or (x*95)/100 instead of x*0.95. This introduces a possible problem with integer overflow though.
• 08-14-2006
dwks
Why don't you want to use floating point numbers?
• 08-14-2006
CornedBee
You could use fixed-point math. But you'll have to write pretty much everything yourself.
• 08-14-2006
VirtualAce
Floating point on modern machines is a tad faster than integer ops. No need for fixed point anymore.
• 08-14-2006
jafet
Quote:

How one can do floating point operations ... ... ... with out using any floating point operations
:rolleyes:

unsigned long long comes to mind.
• 08-14-2006
FillYourBrain
Quote:

Originally Posted by jafet
:rolleyes:

unsigned long long comes to mind.

?

how does a 64 bit integer solve his problem?
• 08-14-2006
dwks
You use fixed point math, which, as Bubba pointed out, isn't much faster anymore.

An example is storing monetary values in an integer, the cent value in the first two digits, and dollars in the other digits. To store \$14.68, you would store 1468. There's a fixed point -- the decimal point is always in the second place.
• 08-14-2006
quzah
Sometimes you need accuracy over speed. Floating point isn't accurate.

Quzah.
• 08-14-2006
dwks
Like with money. You wouldn't want ten thousand 99 dollar items to cost \$9900.13, now would you?
• 08-15-2006
jafet
Of course I would! That'd be nearly 99% off the store price! It's always nice to buy in bulk :) Unless you meant, of course, 99 cents per item.