i'm having problems with large float numbers. I'm taking the l2-norm of some vectors and having problems when working with large point values. For example, consider vec as a vector:
float vec[] = { 10001.000000, 10002.000000, 10000.000000, 10003.000000,
10003.000000, 10002.000000, 10003.000000 };
float sumzz = 0;
for (int i = 0; i < 7; i++) {
sumzz += pow(vec[i], 2);
}
The output is '700280064', and it's wrong because the correct value is '700280036'.
So i tryed some stuff and i found that when i cast some large value to float it loses precision. Another example:
long num = 5502160332;
printf("%ld\n", num);
printf("%f\n", (float) num);
The output for the first print is clearly 5502160332, while the second is 5502160384. Am i doing something wrong? Is there a solution about this?
EDIT: as I mentioned in a comment, the problem is that i should use as less double values as it's possibile, because i'm working with CUDA and except for Tesla or high-end Quadro cards, double values have 1/32 efficiency compared to float
or other types.