Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

not that simple, 1/3 + 5 - 1/3 should be 5. It doesn't overflow in IEEE754


That does appear to equal exactly 5... would you care to show how it doesn't?

    $ cat check_math.c
    #include <stdio.h>

    int main() {
        // Define the values as float (32-bit floating point)
        float one_third = 1.0f / 3.0f;
        float five = 5.0f;

        // Compute the equation
        float result = one_third + five - one_third;

        // Check for exact equality
        if (result == five) {
            printf("The equation evaluates EXACTLY to 5.0 (True)\n");
        } else {
            // Print the actual result and the difference
            printf("The equation does NOT evaluate exactly to 5.0 (False)\n");
            printf("Computed result: %.10f\n", result);
            printf("Difference: %.10f\n", result - five);
        }

        return 0;
    }

    $ gcc -O0 check_math.c -o check_math; ./check_math
    The equation evaluates EXACTLY to 5.0 (True)


Okay..that was just an example (and a false one apparently).

Its easy enough to find an example where your typical FP operations doesn't work out.

https://godbolt.org/z/Mr4Ez8xz1




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: