A single/double/extended-precision floating-point representation of Pi is accurate up to how many decimal places?
相关问题
- Floating point again
- Is there a functional difference between “2.00” an
- How are these double precision values accurate to
- Having trouble rounding in c
- Having trouble rounding in c
相关文章
- How can I convert a f64 to f32 and get the closest
- Macro or function to construct a float (double) fr
- Math.Max vs Enumerable.Max
- `std::sin` is wrong in the last bit
- Rounding problem with rspec tests when comparing f
- PI and accuracy of a floating-point number
- String.format uses comma instead of point
- How are floating point array indices interpreted?
Accuracy of a floating-point type is not related to PI or any specific numbers. It only depends on how many digits are stored in memory for that specific type.
In case of IEEE-754
float
uses 23 bits of mantissa so it can be accurate to 23+1 bits of precision, or ~7 digits of precision in decimal. Regardless of π, e, 1.1, 9.87e9... all of them is stored with exactly 24 bits in a float. Similarlydouble
(53 bits of mantissa) can store 15~17 decimal digits of precision.When I examined Quassnoi's answer it seemed suspicious to me that
long double
anddouble
would end up with the same accuracy so I dug in a little. If I ran his code compiled with clang I got the same results as him. However I found out that if I specified thelong double
suffix and used a literal to initialize the long double it provided more precision. Here is my version of his code:And the results:
Print and count, baby, print and count. (Or read the specs.)
Since there are sieve equations for binary representations of pi, one could combine variables to store pieces of the value to increase precision. The only limitation to the precision on this method is conversion from binary to decimal, but even rational numbers can run into issues with that.
For C code, look at the definitions in
<float.h>
. That coversfloat
(FLT_*
),double
(DBL_*
) andlong double
(LDBL_*
) definitions.* EDIT: see this post for up to date discussion: Implementation of sinpi() and cospi() using standard C math library *
The new math.h functions
__sinpi()
and__cospi()
fixed the problem for me for right angles like 90 degrees and such.