Long Luo's Life Notes

每一天都是奇迹

By Long Luo

挖坑

参考文献

  1. Cody Waite和Payne-Hanek归约算法的详细解释

https://github.com/taschini/crlibm/blob/master/trigo_fast.c

https://pavpanchekha.com/blog/synthesize-range-reductions.html

https://github.com/uxlfoundation/oneapi-construction-kit/blob/main/doc/modules/builtins/abacus.md

https://forum.orekit.org/t/jmh-performance-benchmarks-math-vs-strictmath-vs-fastmath-hipparchus-vs-fastmath-apache/4661/20?page=3

By Long Luo

挖坑

假如你知道浮点数的话,你就知道为什么了!

按照 IEEE 754 浮点数标准 制定的 浮点数运算法则, float 类型的单精度浮点数 的尾数部分有 \(23\) 位二进制数,如下图所示:

IEEE 754 Single Floating Point Format

在十进制下,大致相当于 \(\log_{10}{2^{23}} = 23 \cdot \log {2} \approx 23 \times 0.301 \approx 6.9\) ,有效数字大约有 \(7\) 位。

所以当 \(x = 1000001\) 时,我们应该使用 double 类型的双精度浮点数 [^12] ,这样才能保证结果有足够的精度

双精度浮点数的尾数部分有 \(52\) 位,如下图所示:

IEEE 754 Double Floating Point Format

在十进制中大致相当于 \(\log_{10}{2^{52}} = 52 \cdot \log {2} \approx 52 \times 0.301 \approx 15.6\) ,也就是说当 \(x\) 有效数字是 \([7, 15]\) 时,我们应该使用 double 类型的双精度浮点数可以保证精度!

但这仍然有个问题,那就是 \(x\) 有效数字 超过 \(15\) 位,应该怎么办?

参考文献

  1. IEEE 754
  2. Floating-point arithmetic
  3. Single-precision floating-point format 单精度浮点数
  4. Double-precision floating-point format 双精度浮点数
0%