float64
func float64[T: Number](x: T): float64float64 converts x to float64, rounding to the nearest value it holds; it never panics. x may also be a named primitive over a number.
Details
- An integer above 2^53 in magnitude may not fit exactly; it rounds to the nearest
float64, ties to even. - A
float32widens exactly;NaN, the infinities, and-0.0are kept.
Basic examples
Convert an int to divide with a fraction:
float64(3) // 3.0
float64(7) / 2.0 // 3.5
let n = 7
float64(n) / 2.0 // 3.5The average of an array of integers:
let scores = [3, 4, 6]
float64(scores.sum() ?? 0) / float64(len(scores)) // 4.333333333333333Scope
Every numeric type converts, the sized ones included:
float64(byte(200)) // 200.0
float64(int8(-5)) // -5.0
float64(uint16(500)) // 500.0
float64(float32(1.5)) // 1.5
float64(2.5) // 2.5The largest integers round to the nearest float64: Math.maxInt to 2^63 and Math.maxUint64 to 2^64.
float64(Math.maxInt) // 9223372036854776000.0
float64(Math.minInt) // -9223372036854776000.0
float64(Math.maxUint64) // 18446744073709552000.0A float32 widens to the same binary value, which is not the decimal it was written as:
float64(float32(0.1)) // 0.10000000149011612NaN, the infinities, and the sign of zero stay as they are:
float64(float32(Math.NaN)) // NaN
float64(-float32(Math.Infinity)) // -Infinity
float64(float32(-0.0)) // -0.0A named primitive over a number is unwrapped, and the result is a plain float64:
type Meters = float64
type Count = int
float64(Meters(2.5)) // 2.5
float64(Count(4)) // 4.0float64 is a function value, so it maps an array or ends a pipe:
[1, 2, 3].map(float64) // [1.0, 2.0, 3.0]
7 | float64() // 7.0Applications
Take the square root of an integer; Math.sqrt takes a float64:
let area = 50
Math.sqrt(float64(area)) // 7.0710678118654755Show a share as a percentage:
func percent(part: int, whole: int): string {
let p = float64(part) / float64(whole) * 100.0
"${String.fromFloat(p, 1)}%"
}
println(percent(1, 3))
println(percent(7, 8))33.3%
87.5%A generic mean over any numeric array converts each element:
func mean[T: Number](xs: Array[T]): float64 {
var total = 0.0
for x in xs {
total += float64(x)
}
total / float64(len(xs))
}
println(mean([1, 2, 4]))
println(mean([byte(1), byte(2)]))
println(mean([0.5, 1.5]))2.3333333333333335
1.5
1.0Properties and relations
An integer up to 2^53 in magnitude survives the round trip through float64:
for n in [0, -17, 9007199254740992, -9007199254740992] {
assert(int(float64(n)) == n)
}A float32 survives the round trip through float64:
for x in [float32(0.1), float32(-2.75), float32(Math.Infinity)] {
assert(float32(float64(x)) == x)
}On a float64, it is the identity:
for x in [2.5, -0.0, Math.Infinity, Math.maxFloat64] {
assert(float64(x) == x)
}Possible issues
An int and a float64 never mix in arithmetic. Convert one side:
let n = 7
println(n / 2.0) // error: / needs both operands of one type, got int and float64let n = 7
float64(n) / 2.0 // 3.5An integer literal is an int, never a float64. Write 3.0:
let x: float64 = 3 // error: cannot use int where float64 is expectedAbove 2^53, distinct integers can convert to the same float64:
float64(9007199254740993) == float64(9007199254740992) // truefloat64(Math.maxInt) rounds up to 2^63, which no int holds, so converting back panics:
int(float64(Math.maxInt)) // panic: 9223372036854776000.0 out of range for intMath.toInt returns null instead of panicking:
Math.toInt(float64(Math.maxInt)) ?? 0 // 0Converting back to an integer is exact, so a fraction panics. Round first with Math.round or Math.trunc:
let ratio = float64(7) / 2.0
int(ratio) // panic: 3.5 has a fraction, so it does not convert to int
int(Math.trunc(ratio)) // 3
int(Math.round(ratio)) // 4A string is not a number. Parse it with String.toFloat, which returns null on text that is not a number:
println(float64("1.5"))
// error: float64 converts a number or a named primitive type over a number, got stringString.toFloat("1.5") ?? 0.0 // 1.5A union such as int | null is not a number. Narrow it first:
let n: int | null = 3
println(float64(n))
// error: float64 converts a number or a named primitive type over a number, got int | nulllet n: int | null = 3
float64(n ?? 0) // 3.0See also
float32 · int · Math.trunc · Math.round · Math.toInt · String.toFloat · String.fromFloat · float types · Number literals · Arithmetic operators
