浮点数精度 [英] Precision of floating-point numbers
问题描述
Python中的浮点数精度是多少?它是否始终是双精度的,或者是实现或特定于平台的?例如,CPython和PyPy是否有所不同?
What precision are the floating-point numbers in Python? Is it always double-precision or is it implementation or platform specific? Does it differ in, say, CPython and PyPy?
推荐答案
来自 Python 文档:
From Python documentation:
浮点数通常使用C中的 double 来实现; 有关 精度 的信息以及内部的表示形式 程序所在机器的浮点数 在 sys.float_info 中可用.
Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in sys.float_info.
sys.float_info 是一个结构序列,其中包含有关浮点类型的信息.它包含有关精度和内部表示的低级信息.
sys.float_info is a struct sequence holding information about the float type. It contains low level information about the precision and internal representation.
import sys
print(sys.float_info.mant_dig)
mant_dig
属性指向浮点精度:浮点数有效的基数基数
几乎所有平台都将Python浮点数映射到 IEEE 754 double 精度.
Almost all platforms map Python floats to IEEE 754 double precision.
这篇关于浮点数精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!