这是一个非常简单的问题。在我看来, double 的上限为小数点后 15 位左右。例如,采用以下代码片段:
var test : Double = 3.141592653589793238462643383279
Swift playgrounds 将此 Double 的值显示为 3.141592653589793。有什么方法可以让我在 Double 上获得超过 15 位小数?从 Apple 的文档来看,这听起来应该是可能的:
Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double is preferred.
说“至少”的部分看起来这是可能的,但我找不到这样做的任何方法。我也知道 Swift Playgrounds 不是简单地截断输出中的值。下面的代码返回 true,即使值不一样,所以很明显 double 只存储和比较最多 15 位小数:
var test : Double = 3.141592653589793238462643383279
var result = test == 3.1415926535897932384626433
最佳答案
为了更精确使用 Float80 类型
(此结构中的第一位用于符号):
Float80.exponentBitCount // 15
Float80.significandBitCount // 63
Double.exponentBitCount // 11
Double.significandBitCount // 52
希望这对您有所帮助。
关于swift - 获取超过 15 位小数的 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058163/