# Rounding Problem

**URL:** <https://forums.developer.nvidia.com/t/rounding-problem/41736>\
**Category:** CUDA Programming and Performance\
**Created:** [March 7, 2016, 4:01pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736 "2016-03-07T16:01:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mungio](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mungio/32/10715_2.png) [@Mungio](https://forums.developer.nvidia.com/u/Mungio)\
**Post date:** [March 7, 2016, 4:01pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/1 "2016-03-07T16:01:27Z")

</div>

Hi, I have a problem with some arithmetic operations on CUDA.

I have two int a and b

a = -2596  
b = 4096

when I perform m = a/b I have an error on the result. m is also an int.

The real result is -0,6337890625

on the CPU the result is -1

on GPU the result is 0.

Why this difference between CPU and GPU? how I can modify the rounding mode on GPU?

Thank you

---

<div class="post-metadata">

**Author:** ![Mungio](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mungio/32/10715_2.png) [@Mungio](https://forums.developer.nvidia.com/u/Mungio)\
**Post date:** [March 7, 2016, 4:22pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/2 "2016-03-07T16:22:30Z")

</div>

I think that the problem is in the cast function. Because the CPU when performs a cast from float to int, it performs also a rounding, while the GPU simply truncate the result.

---

<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:** [March 7, 2016, 4:22pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/3 "2016-03-07T16:22:45Z")

</div>

Could you post minimal, complete (buildable and runnable) code please that reproduces the problem? I’d be happy to take a look.

Integer arithmetic in CUDA follows standard C/C++ specifications and there should be no result differences to CPU computation unless undefined behavior is invoked, which should not be the case here. Note that there is no rounding mode in integer computations, that is a concept that applies to floating-point computation. According to you original post, ‘a’, ‘b’, and ‘m’ are all of type ‘int’, so no type conversions take place.

What version of CUDA are you using? What is your exact nvcc command line? If you are currently doing a debug build try switching to release build, if you are currently doing a release build, try switching to a debug build. Does the observed behavior change?

---

<div class="post-metadata">

**Author:** ![Jimmy\_Pettersson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jimmy\_Pettersson](https://forums.developer.nvidia.com/u/Jimmy_Pettersson)\
**Post date:** [March 7, 2016, 4:36pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/4 "2016-03-07T16:36:07Z")

</div>

> [@](#):
>
> Hi, I have a problem with some arithmetic operations on CUDA.
> 
> I have two **int** _a_ and _b_
> 
> _a_ = -2596  
> _b_ = 4096
> 
> when I perform _m = a/b_ I have an error on the result. _m_ is also an **int**.
> 
> The real result is **-0,6337890625**
> 
> on the CPU the result is **-1**
> 
> on GPU the result is **0**.
> 
> Why this difference between CPU and GPU? how I can modify the rounding mode on GPU?
> 
> Thank you

The correct result of -2596/4096 is 0 (ZERO).

The correct result of casting -0.633… to int is 0 (ZERO).

Am I missing something here?

---

<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:** [March 7, 2016, 4:54pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/5 "2016-03-07T16:54:07Z")

</div>

> [@](#):
>
> Am I missing something here?

I was thinking the same thing, which is why I asked for the OP to post the relevant code. I suspect there are actually differences between CPU and GPU portions of the code, which then leads to the differences in the results. But that is pure conjecture at this point. It is also possible that OP inadvertently reversed the values of ‘a’ and ‘b’ in their post.

---

<div class="post-metadata">

**Author:** ![Mungio](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mungio/32/10715_2.png) [@Mungio](https://forums.developer.nvidia.com/u/Mungio)\
**Post date:** [March 7, 2016, 5:30pm UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/6 "2016-03-07T17:30:12Z")

</div>

I resolved using \_\_float2int\_rn() function.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Mungio](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/mungio/32/10715_2.png) [@Mungio](https://forums.developer.nvidia.com/u/Mungio)\
**Post date:** [March 8, 2016, 8:27am UTC](https://forums.developer.nvidia.com/t/rounding-problem/41736/7 "2016-03-08T08:27:30Z")

</div>

> [@](#):
>
> The correct result of -2596/4096 is 0 (ZERO).
> 
> The correct result of casting -0.633… to int is 0 (ZERO).
> 
> Am I missing something here?

This is correct. The problem was an AVX intrinsic that perform the division. A normal C code return

_a/b =_ **0**

Sorry for the inconvenience
