1

How do I convert an int to a float (returned as unsigned, but interpreted as a float)? I can't use any high level language constructs like unions, only bitwise ops, control logic (if/while), + and -, etc.

Mat
  • 202,337
  • 40
  • 393
  • 406
Trevor Hill
  • 57
  • 1
  • 1
  • 2
  • 8
    Is this a homework? What have you tried so far? – Levon Sep 10 '12 at 01:29
  • How is this constructed "float" value to be accessed? –  Sep 10 '12 at 01:35
  • You Google the IEEE floating point format standard, and figure out how to twiddle the bits to set your integer into the "fraction" and the appropriate value into the "exponent". – Hot Licks Sep 10 '12 at 01:37
  • @EdS. -- Though TPTB in meta are deprecating the "homework" tag. – Hot Licks Sep 10 '12 at 01:38
  • This is (at least) the fourth question in the last 48 hours on this general topic. I think the others have been float to int and this is int to float, but the family likeness is remarkable. [SO 12342926](http://stackoverflow.com/questions/12342926/); [SO 12336675](http://stackoverflow.com/questions/12336675/); [SO 12336314](http://stackoverflow.com/questions/12336314/). – Jonathan Leffler Sep 10 '12 at 01:41
  • 1
    @HotLicks: All I could find was [this thread](http://meta.stackexchange.com/questions/60422/is-homework-an-exception-to-the-no-meta-tag-rule/60495#60495), in which Jeff defends this particular meta-tag. Do you have a more recent link? I honestly hope they don't as it makes it clear that one should not just hand over a complete answer. Oh, and I don't know what "TPTB" stands for :D – Ed S. Sep 10 '12 at 01:44
  • Hopefully I didn't give away too much with that answer =) – D.Shawley Sep 10 '12 at 01:53
  • @EdS: The Powers That Be (TPTB). – Ken White Sep 10 '12 at 01:57
  • @EdS. See http://meta.stackexchange.com/q/123758/172880 – Hot Licks Sep 10 '12 at 11:51
  • If you only want to stash the bits away, use a `union {int i; float f;}` to reinterpret the `int` as `float` and viceversa. Be careful, AFAIR C doesn't guarantee they are the same (some architectures have extra type bits, or do other funky stuff here). – vonbrand Jan 20 '13 at 17:35

2 Answers2

5

Sure. You can certainly construct a floating point number from a raw byte buffer. Single precision floats are represented as 32-bit values in IEEE-754 and can represent the integer space quite easily. The wikipedia entry on IEEE floating point describes the various formats. All that you have to do is figure out which bits to set or test. Look at the wikipedia page on single-precision for a nice description of the bits. Here's a start:

float convert_int(unsigned int num) {
    float result;
    unsigned int sign_bit = (num & 0x80000000);
    unsigned int exponent = (num & 0x7F800000) >> 23;
    unsigned int mantissa = (num & /* insert mask value here */);

    /* you can take over from here */

    return result;
}

I left a lot up to the imagination, but I have a feeling that this is a homework problem. Once you understand the part that I have written, it should be easy enough to do the rest. A much nicer solution is to use a union, but I will leave that as an exercise to the reader as well.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
-3

If you dont want to use any arithmetic or bitwise operators then you can use a method as

 float convrt(int x)
 {
  return x;
 }

It will return float... you can verify it in main as..

  void main()
 {
   int x=5;
   cout<<"size of x=" << sizeof(x);
   cout<<"size of convrt(x)=" << sizeof(convrt(x));
 }

it will give output as

    size of x=2
    size of convrt(x)=4

that means it gets converted into float. :-)