# Getting strange result in simple multiplication 

**URL:** <https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306>\
**Category:** CUDA Programming and Performance\
**Created:** [November 22, 2016, 4:31pm UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306 "2016-11-22T16:31:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhijit\_Ghosh](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/abhijit_ghosh/32/56084_2.png) [@Abhijit\_Ghosh](https://forums.developer.nvidia.com/u/Abhijit_Ghosh)\
**Post date:** [November 22, 2016, 4:31pm UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/1 "2016-11-22T16:31:32Z")

</div>

I have define M = 250 and N = 250 and try to compute int bigsize = (M-1)_(N-1)_(M-1)_(N-1); but getting strange result in console ,like  
M-1 = 249 N-1 = 249 bigsize = -450843295  
What is the problem here.  
when I multiply (M-1)_(N-1)\*(M-1), I get 15438249, which is correct.  
But when I include math.h and calculate int bigsize = pow(N-1, 2)\*pow(M-1, 2), I am getting bigsize = 2147483647  
but it should be 3844124001.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 22, 2016, 4:34pm UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/2 "2016-11-22T16:34:34Z")

</div>

Google “integer overflow”.

---

<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:** [November 22, 2016, 5:46pm UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/3 "2016-11-22T17:46:13Z")

</div>

For M=250 and N=250, the expression (M-1)_(N-1)_(M-1)\*(N-1) mathematically results in a value between 2 **31 and 2** 32, which, however, is too large to be representable in a 32-bit _signed_ ‘int’. The result of signed integer overflow is _undefined_, it could be anything.

Here you could get by using an unsigned ‘int’:

```auto
#define M (250U)
#define N (250U)
unsigned int bigsize = (M-1)*(N-1)*(M-1)*(N-1);

```

For a general case, it would be best to perform such computations with ‘size\_t’ operands, as you could easily overflow the range of 32-bit unsigned integers as well. Side remark: You would never want to invoke pow() to simply square operands.

---

<div class="post-metadata">

**Author:** ![Abhijit\_Ghosh](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/abhijit_ghosh/32/56084_2.png) [@Abhijit\_Ghosh](https://forums.developer.nvidia.com/u/Abhijit_Ghosh)\
**Post date:** [November 23, 2016, 9:53am UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/4 "2016-11-23T09:53:00Z")

</div>

But  
unsigned int linsizeA2 = (N-1)_(M-1)_(N-1)_(M-1);  
checkCuda(cudaMalloc((void\*\*) &A2l, linsizeA2_sizeof(double)));

giving error like that  
$ cuda-memcheck ./hesttry2 |more  
GPUassert: out of memory hestry2.cu 931

free memory = 11926175744 total memory = 12079136768

# ========= CUDA-MEMCHECK ========= Program hit cudaErrorMemoryAllocation (error 2) due to “out of memory” on CUDA API call to cudaMalloc. ========= Saved host backtrace up to driver entry point at error ========= Host Frame:/usr/lib64/libcuda.so.1 [0x2e40d3] ========= Host Frame:./hesttry2 [0x43549] ========= Host Frame:./hesttry2 [0x52f9] ========= Host Frame:/lib64/libc.so.6 (\_\_libc\_start\_main + 0xfd) [0x1ed1d] ========= Host Frame:./hesttry2 [0x25b9]

========= ERROR SUMMARY: 1 error

I have tried with size\_t instead of unsigned int but getting same result

Now 11926175744/((N-1)_(M-1)_(N-1)\*(M-1)) = 3.10 but double variable would take 8 byte, is this the reason or am I making some another mistake?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [November 23, 2016, 10:02am UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/5 "2016-11-23T10:02:32Z")

</div>

You’re out of memory.

249_249_249_249_8 = 30752992008

That number is a little over 30GB

Probably your GPU does not have 30GB of memory.

Also note that you still haven’t handled the calculation correctly. If your final result is 30GB (i.e. larger than 2^32) you should use a 64-bit type such as size\_t (on a 64-bit platform) instead of this:

linsizeA2\*sizeof(double)

But this wouldn’t “resolve” your issue because the request to allocate 30GB of memory is going to fail on any currently available CUDA GPU that I am aware of.

---

<div class="post-metadata">

**Author:** ![Abhijit\_Ghosh](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/abhijit_ghosh/32/56084_2.png) [@Abhijit\_Ghosh](https://forums.developer.nvidia.com/u/Abhijit_Ghosh)\
**Post date:** [November 23, 2016, 11:58am UTC](https://forums.developer.nvidia.com/t/getting-strange-result-in-simple-multiplication/46306/6 "2016-11-23T11:58:29Z")

</div>

Thank you very much for help.
