Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do floating point calculations with integers

I have a coprocessor attached to the main processor. Some floating point calculations needs to be done in the coprocessor, but it does not support hardware floating point instructions, and emulation is too slow.

Now one way is to have the main processor to scale the floating point values so that they can be represented as integers, send them to the co processor, who performs some calculations, and scale back those values on return. However, that wouldn't work most of the time, as the numbers would eventually become too big or small to be out of range of those integers. So my question is, what is the fastest way of doing this properly.

like image 829
MetallicPriest Avatar asked Apr 09 '13 18:04

MetallicPriest


People also ask

How do you find the float value of an integer?

The intValue() method in Float Class is a built in method in Java that returns the value specified by the calling object as int after type casting. Parameters: The function accepts no parameter. Return Value: It return the value of FloatObject as int.

Can float take integer values?

Yes, an integral value can be added to a float value. The basic math operations ( + , - , * , / ), when given an operand of type float and int , the int is converted to float first.

Is 2.0 a floating-point number?

The number 2.0 is a floating-point number because it has a decimal in it. The number 2 (without a decimal point) is a binary integer. Floating-point operations involve floating-point numbers and typically take longer to execute than simple binary integer operations.


1 Answers

You are saying emulation is too slow. I guess you mean emulation of floating point. The only remaining alternative if scaled integers are not sufficient, is fixed point math but it's not exactly fast either, even though it's much faster than emulated float.

Also, you are never going to escape the fact that with both scaled integers, and fixed point math, you are going to get less dynamic range than with floating point.

However, if your range is known in advance, the fixed point math implementation can be tuned for the range you need.

Here is an article on fixed point. The gist of the trick is deciding how to split the variable, how many bits for the low and high part of the number.

A full implementation of fixed point for C can be found here. (BSD license.) There are others.

like image 146
Prof. Falken Avatar answered Oct 25 '22 05:10

Prof. Falken