int64
func int64[T: Number](x: T): int64int64 converts x to int64 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; int64(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
int64andintare one type under two names, soint64(n)on anintreturnsnunchanged, and Ant shows the type asint.- To drop a fraction, round first with
Math.trunc,Math.floor,Math.ceil, orMath.round.
Basic examples
Widen a smaller integer to int64:
int64(int32(-5)) // -5
int64(byte(200)) // 200Convert a float that holds a whole number:
int64(2.0) // 2
int64(-1e12) // -1000000000000Convert a value from another kind before mixing it with an int64:
let count: int64 = 10
let extra = int32(5)
count + int64(extra) // 15Scope
Every numeric kind converts, as long as the value fits:
int64(int8(-128)) // -128
int64(uint32(4000000000)) // 4000000000
int64(uint64(1) + uint64(2)) // 3
int64(float32(1.5e9)) // 1500000000
int64(-0.0) // 0The range ends at Math.minInt and Math.maxInt:
int64(Math.minInt) // -9223372036854775808
int64(Math.maxUint64 / uint64(2)) // 9223372036854775807
int64(Math.maxUint64 / uint64(2)) == Math.maxInt // trueA value of a named primitive type loses its tag:
type Cents = int64
let price = Cents(250)
price // Cents(250)
int64(price) // 250int64 is a function value, so it can be passed to Array.map:
let small: Array[int16] = [int16(1), int16(-2), int16(300)]
small | Array.map(int64) // [1, -2, 300]Applications
Add up int32 readings without overflow by widening them first:
let readings: Array[int32] = [int32(2000000000), int32(2000000000)]
readings | Array.map(int64) | Array.sum() // 4000000000Round a measurement to the nearest whole number, then convert:
let meters = 19.6
int64(Math.round(meters)) // 20
int64(Math.trunc(meters)) // 19Total the prices of a cart kept as a named primitive type:
type Cents = int64
let cart = [Cents(250), Cents(199), Cents(1000)]
println("total: ${(cart | Array.map(int64) | Array.sum()) ?? 0} cents")total: 1449 centsParse an unsigned number and use it where an int is expected:
func port(text: string): int {
let n = text.toUint() ?? return -1
if n > uint64(65535) {
return -1
}
int64(n)
}
println(port("8080"))
println(port("http"))
println(port("18446744073709551615"))8080
-1
-1Properties and relations
Widening an integer and narrowing it back gives the same value:
for n in [int32(-7), int32(0), int32(123456)] {
assert(int32(int64(n)) == n)
}int64 and int name one type, so an int64 goes wherever an int does:
let n: int = int64(int8(3))
assert(type(int64) == type(int))
assert(n == 3)An integer from -2^53 to 2^53 survives a round trip through float64:
for n in [0, -1, 9007199254740992] {
assert(int64(float64(n)) == n)
}int64(Math.trunc(x)) drops the fraction toward zero:
int64(Math.trunc(2.7)) // 2
int64(Math.trunc(-2.7)) // -2Possible issues
A float with a fraction panics, including one that float arithmetic produced:
int64(2.5) // panic: 2.5 has a fraction, so it does not convert to int
int64(0.1 + 0.2) // panic: 0.30000000000000004 has a fraction, so it does not convert to intA value outside the range of int64 panics:
int64(Math.maxUint64) // panic: 18446744073709551615 out of range for int
int64(1e19) // panic: 10000000000000000000.0 out of range for intNaN and the infinities have no integer value:
int64(Math.NaN) // panic: NaN does not convert to int
int64(Math.Infinity) // panic: Infinity does not convert to intTo get null instead of a panic, use Math.toInt:
Math.toInt(2.5) // null
Math.toInt(2.0) // 2float64(Math.maxInt) rounds up to 2^63, which is one past Math.maxInt, so converting it back panics:
int64(float64(Math.maxInt)) // panic: 9223372036854776000.0 out of range for intA float64 holds every integer only up to 2^53. Past that, the value already changed when it became a float64, and int64 returns the rounded number without a panic:
int64(float64(9007199254740993)) // 9007199254740992int64 does not parse text. Use String.toInt:
let n = int64("42")
// error: int converts a number or a named primitive type over a number, got stringConverting the result does not stop an overflow that already happened in a smaller kind. Widen before the arithmetic:
let a = int32(2000000000)
int64(a + a) // panic: integer overflow
int64(a) + int64(a) // 4000000000See also
int · int32 · uint64 · float64 · Math.trunc · Math.round · Math.toInt · String.toInt · String.toUint
