0

i need to write function that add two numbers but we have a processor that does not support a Variable type Float !!

So to represent fractions , is using a long integer is represented with actually represented using 4B-32bit.

We define the long bits as follows:

the MSB marked S -signed .

The 8 bits following marked E - Exponentially.

The remaining 23 bits marked M -mantissa.

The following formula represents the fraction in long:

(E^2)(M)(S^-1).

and i need to write the function :

unsigned long add(unsigned long float1, unsigned long float2)

that Receives two long values (which are actually fractions),add them and return the answer in long .

I tried to do something like this but i get stuck .

unsigned long add(unsigned long float1, unsigned long float2) {

    char E1=float1>>23, E2=float2>>23, E3;

. . . .

thanks :)

Yinon Haver
  • 11
  • 1
  • 4

1 Answers1

0

To paraphrase sui's answer, here is some pseudo code.

while exponent(f1) > exponent(f2):
    increment exponent(f2)
    shiftleft mantissa(f2)

while exponent(f2) > exponent(f1):
    increment exponent(f1)
    shiftleft mantissa(f1)

add mantissa(f1) to mantissa(f2)
adjust exponent
calculate sign
Community
  • 1
  • 1
Matthew
  • 160
  • 9