Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two calculations give different answers?

#include <stdio.h>

#define pi1 3.141

const float pi = 3.141;

int main()    
{    
    printf("%f %f", 4 * 10 * pi, 4 * 10 * pi1); 
}

Output (on my machine) is 125.639999 125.640000

like image 372
Sidath Asiri Avatar asked Mar 20 '23 17:03

Sidath Asiri


1 Answers

pi1 is a preprocessor symbol and is replaced textually as a double.

pi is a float constant initialized from a double, and thus lose some precision bits (see IEEE754 specs).

For more details, pi as a float is in fact stored as 0x40490625 which is 3.1410000324249267578125. pi1 is stored as 0x400920C49BA5E354, which is 3.1410000000000000142108547152

like image 174
Joel Falcou Avatar answered Mar 29 '23 06:03

Joel Falcou