# Floating Point to Integer Representation

**URL:** <https://forums.developer.nvidia.com/t/floating-point-to-integer-representation/20223>\
**Category:** CUDA Programming and Performance\
**Created:** [December 15, 2010, 6:30pm UTC](https://forums.developer.nvidia.com/t/floating-point-to-integer-representation/20223 "2010-12-15T18:30:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmyablonski](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dmyablonski](https://forums.developer.nvidia.com/u/dmyablonski)\
**Post date:** [December 15, 2010, 6:30pm UTC](https://forums.developer.nvidia.com/t/floating-point-to-integer-representation/20223/1 "2010-12-15T18:30:59Z")

</div>

I’m simply trying to do some floating point subtractions, then interpret the result as an integer (not casting, simply accessing it as an integer to see the actual bit value).

I’ve tried using a union, doesn’t seem to work. I end up with a 0 integer value (no bits set).

I have since tried to do it with pointer tricks. I’m seeing weird behavior.

result is an unsigned integer array. This is code from the kernel…

```auto
unsigned int start=0;

float my1, my2;

my1=*( (float*) &start );

my2= *( (float*) &tid );

result[tid] = *( (int*) &my2 );

```

The result in result array is:

```
    0, 1, 2, 3, 4, 5, 6 

```

Which is as expected.

BUT,

```auto
unsigned int start = 0;

float my1, my2;

float fresult;

my1=*( (float*) &start );

my2= *( (float*) &tid );

fresult=my1-my2;

result[tid] = *( (int*) &fresult );

```

Gives me all 0’s as a result. I expect to see something like

```
   0 0x80000001 0x80000002, etc

```

which is what I see if I do a similar kind of calculations on the CPU using union datatypes.

Any ideas?

---

<div class="post-metadata">

**Author:** ![dmyablonski](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dmyablonski](https://forums.developer.nvidia.com/u/dmyablonski)\
**Post date:** [December 15, 2010, 6:44pm UTC](https://forums.developer.nvidia.com/t/floating-point-to-integer-representation/20223/2 "2010-12-15T18:44:03Z")

</div>

Looks like it’s because of denormalized floating point being flushed to 0 on pre-fermi devices… Guess I understand now.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [December 15, 2010, 6:49pm UTC](https://forums.developer.nvidia.com/t/floating-point-to-integer-representation/20223/3 "2010-12-15T18:49:58Z")

</div>

CUDA offers dedicated device functions for re-interpreting a float as an int and vice-versa, and I would recommend using those:

```auto
int i;

float f;

i = __float_as_int(f);

f = __int_as_float(i);

```

Alternatively, reinterpret\_cast (as known from C++) can be used. I find that a bit more cumbersome to use, especially when the re-interpretation is used within an expression.
