-1
chars(test value) = 1124000727777607680000 , 25852016738884976640000 , 620448401733239439360000

This is part of my code....

char* chars = (char*)malloc(capacity_c + 1);

long double* numbers = malloc((sizeof(long double) *capacity_l + 1));

printf("string = %s\n",chars);

long double d = 0;

sscanf_s(chars, "%Lf", &d);

printf("%Lf \n", d);

string = 1124000727777607680000
     d = 1124000727777607680000.000000   --> correct value input numbers array

string = 25852016738884976640000
     d = 25852016738884978212864.000000  --> it was changed the value.....

string = 620448401733239439360000
     d = 620448401733239409999872.000000 --> it was changed the value.....
bruno
  • 32,421
  • 7
  • 25
  • 37
  • Correct duplicates: [Unsigned 64 bit integers which cannot map onto a double](https://stackoverflow.com/q/17782425/995714), [Are all integer values perfectly represented as doubles?](https://stackoverflow.com/q/43655668/995714) – phuclv Feb 24 '19 at 00:16
  • @phuclv it is a long double, yet we don't know what is the exact type here. – Antti Haapala -- Слава Україні Feb 24 '19 at 08:53
  • @AnttiHaapala `long double` may be the same as `double`, for example on platforms without an extended-precision floating-point type or on MSVC. But yeah, the OP needs to clarify which compiler is being used – phuclv Feb 24 '19 at 12:42

1 Answers1

1

Doubles do not have enough precision to exactly represent the value, and so the number gets changed to the closest value representable by a long double. For storing exact values of large integers, you should probably not use floating point numbers. Instead, use an integer value large enough for all your uses. If your numbers are unbounded, then use a dynamically allocated integer class that can keep adding bits (like Python does for long integers).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Leo Adberg
  • 342
  • 2
  • 18