Search code examples
ieee-754pi

the most accurate approximation of pi in IEEE-754 float64?


what is the most accurate approximation of pi possible in IEEE-754 float64?

fwiw it seems both Javascript and PHP use 3.141592653589793115997963468544185161590576171875 , which might be the answer, i don't know.


Solution

  • Yes, 3.141592653589793115997963468544185161590576171875 is the IEEE-754 binary641 number closest to π. It can also be written as a hexadecimal floating-point constant, 0x1.921fb54442d18p1. (I keep 0x3.243f6a8885a308d313198a2e03707344ap0L on hand to have the value for wider formats as well.) The C standard requires C implementations that use a base-two floating-point format to correctly round hexadecimal floating-point constants, and it does not require that for decimal floating-point constants, so you may be more likely to get a correct result when you use the hexadecimal form.

    Footnote

    1 IEEE-754 2008 uses “binary64” for the standard 64-bit base-two format. It is also called “double precision.” Some programming languages might call it float64 or Float64.