uint64
func uint64[T: Number](x: T): uint64uint64 converts x to uint64 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; uint64(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
- An integer literal is always an
int, so auint64comes from a conversion, such asuint64(42), or from a function that returns one, such asString.toUint. - To drop a fraction, round first with
Math.trunc,Math.floor,Math.ceil, orMath.round. - Arithmetic on two
uint64values stays auint64and panics on overflow, including subtraction below zero.Bits.wrapAdd,Bits.wrapSub, andBits.wrapMulwrap modulo 2^64 instead.
Basic examples
Convert an int that is not negative:
uint64(42) // 42
uint64(0) // 0Convert a float that holds a whole number:
uint64(3.0) // 3
uint64(1e19) // 10000000000000000000Convert a value before mixing it with a uint64:
let total = uint64(100)
total + uint64(byte(7)) // 107Scope
Every numeric kind converts, as long as the value is in range:
uint64(int8(127)) // 127
uint64(uint32(4000000000)) // 4000000000
uint64(Math.maxInt) // 9223372036854775807
uint64(float32(1e10)) // 10000000000
uint64(-0.0) // 0The range goes past Math.maxInt, up to Math.maxUint64:
Math.maxUint64 // 18446744073709551615
uint64(Math.maxInt) * uint64(2) + uint64(1) // 18446744073709551615
uint64(Math.pow(2.0, 63.0)) // 9223372036854775808A value of a named primitive type loses its tag:
type UserId = uint64
let id = UserId(uint64(7))
id // UserId(7)
uint64(id) // 7uint64 is a function value, so it can be passed to Array.map:
[1, 2, 3] | Array.map(uint64) // [1, 2, 3]Applications
Hash a string with 64-bit FNV-1a, which relies on multiplication that wraps:
func fnv1a(s: string): uint64 {
var h = "14695981039346656037".toUint() ?? panic("bad offset")
let prime = uint64(1099511628211)
for b in s.toBytes() {
h = Bits.wrapMul(Bits.xor(h, uint64(b)), prime)
}
h
}
println(fnv1a(""))
println(fnv1a("a"))14695981039346656037
12638187200555641996Add up file sizes as uint64, where a negative size panics instead of slipping into the total:
let sizes = [1200, 3400, 560]
(sizes | Array.map(uint64) | Array.sum()) ?? uint64(0) // 5160Properties and relations
Converting a non-negative int to uint64 and back gives the same value:
for n in [0, 1, Math.maxInt] {
assert(int(uint64(n)) == n)
}Math.maxUint64 is the largest uint64, and adding one with Bits.wrapAdd wraps to zero:
assert(Bits.wrapAdd(Math.maxUint64, uint64(1)) == uint64(0))
assert(Bits.wrapSub(uint64(0), uint64(1)) == Math.maxUint64)Round first to drop a fraction: Math.trunc rounds toward zero, Math.floor down:
uint64(Math.trunc(2.7)) // 2
uint64(Math.floor(9.99)) // 9Possible issues
A negative value panics, even -1:
uint64(-1) // panic: -1 out of range for uint64
uint64(-1.0) // panic: -1.0 out of range for uint64A float with a fraction panics:
uint64(2.5) // panic: 2.5 has a fraction, so it does not convert to uint64NaN and the infinities have no integer value:
uint64(Math.NaN) // panic: NaN does not convert to uint64
uint64(-Math.Infinity) // panic: -Infinity does not convert to uint64float64(Math.maxUint64) rounds up to 2^64, which is one past Math.maxUint64, so converting it back panics:
uint64(float64(Math.maxUint64)) // panic: 18446744073709552000.0 out of range for uint64Subtraction below zero and addition past Math.maxUint64 panic. Use Bits.wrapSub or Bits.wrapAdd when wrapping is what you want:
uint64(1) - uint64(2) // panic: integer overflow
Math.maxUint64 + uint64(1) // panic: integer overflow
Bits.wrapSub(uint64(1), uint64(2)) // 18446744073709551615A large uint64 does not fit an int:
int(Math.maxUint64) // panic: 18446744073709551615 out of range for intA uint64 does not mix with an int, not even with a literal:
let n = uint64(5)
let m = n + 1 // error: + needs both operands of one type, got uint64 and intuint64 does not parse text. Use String.toUint:
let n = uint64("42")
// error: uint64 converts a number or a named primitive type over a number, got stringSee also
uint32 · int64 · int · byte · String.toUint · Bits.wrapAdd · Bits.wrapMul · Math.trunc
