简体   繁体   English

CGFloat 比 Double 更准确吗?

[英]Is CGFloat more accurate than Double?

Double & CGFloat both print the same value to the console (#2), but only the Double is issued the warning (#1): Double 和 CGFloat 都向控制台打印相同的值 (#2),但只有 Double 发出警告 (#1):

(#1) - The warning (#1) - 警告

警告

(#2) - Printing each to the console (#2) - 将每个打印到控制台

DOUBLE  = 9.223372036854776e+18
CGFLOAT = 9.223372036854776e+18

Many articles mention that a CGFloat is a Float on 32-bit platforms & a Double on a 64-bit platform, but doesn't that only refer to the backing storage?许多文章提到 CGFloat 在 32 位平台上是 Float,在 64 位平台上是 Double,但这不只是指后备存储吗? Is there more going on behind the scenes which causes the CGFloat to be more accurate than the Double?是否有更多幕后活动导致 CGFloat 比 Double 更准确?

No , CGFloat does not represent a BigDecimal -type number, with infinite precision.CGFloat不代表BigDecimal类型的数字,具有无限精度。 CGFloat is defined as a struct with either wraps a Double or a Float , depending on whether you are on a 32-bit or 64-bit platform; CGFloat 被定义为包含DoubleFloatstruct ,具体取决于您是在 32 位还是 64 位平台上; ie, on a 64-bit platform, it is equivalent to a Double , with no more or less precision or range .即,在 64 位平台上,它等同Double没有更多或更少的精度或范围

The difference in the warning you see is that Double is a type native to Swift, which the compiler knows about, and has enough information about in order to be able to provide such a warning.您看到的警告中的不同之处在于, Double是 Swift 的原生类型,编译器知道它,并且有足够的信息来提供这样的警告。 CGFloat , however, is defined by Apple frameworks, and the compiler does not know enough about it to produce such a warning.然而, CGFloat是由 Apple 框架定义的,编译器对它的了解不足以产生这样的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM