swift - 获取超过 15 位小数的 Swift

标签 swift cocoa-touch double

这是一个非常简单的问题。在我看来, 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

enter image description here

希望这对您有所帮助。

关于swift - 获取超过 15 位小数的 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058163/

相关文章:

c++ - 如果我试图用另一种语言复制一个程序。使用更高的精度是不明智的吗?

c - 当指向结构的双指针是函数的参数时,为什么我们使用 "&(*"语句?

ios - 可扩展的 ListView 不会 swift 折叠

swift - 对 UITabBarController 使用自定义 collectionView

ios - UICollectionView Flow 布局没有固定的行高

ios - 如何在 Sprite 套件游戏的触摸方法中编写 UIbuttons/IBactions

c++ - 是否存在将 double 转换为 long long 的优雅方法?

ios - 如何从 SwiftUI 的详细信息 View (编辑项目 View )中删除核心数据条目?

ios - 如何在使用 swift 切换不同 View 的同时连续播放背景音乐?

ios - 我们可以修改 iOS 中的 UIViewController 属性吗?