Hot!Converting an int to float without the libraries.

Author
NKurzman
A Guy on the Net
  • Total Posts : 16032
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
2018/02/14 18:01:01 (permalink)
0

Converting an int to float without the libraries.

I need to convert numbers to IEEE 754 and from IEEE 754.  I do not need to do any floating point math.  Numbers are decimal fixed point ( ie 100.0 is stored as 1000) is anyone aware of an code available to do the conversions?  I am lazy and do not want to write it and debug it, if I do not have to.  And bringing in the Floating point library just for this seems a waste. 
#1

4 Replies Related Threads

    Michael.W.Mann
    Super Member
    • Total Posts : 291
    • Reward points : 0
    • Joined: 2011/01/24 09:58:24
    • Location: Chandler, Arizona
    • Status: offline
    Re: Converting an int to float without the libraries. 2018/02/14 20:38:05 (permalink)
    5 (1)
    From what you've written so far I'm guessing that:
    1) Your code has an integer that you need to convert to a float by Xfloat = Xint/10.0
    2) You don't intend to do anything with the float other than dump it out as a binary result in some data stream.
    3) You don't see the need for doing any floating point math in your code so that you don't bring in the floating point math library.
     
    A Google reveals "Converting floating-point number to IEEE754 representation by using union and struct in c."
    at https://gist.github.com/khajavi/5667960 .  This provides a Union that unpacks the single precision IEEE754 format, however, you have to realize that the fields are packed in a special way.  For example, the mantissa represents a binary point fraction with an implied integer bit that is always one.

    Michael W. Mann
    Principal Applications Engineer
    MCU32 Applications, Microchip
    #2
    NorthGuy
    Super Member
    • Total Posts : 4931
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Converting an int to float without the libraries. 2018/02/14 21:45:30 (permalink)
    0
    Not very hard to do. The floating point format is binary, not decimal, so you need to divide by 10 first, which is easy to do through multiplying by a coefficient, say by 1024*0.1 = 102. Then you keep shifting left until bit 24 becomes one, adjusting the exponent by one for every shift. Then you clear bit 24 and pack the exponent into bits 23-30.
     
    You can steal the code from any floating point emulation library. You do not need the whole library for that.
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 16032
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Converting an int to float without the libraries. 2018/02/14 22:48:38 (permalink)
    0
    Micheal yup pretty much that.
    Well I would like to keep the digits behind the decimal point. So I would need to adjust the exponent. ( I hope)
    I will check the xc32 and XC8 libraries. I think XC8 has a lot of C code.
    #4
    Larry.Standage
    Super Member
    • Total Posts : 823
    • Reward points : 0
    • Joined: 2011/12/30 09:50:47
    • Location: 0
    • Status: offline
    Re: Converting an int to float without the libraries. 2018/02/15 11:22:44 (permalink)
    0
    Are you using MZ EF? You could run a small assembly language snippet to get the integer into the FPU registers, then call the cvt.s.w or cvt.d.w to get the floating-point value, then copy them out.
    #5
    Jump to:
    © 2018 APG vNext Commercial Version 4.5