# Type Conversion? convert ints to floats

**URL:** <https://forums.developer.nvidia.com/t/type-conversion-convert-ints-to-floats/3087>\
**Category:** CUDA Programming and Performance\
**Created:** [March 21, 2008, 3:00pm UTC](https://forums.developer.nvidia.com/t/type-conversion-convert-ints-to-floats/3087 "2008-03-21T15:00:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charley](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Charley](https://forums.developer.nvidia.com/u/Charley)\
**Post date:** [March 21, 2008, 3:00pm UTC](https://forums.developer.nvidia.com/t/type-conversion-convert-ints-to-floats/3087/1 "2008-03-21T15:00:54Z")

</div>

I have a simple kernel that converts an integer array to a float array:  
**global** void  
IntToReal( float\* fsig,  
short\* isig,  
unsigned int max)  
{  
unsigned int id = threadIdx.x+blockIdx.x\*blockDim.x;

if (id \< max)  
{  
fsig[id] = (float)isig[id];  
}  
} // end

It works fine, but I found this \__int2float_ in the programming manual and tried this:  
IntToReal( float\* fsig,  
short\* isig,  
unsigned int max)  
{  
unsigned int id = threadIdx.x+blockIdx.x\*blockDim.x;

if (id \< max)  
{  
fsig[id] = \_\_int2float\_rz(isig[id]);  
}  
} // end  
which also works. Why would I need \__int2float_? Is it better to use it? I get same results, and there doesn’t appear to be a difference in speed.

Thanks –

---

<div class="post-metadata">

**Author:** ![emerth](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/emerth/32/19316_2.png) [@emerth](https://forums.developer.nvidia.com/u/emerth)\
**Post date:** [March 24, 2008, 12:41am UTC](https://forums.developer.nvidia.com/t/type-conversion-convert-ints-to-floats/3087/2 "2008-03-24T00:41:50Z")

</div>

> [@](#):
>
> I have a simple kernel that converts an integer array to a float array:
> 
> **global** void
> 
> IntToReal( float\* fsig,
> 
> short\* isig,
> 
> unsigned int max)
> 
> {
> 
> unsigned int id = threadIdx.x+blockIdx.x\*blockDim.x;
> 
> if (id \< max)
> 
> {
> 
> fsig[id] = (float)isig[id];
> 
> }
> 
> } // end
> 
> It works fine, but I found this \__int2float_ in the programming manual and tried this:
> 
> IntToReal( float\* fsig,
> 
> short\* isig,
> 
> unsigned int max)
> 
> {
> 
> unsigned int id = threadIdx.x+blockIdx.x\*blockDim.x;
> 
> if (id \< max)
> 
> {
> 
> fsig[id] = \_\_int2float\_rz(isig[id]);
> 
> }
> 
> } // end
> 
> which also works.&nbsp; Why would I need \__int2float_? Is it better to use it?&nbsp; I get same results, and there doesn’t appear to be a difference in speed.
> 
> Thanks –
> 
> [snapback]347791[/snapback]

The C - style cast may not round to the same value depending on which compiler you use.

The CUDA function \_\_int2float\_rz() is defined to behave in a particular way. If you want round-to-zero then use the CUDA function \_\_int2float\_rz().

That you use nvcc for all kernels is not the point: it’s more general and a philosophical issue. If you use, say, GCC for some CPU side code and you care about how rounding behaves then you use GCC options to control rounding behaviour. You should say what you mean in the code, do not count on the compiler to DWIM.

---

<div class="post-metadata">

**Author:** ![player999](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@player999](https://forums.developer.nvidia.com/u/player999)\
**Post date:** [November 13, 2011, 11:19pm UTC](https://forums.developer.nvidia.com/t/type-conversion-convert-ints-to-floats/3087/3 "2011-11-13T23:19:27Z")

</div>

Sorry wrong topic  
Moderator may delete this message
