# Rounding Problem

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

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.

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?

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?

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.

I resolved using __float2int_rn() function.

Thanks a lot.

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