Math.max
func Math.max[T: Ordered](a: T, b: T): TMath.max returns the larger of a and b.
Details
- On floats, a
NaNargument givesNaN, whichever side it is on. Math.maxholds-0.0less than0.0, soMath.max(-0.0, 0.0)andMath.max(0.0, -0.0)are both0.0, though-0.0 < 0.0isfalse.- For the largest element of an array, use Array.max.
Basic examples
Math.max(3, 7) // 7
Math.max(-2.5, -1.0) // -1.0
Math.max("apple", "banana") // "banana"Scope
Every integer and float type works, and the result keeps its type:
Math.max(uint8(200), uint8(17)) // 200
Math.max(int8(-5), int8(-100)) // -5
Math.max(float32(1.5), float32(2.25)) // 2.25
Math.max(Math.maxInt, Math.minInt) // 9223372036854775807Strings, the empty string included:
Math.max("", "a") // "a"
Math.max("ant", "ant") // "ant"Special float values:
Math.max(-Math.Infinity, 2.0) // 2.0
Math.max(Math.NaN, 1.0) // NaN
Math.max(1.0, Math.NaN) // NaN
Math.max(Math.Infinity, Math.NaN) // NaNSigned zeros, told apart by dividing by them:
Math.max(0.0, -0.0) // 0.0
1.0 / Math.max(-0.0, 0.0) // Infinity
1.0 / Math.min(-0.0, 0.0) // -InfinityIn a pipe, the value is a:
5 | Math.max(8) // 8For three or more values, nest the calls, or use Array.max on an array:
Math.max(3, Math.max(9, 4)) // 9Pass Math.max as a function value:
[3, 9, 2].reduce(Math.minInt, Math.max) // 9Applications
Keep a value from going below a floor:
func withdraw(balance: int, amount: int): int {
Math.max(balance - amount, 0)
}
withdraw(100, 30) // 70
withdraw(100, 130) // 0The longest name in a list, kept as a running maximum:
type User = { name: string }
let users = [User{ name: "Ann" }, User{ name: "Bartholomew" }, User{ name: "Cy" }]
var width = 0
for u in users {
width = Math.max(width, u.name.length())
}
width // 11The overlap of two ranges, empty when they do not meet:
type Span = { start: int, end: int }
func overlap(a: Span, b: Span): int {
Math.max(0, Math.min(a.end, b.end) - Math.max(a.start, b.start))
}
overlap(Span{ start: 0, end: 10 }, Span{ start: 5, end: 20 }) // 5
overlap(Span{ start: 0, end: 10 }, Span{ start: 15, end: 20 }) // 0Properties and relations
Math.max is symmetric, gives one of its arguments, and is at least as large as both:
for [a, b] in [[3, 7], [-1, -4], [5, 5], [Math.minInt, 0]] {
let m = Math.max(a, b)
assert(m == Math.max(b, a))
assert(m == a || m == b)
assert(m >= a && m >= b)
}It agrees with Array.max on two elements, and with Math.min and Math.clamp:
let a = 4
let b = 11
assert(Math.max(a, b) == [a, b].max())
assert(Math.max(a, b) + Math.min(a, b) == a + b)
assert(Math.clamp(20, a, b) == Math.min(Math.max(20, a), b))Possible issues
Both arguments must have the same type. An int and a float64 are a compile error; convert one:
let x = Math.max(3, 2.5) // error: cannot use float64 where int is expectedMath.max(float64(3), 2.5) // 3.0A value typed Number, or a nullable value, must be narrowed first:
let a: Number = 1
let b: Number = 2
let m = Math.max(a, b)
// error: T would be Number, which is not a single member of its bound Ordered; narrow firstlet x: int | null = 3
let m = Math.max(x ?? 0, 1)
m // 3A named primitive type is not Ordered. Unwrap it, then tag the result again:
type Score = int
let m = Math.max(Score(3), Score(5))
// error: T would be Score, which is not a member of its bound Ordered; unwrap it with int(...)type Score = int
let best = Score(Math.max(int(Score(3)), int(Score(5))))
best // Score(5)bool is not Ordered:
let m = Math.max(true, false)
// error: T would be bool, which is not a member of its bound OrderedA number has no namespace, so a call through . is a compile error. Write Math.max(n, 4), or use a pipe:
let n = 3
let m = n.max(4) // error: int has no field or function maxSee also
Math.min · Math.clamp · Array.max · Array.maxBy · Math namespace
