# Changing from float to double generate wrong result Help needed, I am frustrated

**URL:** <https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865>\
**Category:** CUDA Programming and Performance\
**Created:** [August 30, 2009, 4:30pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865 "2009-08-30T16:30:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![laplaceme](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@laplaceme](https://forums.developer.nvidia.com/u/laplaceme)\
**Post date:** [August 30, 2009, 4:30pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/1 "2009-08-30T16:30:39Z")

</div>

It’s not caused by conversion since I change all the variables together with the constants and there’s no conversion from integer to float/double. And I am sure it’s not because of the small difference made from functions like sin() or pow(). The wired thing is if I compile with the debug option (-G) or emulation mode, it gives correct result. It’s beyond my knowledge. Anyone can point me what kind of thing may cause this problem? Thx.

I am using CUDA 2.3 with driver 190.18 under Ubuntu 9.04 64bit. The card is a 295GTX.

I am wondering if it might be related to the bug found in the driver. It happened to me once that even cudaMalloc can’t run and reboot bring it back to normal.

[topic=“97379”]cuda 2.2 bug?[/topic]

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [August 30, 2009, 4:47pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/2 "2009-08-30T16:47:21Z")

</div>

How exactly is the result “wrong”?

---

<div class="post-metadata">

**Author:** ![laplaceme](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@laplaceme](https://forums.developer.nvidia.com/u/laplaceme)\
**Post date:** [August 30, 2009, 5:19pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/3 "2009-08-30T17:19:13Z")

</div>

Some thing like:

F\_dd[i]=F\_dd[i]+Fn+Fs; gives 291.0 when using nvcc -G or float variables compare to 500.4 when using nvcc and double.

Although I can identify which part of the code goes wrong ( the above line), it’s very unlikely the line itself cause the problem since I’ve using it for a long time on CPU version.

F\_dd is defined as:

VecR \*F\_dd

cudaMalloc((void \*_) &F\_d,3_(N+1)\*sizeof(double));

and Fn Fs is also defined as VecR inside **global** function

Where VecR is define like:

**host**  **device** class VecR

{

public:

```
    double x;

    double y;

    double z;

```

**host**  **device** VecR operator +(VecR vb)

```
    {

    VecR result;

    result.x= x+vb.x;

    result.y= y+vb.y;

    result.z= z+vb.z;

    return result;

    }

```

}

Thanks for reply.

> [@](#):
>
> How exactly is the result “wrong”?

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [August 30, 2009, 6:04pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/4 "2009-08-30T18:04:30Z")

</div>

are you compiling with -arch sm\_13?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Collange](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Sylvain\_Collange](https://forums.developer.nvidia.com/u/Sylvain_Collange)\
**Post date:** [August 30, 2009, 6:20pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/5 "2009-08-30T18:20:21Z")

</div>

> [@](#):
>
> It’s not caused by conversion since I change all the variables together with the constants and there’s no conversion from integer to float/double. And I am sure it’s not because of the small difference made from functions like sin() or pow(). The wired thing is if I compile with the debug option (-G) or emulation mode, it gives correct result. It’s beyond my knowledge. Anyone can point me what kind of thing may cause this problem? Thx.

Your problem seems close to this one:

[topic=“103726”]different output when compiled for emulation, device, and device with -g -G[/topic]

So you can try the proposed workaround (-Xopencc -O0) and see if it helps…

(maybe I should bet something real this time, like a beer ;))

---

<div class="post-metadata">

**Author:** ![laplaceme](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@laplaceme](https://forums.developer.nvidia.com/u/laplaceme)\
**Post date:** [August 30, 2009, 6:44pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/6 "2009-08-30T18:44:19Z")

</div>

Yes.

> [@](#):
>
> are you compiling with -arch sm\_13?

---

<div class="post-metadata">

**Author:** ![laplaceme](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@laplaceme](https://forums.developer.nvidia.com/u/laplaceme)\
**Post date:** [August 30, 2009, 7:08pm UTC](https://forums.developer.nvidia.com/t/changing-from-float-to-double-generate-wrong-result-help-needed-i-am-frustrated/11865/7 "2009-08-30T19:08:03Z")

</div>

It works! I really appreciate. I own you a beer.

And thanks for all you guys.

> [@](#):
>
> Your problem seems close to this one:
> 
> [topic=“103726”]different output when compiled for emulation, device, and device with -g -G[/topic]
> 
> So you can try the proposed workaround (-Xopencc -O0) and see if it helps…
> 
> (maybe I should bet something real this time, like a beer ;))
