# CUDA type casting unexpected behaviour

**URL:** <https://forums.developer.nvidia.com/t/cuda-type-casting-unexpected-behaviour/28141>\
**Category:** CUDA Programming and Performance\
**Created:** [February 2, 2013, 5:46am UTC](https://forums.developer.nvidia.com/t/cuda-type-casting-unexpected-behaviour/28141 "2013-02-02T05:46:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mavericklsd](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mavericklsd/32/42645_2.png) [@Mavericklsd](https://forums.developer.nvidia.com/u/Mavericklsd)\
**Post date:** [February 2, 2013, 5:46am UTC](https://forums.developer.nvidia.com/t/cuda-type-casting-unexpected-behaviour/28141/1 "2013-02-02T05:46:13Z")

</div>

Hi, can anyone explain why the cuda type casting functions behave differently from standard C++ on CPU, for example:  
Cuda:  
0x86’?’ = unsigned char( 0x86 )  
C++ CPU:  
0x1f’\x1f’ = unsigned char( 0x86 )  
and CUDA int() and CUDA float() behave differently from CPU int() and float() too. Why is that?  
Moreover, I notice that there is a small difference even between \_float2int\_rd() and CPU int(), too.

---

<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:** [February 2, 2013, 8:56am UTC](https://forums.developer.nvidia.com/t/cuda-type-casting-unexpected-behaviour/28141/2 "2013-02-02T08:56:22Z")

</div>

\_\_float2int\_rd() is not the same as regular float → int conversion. The C and C++ standards specify that float → int conversion truncates, so the equivalent intrinsic would be \_\_float2int\_rz(). The suffix “\_rd” means “round down” / “round towards negative infinity”, while “\_rz” means “round towards zero”.

I don’t understand your first question. Could you post a compilable example code that demonstrates the difference(s) you are concerned about?

---

<div class="post-metadata">

**Author:** ![Mavericklsd](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mavericklsd/32/42645_2.png) [@Mavericklsd](https://forums.developer.nvidia.com/u/Mavericklsd)\
**Post date:** [February 2, 2013, 10:09am UTC](https://forums.developer.nvidia.com/t/cuda-type-casting-unexpected-behaviour/28141/3 "2013-02-02T10:09:17Z")

</div>

Sorry it’s garbage. I made a stupid mistake. the type casting function of CUDA c++ and standard c++ are exactly the same, and \_\_float2int\_rd() are round half down function which is unrelated to the topic.
