Current learning Swift, there are ways to find max and min value for different kind of Integer like Int.max
and Int.min
.
现有的快速学习算法,对于不同类型的整数,如Int.max和Int.min,都有求最大值和最小值的方法。
Is there a way to find max value for Double and Float? Moreover, which document should I refer for this kind of question? I am currently reading Apple's The Swift Programming Language.
有没有一种方法可以找到Double和Float的最大值?另外,这类问题我应该参考哪一份文件?我目前正在阅读苹果的Swift编程语言。
4 个解决方案
#1
56
While there’s no Double.max
, it is defined in the C float.h
header, which you can access in Swift via import Darwin
.
虽然没有翻倍。max,它是在C浮点数中定义的。h header,你可以通过import Darwin快速访问。
import Darwin
let fmax = FLT_MAX
let dmax = DBL_MAX
These are roughly 3.4 * 10^38
and 1.79 * 10^308
respectively.
这些是约3.4 * 10 ^ 38岁,分别为1.79 * 10 ^ 308。
But bear in mind it’s not so simple with floating point numbers (it’s never simple with floating point numbers). When holding numbers this large, you lose precision in a similar way to losing precision with very small numbers, so:
但请记住,浮点数并不简单(浮点数也不简单)。当保持这么大的数字时,您将以类似于在非常小的数字中失去精度的方式失去精度,因此:
let d = DBL_MAX
let e = d - 1.0
let diff = d - e
diff == 0.0 // true
let maxPlusOne = DBL_MAX + 1
maxPlusOne == d // true
let inf = DBL_MAX * 2
// perhaps infinity is the “maximum”
inf == Double.infinity // true
So before you get into some calculations that might possibly brush up against these limits, you should probably read up on floating point. Here and here are probably a good start.
在你开始计算这些极限之前,你应该先读一下浮点数。这里和这里可能是一个好的开始。
#2
82
As of Swift 3, you should use:
在Swift 3中,您应该使用:
CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude
#3
8
AV's answer is fine, but I find those macros hard to remember and a bit non-obvious, so eventually I made Double.MIN
and friends work:
AV的答案是好的,但是我发现这些宏很难记住,而且有点不明显,所以最终我做了Double。最小值和朋友工作:
extension Double {
static var MIN = -DBL_MAX
static var MAX_NEG = -DBL_MIN
static var MIN_POS = DBL_MIN
static var MAX = DBL_MAX
}
Don't use lowercase min and max -- those symbols are used in Swift 3.
不要使用小写的最小值和最大值——这些符号在Swift 3中使用。
#4
3
Just Write
只写
let mxFloat = MAXFLOAT
You will get maximum value of float in swif.
您将在swif中获得浮点数的最大值。
#1
56
While there’s no Double.max
, it is defined in the C float.h
header, which you can access in Swift via import Darwin
.
虽然没有翻倍。max,它是在C浮点数中定义的。h header,你可以通过import Darwin快速访问。
import Darwin
let fmax = FLT_MAX
let dmax = DBL_MAX
These are roughly 3.4 * 10^38
and 1.79 * 10^308
respectively.
这些是约3.4 * 10 ^ 38岁,分别为1.79 * 10 ^ 308。
But bear in mind it’s not so simple with floating point numbers (it’s never simple with floating point numbers). When holding numbers this large, you lose precision in a similar way to losing precision with very small numbers, so:
但请记住,浮点数并不简单(浮点数也不简单)。当保持这么大的数字时,您将以类似于在非常小的数字中失去精度的方式失去精度,因此:
let d = DBL_MAX
let e = d - 1.0
let diff = d - e
diff == 0.0 // true
let maxPlusOne = DBL_MAX + 1
maxPlusOne == d // true
let inf = DBL_MAX * 2
// perhaps infinity is the “maximum”
inf == Double.infinity // true
So before you get into some calculations that might possibly brush up against these limits, you should probably read up on floating point. Here and here are probably a good start.
在你开始计算这些极限之前,你应该先读一下浮点数。这里和这里可能是一个好的开始。
#2
82
As of Swift 3, you should use:
在Swift 3中,您应该使用:
CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude
#3
8
AV's answer is fine, but I find those macros hard to remember and a bit non-obvious, so eventually I made Double.MIN
and friends work:
AV的答案是好的,但是我发现这些宏很难记住,而且有点不明显,所以最终我做了Double。最小值和朋友工作:
extension Double {
static var MIN = -DBL_MAX
static var MAX_NEG = -DBL_MIN
static var MIN_POS = DBL_MIN
static var MAX = DBL_MAX
}
Don't use lowercase min and max -- those symbols are used in Swift 3.
不要使用小写的最小值和最大值——这些符号在Swift 3中使用。
#4
3
Just Write
只写
let mxFloat = MAXFLOAT
You will get maximum value of float in swif.
您将在swif中获得浮点数的最大值。