I randomly found that the π value(PI_F) is define as 3.1415927410125732421875f (not something like 3.14159265358979323846264f in general), is there a reason doing that or it’s just something like a typo?
https://github.com/electro-smith/DaisySP/blob/master/Source/Utility/dsp.h (line13)
1 Like
Welcome @ABlackCat - nice first post! I’m going to go out on a limb here and say it’s a mistake. I’d be very interested to see a different explanation.
Probably something to do with precision.
On my desktop this:
#define PI 3.1415926535897932384626433f
void main (char argc, char **argv)
{
float pi = PI;
printf("PI=%.22f", pi);
}
gives:
$ gcc test.c && ./a.out
PI=3.1415927410125732421875
2 Likes
If i read correctly, this is actually the closest that 32 bit float can get
1 Like
Very interesting. Would there be a performance benefit to storing the value that way as opposed to the more ‘precise’ value? Like, does the processor-program get to skip a conversion step?
1 Like
All constants are resolved at compiled time, so it generally doesn’t matter. It likely is changed as it could get confusing to use a value that can’t be represent by hardware.
Things would be different if you build a firmware that actually uses 64 bit maths which is possible on STM32H7 (but using double precision for FPU has a performance cost).
3 Likes