# double to float conversion

**URL:** <https://forums.developer.nvidia.com/t/double-to-float-conversion/7419>\
**Category:** CUDA Programming and Performance\
**Created:** [January 27, 2009, 5:12am UTC](https://forums.developer.nvidia.com/t/double-to-float-conversion/7419 "2009-01-27T05:12:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sachin](https://forums.developer.nvidia.com/u/sachin)\
**Post date:** [January 27, 2009, 5:12am UTC](https://forums.developer.nvidia.com/t/double-to-float-conversion/7419/1 "2009-01-27T05:12:58Z")

</div>

hi everyone,

i am trying to implement a code such that it truncates a double number(/variable) to a float. but i cant seem to get it working. I have a strong feeling the fact that i only got a GPU that supports single precision (float only) has a lot to do with it.

[codebox]#include \<cuda.h\>

#include \<math.h\>

#include “mem\_trunc.h”

// globals

// contains information about maximum allowable sizes

static int max\_threads=0;

//function prototypes

**global** void mem\_dtf (double \*, int );

//condenses double storage to float

**global** void mem\_dtf (double \* array\_, int elements){ /_to be executed on device only_/

```
int i=blockIdx.x*blockDim.x+threadIdx.x;/*thread number*/

float *temp=NULL;

temp=(float *)array_;

if (i<elements)

	temp[i]=(float)array_[i];

```

}

void call\_mem\_dtf (void \*array\_, int elements){

```
dim3 dimBlock (max_threads,1);

dim3 dimGrid((int)ceil((float)elements/max_threads),1);

mem_dtf <<<dimBlock, dimGrid>>>((double *)array_, elements) ;

```

}

void set\_max\_thread (int threads){

```
max_threads=threads;

```

}[/codebox]

dw abt threads- that is set by me from a c function (main) and i dont think thats where the error is.

my question is how can i truncate a double to a float in a GPU that only supports single precision (compute capability 1.1)

any suggestions?

thanks

sachin

---

<div class="post-metadata">

**Author:** ![Simon\_Green](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Simon\_Green](https://forums.developer.nvidia.com/u/Simon_Green)\
**Post date:** [January 28, 2009, 1:08pm UTC](https://forums.developer.nvidia.com/t/double-to-float-conversion/7419/2 "2009-01-28T13:08:38Z")

</div>

Since your hardware doesn’t understand double precision, there’s no way it can convert from double to float in a kernel. The only way to do this would be to cast the data to a different 64-bit type like uint2 and then extract the bits using bit arithmetic.
