I try to convert some numbers to string using snprintf. The name1 should have the same digits after comma as name2.
#include <stdio.h>
#define length 50
int main()
{
char name1 [length];
char name2 [length];
double step= 0.00001;
unsigned long long int iterMax =100000000000;
int k;
for (k = 0; k <= 20; k++)
{ printf("numbers : k = %2d ; k*step = %f ;", k, k*step);
snprintf(name1,length+1,"%f", iterMax+k*step); /* */
snprintf(name2,length+1, " %f", k*step); /* */
printf("strings : k*step = %s ; iterMax+k*step = %s \n",name2, name1);
}
return 0;
}
Compile it with :
gcc t.c -Wall
Output is :
./a.out
numbers : k = 0 ; k*step = 0.000000 ;strings : k*step = 0.000000 ; iterMax+k*step = 100000000000.000000
numbers : k = 1 ; k*step = 0.000010 ;strings : k*step = 0.000010 ; iterMax+k*step = 100000000000.000015
numbers : k = 2 ; k*step = 0.000020 ;strings : k*step = 0.000020 ; iterMax+k*step = 100000000000.000015
numbers : k = 3 ; k*step = 0.000030 ;strings : k*step = 0.000030 ; iterMax+k*step = 100000000000.000031
numbers : k = 4 ; k*step = 0.000040 ;strings : k*step = 0.000040 ; iterMax+k*step = 100000000000.000046
The results are the same ( digits aftter comma ) when iterMax is smaller , for example 100000000 :
numbers : k = 0 ; k*step = 0.000000 ;strings : k*step = 0.000000 ; iterMax+k*step = 100000000.000000
numbers : k = 1 ; k*step = 0.000010 ;strings : k*step = 0.000010 ; iterMax+k*step = 100000000.000010
numbers : k = 2 ; k*step = 0.000020 ;strings : k*step = 0.000020 ; iterMax+k*step = 100000000.000020
numbers : k = 3 ; k*step = 0.000030 ;strings : k*step = 0.000030 ; iterMax+k*step = 100000000.000030
numbers : k = 4 ; k*step = 0.000040 ;strings : k*step = 0.000040 ; iterMax+k*step = 100000000.000040
The ULLONG_MAX = 18446744073709551615 is greater then iterMax.
How can I resolve that ?
TIA