0

I am having a string pointer in C which is having bigint data.

eg 9223372036854775807 i.e 2^63

I wanted to cast this to double but as you know double has 15/16 digits available to store in fraction part the rest of the bits are discarded.so the above number which is very large would be casted to 9.22337203685476E+18 i.e 922337203685476000.

This makes comparing the original value and casted value mismatch. This usually happens on Linux platform. Thing is why this doesnot happen on Windows?

Is it compiler dependent or something which is unknown to me. ?

1 Answer 1

0

That value is 2^63 - 1, which cannot be exactly represented with a double. The closest value that can be represented is 2^63. And that's what you get if you use e.g. sscanf or atof:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    const char *str = "9223372036854775807";
    double d;
    double e;
    sscanf(str, "%lf", &d);
    e = atof(str);
    printf("%f\n", d);    // 9223372036854775808.000000
    printf("%f\n", e);    // 9223372036854775808.000000
}

See http://ideone.com/zz2NIF.

1
  • i agree that the number cant be represented by double but when then comparing it in windows is a success and in linux is a failure ? Apr 3, 2013 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.