on-internet
← Community

How does C know the value of DBL_EPSILON in machines that do not follow the IEEE floating point convention

Stack Overflow ·

I was trying to solve the problem posed in this question where the author asks that the value of DBL_EPSILON be computed portably without using the macro. My solution : #include <stdio.h> int main(void) { double one = 1, next = 1, zero = -0.0; unsigned char * ptr_next = (unsigned char *)&next, * ptr_zero = (unsigned char *)&zero; int endianness; if (ptr_zero[7] >> 7) endianness = 0; if (ptr_zero[0] >> 7) endianness = 7; ptr_next[endianness] |= 1; printf("DBL_EPSILON = %e", (next - one)); return 0; } I determined the endianness of the machine by finding out at what position the 1 bit of -0.0 is

Comments

Sign in or sign up to comment and upvote.

No comments yet.