Converting floating point to scaled fixed point 
Author Message
 Converting floating point to scaled fixed point

        c language gurus

        Does any one out there have a short code example of
        how to convert an ieee floating point number to a scaled
        fixed point number.



Fri, 09 Apr 1999 03:00:00 GMT  
 Converting floating point to scaled fixed point

Quote:

>    c language gurus

>    Does any one out there have a short code example of

Homework alert!

Quote:
>    how to convert an ieee floating point number to a scaled

I wrote the following to help someone's homework a while ago. It does a similar
thing. It takes a string representing a decimal floating point number,
and converts it first to a scaled representation, and then thunks it into
IEEE single-precision format.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

#define TWO_TO_32ND_POWER 4294967296.0L

unsigned long ieeeconvert(char *number)

{
        char *istr = number, *fstr = number;
        int neg = 0, exp = 0;
        unsigned long integ, frac, mant;
        double powten;

        /*
         * Step 1
         *
         * break string into integer fractional part
         */

        while(*fstr != 0 && *fstr != '.') {
                if (*fstr == '-' && !neg) {
                        neg = 1;
                        *fstr = ' ';
                }
                fstr++;
        }
        if (*fstr)
                *fstr++ = 0;

        /*
         * Step 2
         *
         * create a fixed-point binary number
         * in (integ, mant)
         */

        integ = atoi(istr);
        frac = atoi(fstr);

        if (integ == 0 && frac == 0)
                return 0;

        for (powten = 1.0; isdigit(*fstr); fstr++, powten *= 10.0);

        mant = frac * TWO_TO_32ND_POWER / powten;

        /*
         * Step 3
         *
         * normalize the number so it's in the range [1,2)
         */

        while (integ == 0) {    /* normalize up         */
                exp--;
                integ |= (mant >> 31);
                mant <<= 1;
        }

        while (integ > 1) {  /* normalize down       */
                exp++;
                mant >>= 1;
                mant |= (integ & 1) << 31;
                integ >>= 1;
        }

        return (mant >> 9) | (((exp + 127) & 0xff)  << 23) | (neg == 1) << 31;

Quote:
}

int main(void)

{
        char number[20];
        unsigned long ieee;

        fgets(number, 20, stdin);

        ieee = ieeeconvert(number);

        printf("hex: %lx\n", ieee);

        return 0;

Quote:
}
>    fixed point number.

It's not the most robust thing in the world, but perhaps it may be useful
to you, if not for anything else other than generating IEEE representations
to test whether your code produces the correct fixed point.


Fri, 09 Apr 1999 03:00:00 GMT  
 Converting floating point to scaled fixed point


Quote:

>        c language gurus

>        Does any one out there have a short code example of
>        how to convert an ieee floating point number to a scaled
>        fixed point number.

Assuming your scaled fixed point number can be held in an integer type
(i.e. is not too big), then, simply, for example:

  i = (int)(f * 100.0);   /* convert f to 2 digit fixed point */

You may want to consider what to do about rounding (or not).      
--
Ray Dunn (opinions are my own) | Phone: (514) 938 9050
Montreal                       | Phax : (514) 938 5225



Tue, 13 Apr 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. floating point rounding or floating point printing ?

2. Converting Microsoft Floating Point type to the IEEE floating

3. WANTED: FIXED POINT C LIBRARY FOR FLOAT OPERATIONS

4. Float or fixed point

5. HELP - (Some) Printer Drivers causing FALSE floating point errors - (how to fix/mask)

6. floating/fixed point lib wanted

7. Implicit decimal points in floating-point reads

8. Convert IBM Floating Point to IEEE 754

9. How to convert a floating point into string?

10. Converting HEX to Floating-Point Manually

11. How to convert floating point from VAX to Sun

12. How to convert floating point from VAX to Sun

 

 
Powered by phpBB® Forum Software