Link to home
Start Free TrialLog in
Avatar of djhex
djhex

asked on

Normazile float numbers

How can I Normalize two float numbers


Suppose the input of users as this

1.98 x 10E 4   = 19800
 21.22 x 10E5 = 2120000

But I need to set them as

0.19 x 10 E -5
and the second one must be 0 in the first digit and must be the same exponent
0.2122 x 10E-6 -> as this is greater than first I must move the coma of the first one

0.019 x 10 E -6
0.2122 x 10 E -6

I need to make this in a single function.
Avatar of venkateshwarr
venkateshwarr

Is this what u wanted??

double getNormalValue(double dnum)
{
     while(dnum>1)
          dnum /= 10;
     printf("num = %E",dnum);
     return dnum;
}
Hi,

I think venkateshwarr wrote part of
solution, but you need to chek which
of numers is bigger and then normalize
numbers.

#include <iostream>

using namespace std;

int main(){

      double num1 = 0, num2 = 0;

      cout << "Input two numbers" << endl;
      cin >> num1 >> num2;
      
      if (num1 >= num2)
      {
            while(num1 > 1)
            {
                  num1 /= 10;
                  num2 /= 10;
            }
                  
      }
      else
      {
            while(num2 > 1)
            {
                  num1 /= 10;
                  num2 /= 10;
            }
      }

      cout << "num1 = "  << num1
           << " num2 = " << num2
           << endl;
}
void normalize(double &num1, double &num2)
   
     if (num1 >= num2)
     {
          while(num1 > 1)
          {
               num1 /= 10;
               num2 /= 10;
          }
               
     }
     else
     {
          while(num2 > 1)
          {
               num1 /= 10;
               num2 /= 10;
          }
     }

}
The above examples only handle one of four cases, when the numbers are larger than desired, and positive.
The code below works when the numbers are either too big or too small, and for negative values also:



          while( abs(num1) < 1.0 && abs(num2) < 1.0 )    {  num1 *= 10.0;    num2 *= 10.0;  }

          while( abs(num1) > 1.0 && abs(num2) > 1.0 )    {  num1 /= 10.0;     num2 /= 10.0;   }

Avatar of djhex

ASKER

I have to test it when I go home tonight. Thanks For the help.

Does this work on C too? What I need to change?
OOPS, never mind!      All the examples above are scaling the numbers, but you apparently just want to display both of them with the same exponent, and the larger one between 0 and 1.   Try this instead:

double Larger;   int Exponent;

if( abs(num1) > abs(num2) ) Larger = num1; else Larger = num2;

Exponent  = 0;


while( abs( Larger ) < 1.0 ) { Larger *= 10.0;  num1 *= 10.0; num2 *= 10.0; Exponent--; }

while( abs( Larger ) >= 1.0 ) { Larger /= 10.0;  num1 /= 10.0; num2 /= 10.0;  Exponent++; }

printf( "num1 = %10.5f  x 10^%d  num2 = %10.5f x 10^%d\n", num1, Exponent, num2, Exponent );

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of djhex

ASKER

I need to normalize to the same exponent because the internal representation of float is in IEEE Format. and I need to sum 2 float numbers in assembly language. SO its better that I send the assembler function the numbers already normalized  so the BITS of the exponent will be the same and I can sum the bits of the mantissa easily without having to program a normalization in assembly.
Avatar of djhex

ASKER

I was making some test in this but I think It Will not Work.

http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

Put
0.101

and 0.0101

The exponent bits are different but as you see THIS HAS THE SAME EXPONENT:


I think I am confused.

What I just want is that binary representation of the exponents bits are the same on both numbers so I can make a binary addition in assembler of the mantisas.

Your're confusing the base 10 printout of the numbers with the base2 representation.

In memory floating-point numbers are kept in a binary normalized format with a binary exponent.  

So when you execute the statement   X = 12345.678 E 5, the compiler at compile time  first converts that string of decimal digits to the correct IEEE format, normalized, then generates the code to do the assignment.

So by the time your program is running, all the IEEE format numbers are already normalized.

Also when you go to add two floating-point numbers, the ADD or SUB instructions align the mantissas as needed-- you never have to do this yourself.

Unless your goal is to SIMULATE the addition with a series of instructions, instead of using the built-in FPU ADD instruction.

Hope this clears things up a bit.


Avatar of djhex

ASKER

My goal is to SIMULATE without using FPU. Any suggestions please.
Avatar of djhex

ASKER

If you know how to  make the addition I will give you more points if Needed.
You could get some ideas by looking at the various floating-point emulation libraries.  

The old PC's didnt all have FPU's, so compilers came with emulation libraries, that emulated the floating-point operations (to various levels of completeness).  

You could compile a little test program that does x = a + b, using the "NOFPU" option on your compiler., then step thru it to see how it does it.

Or download the gcc or watcom C library source code and look at their floating point emulation stuff.

Just to get the rough idea, pls don't copy the code verbatim (or even dysan).
Avatar of djhex

ASKER

Can I make that with any Ide??
I have and Ide called  DevC++ of bloodshed.? DOes it make that?

Avatar of djhex

ASKER

Does it convert my function to assembler? Which file does it generate to the the assembly code?
I don't know aboput bloodshed, to see the FPU emulation code you'll need a debugger that can step into the asm code.

You're probably better off downloading the source of a FPU emulator, such as the one in gcc or Watcom C.