Number literals
An integer literal such as 42, 0xFF, or 1_000 is an int. A float literal such as 3.14 or 1e9 is a float64.
Details
- An integer literal is always an
intand a float literal is always afloat64. No suffix picks a numeric type: write1.0for a float, and call a conversion such asbyte(255)orfloat32(1.5)for any other numeric type. The only letters that can follow a number are the duration unitsh,m,s,ms,usandns, and2h30mis then a duration literal of typeTime.Duration, not a number. - A float literal has digits on both sides of the point, an exponent, or both.
.5and1.are errors. _may stand between two digits only, to group them.- The prefixes
0x,0o,0band the exponent'seare lowercase. Hexadecimal digits may be either case. - A decimal integer, or the digits before a float's point, start with
0only when they are0:0,0.5, and0e3are fine;007and00.5are errors. Octal is0o7. - A literal that does not fit its type is a compile error. The most negative
inthas no literal; writeMath.minInt.
Basic examples
Integers in decimal, hexadecimal, octal, and binary:
42 // 42
1_000_000 // 1000000
0xFF // 255
0o755 // 493
0b1010 // 10Floats with a point, an exponent, or both:
3.14 // 3.14
1e9 // 1000000000.0
2.5e-3 // 0.0025Scope
Digit separators
_ groups digits in any base, and in either part of a float:
0xFFFF_FFFF // 4294967295
0b1111_0000 // 240
1_000.000_1 // 1000.0001Hexadecimal digits
The prefix is lowercase; the digits are not:
0xff == 0xFF // true
0xCafe // 51966Zero
A decimal integer, or the digits before a float's point, start with 0 only when they are 0. Digits after a prefix may have leading zeros:
0 // 0
0.5 // 0.5
0e3 // 0.0
0x0F // 15Negative numbers
A minus sign is the negation operator applied to a literal:
-5 // -5
-2.5e-3 // -0.0025
-0.0 // -0.0A duration literal takes no minus; write Time.negate(5s):
let d = -5s // error: unary - needs a number, got Time.DurationInteger and float are different types
1 and 1.0 are values of different types. is tells them apart in a union:
let n: int | float64 = 1.0
n is float64 // true
n is int // falseOther numeric types
A conversion reaches every other numeric type: the literal is an int or a float64, and the conversion turns it into the target type:
byte(255) // 255
int8(-128) // -128
uint64(0xFF) // 255
float32(1.5) // 1.5Limits
The largest int is a literal; the smallest is Math.minInt:
9_223_372_036_854_775_807 == Math.maxInt // true
-9_223_372_036_854_775_807 - 1 == Math.minInt // trueA float literal can be as large as the largest float64 and as small as the smallest one above zero:
1.7976931348623157e308 // 1.7976931348623157e+308
5e-324 // 5e-324Applications
File modes read naturally in octal:
let path = "notes.txt"
Result.unwrap(File.write(path, "hello\n"))
defer { File.remove(path) }
Result.unwrap(File.chmod(path, 0o600))
let info = Result.unwrap(File.stat(path))
println(String.fromInt(info.mode, base: 8))600Flags written in binary show which bit each one sets:
let read = 0b100
let write = 0b010
let exec = 0b001
let mode = Bits.or(read, write)
Bits.and(mode, write) != 0 // true
Bits.and(mode, exec) != 0 // falseSplit a hexadecimal color into its channels:
let orange = 0xFF8800
let red = Bits.shiftRight(orange, 16)
let green = Bits.and(Bits.shiftRight(orange, 8), 0xFF)
let blue = Bits.and(orange, 0xFF)
[red, green, blue] // [255, 136, 0]Spell bytes in hexadecimal:
Encoding.toHex(Bytes.fromArray([0xCA, 0xFE])) // "cafe"Properties and relations
A number has the same value in every base:
assert(255 == 0xFF)
assert(0xFF == 0o377)
assert(0o377 == 0b1111_1111)_ does not change a value, and an exponent scales by a power of ten:
assert(1_000_000 == 1000000)
assert(1e6 == 1_000_000.0)
assert(2.5e-3 == 0.0025)Possible issues
An integer literal is not a float
1 is an int, so it does not fit where a float64 is expected:
let ratio: float64 = 1 // error: cannot use int where float64 is expectedWrite a float literal instead:
let ratio: float64 = 1.0
ratio // 1.0Literals do not adapt to a sized type
An integer literal stays an int even where a byte or int8 is expected:
let b: byte = 255 // error: cannot use int where byte is expectedConvert it:
let b = byte(255)
b // 255Integer and float do not mix
let total = 1 + 1.0 // error: + needs both operands of one type, got int and float64Comparison does not mix them either:
let same = 1 == 1.0 // error: int and float64 cannot be compared with ==: neither is assignable to the otherConvert the int, or write both as floats:
float64(1) + 1.0 // 2.0
1.0 + 1.0 // 2.0Integer division truncates
Two integer literals divide as integers:
7 / 2 // 3
7.0 / 2.0 // 3.5Floats are binary fractions
Most decimal fractions have no exact float64:
0.1 + 0.2 // 0.30000000000000004
0.1 + 0.2 == 0.3 // falseA leading zero is not octal
Other languages read 007 as octal. Ant rejects it; write 0o7:
let code = 007 // error: a decimal literal other than 0 cannot start with 0A uint64 above Math.maxInt has no literal
A conversion takes an int literal, so a larger uint64 does not fit it:
let max = uint64(18446744073709551615) // error: integer literal 18446744073709551615 does not fit an intWrite Math.maxUint64 for the largest one.
A float needs digits on both sides of the point
let half = .5 // error: a float literal needs a digit before the pointlet one = 1. // error: a float literal needs a digit after the pointPrefixes and exponents are lowercase
let mask = 0XFF // error: malformed number literal: write the prefix 0x in lowercaselet big = 1E9 // error: malformed number literal: write the exponent with a lowercase e_ goes between digits
A doubled, leading, or trailing _ is an error:
let n = 1__000 // error: malformed number literalOut of range
An integer literal past Math.maxInt does not compile, even after a minus sign:
let low = -9223372036854775808 // error: integer literal 9223372036854775808 does not fit an intA float literal that would round to infinity or to zero does not compile either:
let huge = 1e400 // error: float literal 1e400 is out of rangelet tiny = 1e-400 // error: float literal 1e-400 is out of rangeSee also
int · float64 · byte · Arithmetic operators · Math · Bits · String.toInt · String.fromInt · String literals · Duration literals
