Inconsistency in C double precision comparison

Asterisks are expected beside each line with a 0.50 in the last column given below using the following simple C code with the greater than or equivalent to comparisons. Only 2 found out of the 6 expected using the same calculation for all. Adding 1.0e-16 or more solves the problem but that doesn't conform to ANSI standard. Precision problem? Mac mini with latest OS.

#include <stdio.h>

#include <math.h>

int main(  ){

    int     i,j;

double  x,y,delta;

    double  sum;

         x=0.0;

    delta = 0.1;

    for(i=0;i<11;i++)

    {

        y=0.0;

        for(j=0;j<11;j++)

        {

            sum = x+y;

            printf("x= %7.2f  y= %7.2f  sum= %7.2f  ",x,y,sum);

            if( sum == 0.5 )printf("*******");

            printf("\n");

            y=y-delta;

        }

        printf("\n");

        x=x+delta;

    }     return 0;

}

Results(abbreviated)

x=    0.50  y=    0.00  sum=    0.50  *******

x=    0.50  y=   -0.10  sum=    0.40  

x=    0.50  y=   -0.20  sum=    0.30  

x=    0.50  y=   -0.30  sum=    0.20  

x=    0.50  y=   -0.40  sum=    0.10  

x=    0.50  y=   -0.50  sum=    0.00  

x=    0.50  y=   -0.60  sum=   -0.10  

x=    0.50  y=   -0.70  sum=   -0.20  

x=    0.50  y=   -0.80  sum=   -0.30  

x=    0.50  y=   -0.90  sum=   -0.40  

x=    0.50  y=   -1.00  sum=   -0.50  

x=    0.60  y=    0.00  sum=    0.60  

x=    0.60  y=   -0.10  sum=    0.50  *******

x=    0.60  y=   -0.20  sum=    0.40  

x=    0.60  y=   -0.30  sum=    0.30  

x=    0.60  y=   -0.40  sum=    0.20  

x=    0.60  y=   -0.50  sum=    0.10  

x=    0.60  y=   -0.60  sum=    0.00  

x=    0.60  y=   -0.70  sum=   -0.10  

x=    0.60  y=   -0.80  sum=   -0.20  

x=    0.60  y=   -0.90  sum=   -0.30  

x=    0.60  y=   -1.00  sum=   -0.40  

x=    0.70  y=    0.00  sum=    0.70  

x=    0.70  y=   -0.10  sum=    0.60  

x=    0.70  y=   -0.20  sum=    0.50  expected here!

x=    0.70  y=   -0.30  sum=    0.40  

x=    0.70  y=   -0.40  sum=    0.30  

x=    0.70  y=   -0.50  sum=    0.20  

x=    0.70  y=   -0.60  sum=    0.10  

x=    0.70  y=   -0.70  sum=    0.00  

x=    0.70  y=   -0.80  sum=   -0.10  

x=    0.70  y=   -0.90  sum=   -0.20  

x=    0.70  y=   -1.00  sum=   -0.30  

x=    0.80  y=    0.00  sum=    0.80  

x=    0.80  y=   -0.10  sum=    0.70  

x=    0.80  y=   -0.20  sum=    0.60  

x=    0.80  y=   -0.30  sum=    0.50  expected here!

x=    0.80  y=   -0.40  sum=    0.40  

x=    0.80  y=   -0.50  sum=    0.30  

x=    0.80  y=   -0.60  sum=    0.20  

x=    0.80  y=   -0.70  sum=    0.10  

x=    0.80  y=   -0.80  sum=    0.00  

x=    0.80  y=   -0.90  sum=   -0.10  

x=    0.80  y=   -1.00  sum=   -0.20  

x=    0.90  y=    0.00  sum=    0.90  

x=    0.90  y=   -0.10  sum=    0.80  

x=    0.90  y=   -0.20  sum=    0.70  

x=    0.90  y=   -0.30  sum=    0.60  

x=    0.90  y=   -0.40  sum=    0.50  expected here!

x=    0.90  y=   -0.50  sum=    0.40  

x=    0.90  y=   -0.60  sum=    0.30  

x=    0.90  y=   -0.70  sum=    0.20  

x=    0.90  y=   -0.80  sum=    0.10  

x=    0.90  y=   -0.90  sum=    0.00  

x=    0.90  y=   -1.00  sum=   -0.10  

x=    1.00  y=    0.00  sum=    1.00  

x=    1.00  y=   -0.10  sum=    0.90  

x=    1.00  y=   -0.20  sum=    0.80  

x=    1.00  y=   -0.30  sum=    0.70  

x=    1.00  y=   -0.40  sum=    0.60  

x=    1.00  y=   -0.50  sum=    0.50  expected here!

x=    1.00  y=   -0.60  sum=    0.40  

x=    1.00  y=   -0.70  sum=    0.30  

x=    1.00  y=   -0.80  sum=    0.20  

x=    1.00  y=   -0.90  sum=    0.10  

x=    1.00  y=   -1.00  sum=    0.00  

Duplicate post is considered to be a bad manner, you should better care about it. And your question has nothing to do with the WWDC21 session wwdc21-10002, you should use the right tag. And the problem you have experienced, happens because C-double is a binary floating point number type and which cannot represent the value 0.1 precisely. It is the common behavior on all platforms which uses binary floating point, not only on Apple's platform. So, asking it in the Apple's dev forums is not appropriate. You should better find a good site to learn numeric processing.

Inconsistency in C double precision comparison
 
 
Q