Reference

std/number/lib

std/number/src/lib.trb

Every numeric type of the language: Numeric, Signed, Bits and Real, the traits their operators go through; the sized integer types and the two floating-point types; Decimal; and the conversions between all of them.

Reach for Int, UInt or Float - the three default-width aliases - and only name a sized type (Int8, UInt32, ...) where a signature has to say exactly how wide a value is.

trait Numeric

trait Numeric with Add, Subtract, Multiply, Divide, Remainder, Equals, Compare, Show, TryFrom<String, NumberParseError>

The arithmetic and comparison every number has: +, -, *, /, %, ==, <, reading one from text, and its zero and its one.

Text is a source like any other, so Int.tryFrom("42") is the conversion and there is no parse beside it.

Examples

fn total<Scalar: Numeric>(values: List<Scalar>): Scalar {
  var sum = Scalar.zero
  for value in values {
    sum = sum + value
  }
  sum
}

print total([1, 2, 3])
print total([0.5, 0.25])

const zero

static zero: Self

Zero of this type: what a sum starts from, and what a generic body compares against. Scalar.zero reads it through a type parameter, Int.zero and Float.zero through the type.

const one

static one: Self

One of this type: what a product starts from, and the unit of an identity matrix or a homogeneous coordinate.

trait Signed

trait Signed with Numeric, Negate

Numeric plus a sign: the unary - and Signed.absolute. What every signed number has beyond an unsigned one.

fn absolute

fn absolute(): Self

The value without its sign.

trait Real

trait Real with Signed, Power, Power<Int64>

Signed plus the operations that need a root or an angle: the scalar of geometry, trigonometry and interpolation.

It is the bound that separates what a vector can do with whole numbers from what it needs a continuous scalar for. lengthSquared and dot are arithmetic and live under Numeric; length, normalized, rotated and every angle need a Real. A library that is generic over the scalar therefore says which half it is in by its bound alone, and a grid vector never accidentally gets a square root.

Two types carry it. Float64 is the fast one and answers whatever the platform's mathematics library answers. Fixed (std/linear) is the deterministic one: every operation is integer arithmetic, so it answers the same bits on every platform and out of every back end, which is what lockstep simulation and a replay need.

Angles are in radians throughout, and std/linear's Angle is the wrapper that keeps degrees from being passed as radians.

Examples

fn hypotenuse<Scalar: Real>(first: Scalar, second: Scalar): Scalar {
  (first * first + second * second).squareRoot()
}

print hypotenuse(3.0, 4.0)

Pitfalls

  • The results are not bit-identical between implementors, and for Float64 not even between platforms: sine is whatever the platform's mathematics library computes. Where the last bit has to agree - a lockstep simulation, a replay, a checksum over a world state - the scalar is Fixed and not Float.

Open

  • Float32 cannot carry it yet. Float32 has no arithmetic that a compiled program runs - every one of its operators is a planned row of the manifest of natives - and there is no conversion from a Float64 back down to one, so no body could be run for it. <math.h> has the float functions (sinf, powf, ...), so once its arithmetic exists, Real is one extend over them.

Related

  • Signed - what a Real is on top of.
  • Numeric - the bound the arithmetic half of a vector library lives under.

const pi

static pi: Self

Half a turn in radians - the ratio of a circle's circumference to its diameter - in this scalar's type: Scalar.pi in a body that is generic over it, whose literals could not write the digits.

const tau

static tau: Self

A whole turn in radians, twice Real.pi, rounded once in this scalar's type rather than doubled.

const e

static e: Self

The base of the natural logarithm, in this scalar's type: Scalar.e, named like Real.pi.

const epsilon

static epsilon: Self

The gap between one and the next value this scalar can hold: the resolution a tolerance cannot go below. It is the machine epsilon of a float and the one part of a fixed-point number.

fn squareRoot

fn squareRoot(): Self

The non-negative square root. Negative input answers a value the implementor documents.

fn sine

fn sine(): Self

The sine of an angle in radians.

fn cosine

fn cosine(): Self

The cosine of an angle in radians.

fn tangent

fn tangent(): Self

The tangent of an angle in radians.

fn arcSine

fn arcSine(): Self

The angle in radians whose sine is this value, in [-pi/2, pi/2].

Panics

Outside [-1, 1], where no angle has this sine. The precondition is an ordinary expression, value.absolute() <= Scalar.one, so a value outside is a broken promise of the program and every implementor stops there rather than answering a number (docs/design/PANICS.md, 5.1). value.clamp(-Scalar.one, Scalar.one) first is the total form, for a ratio that rounding pushed a hair past one.

fn arcCosine

fn arcCosine(): Self

The angle in radians whose cosine is this value, in [0, pi].

Panics

Outside [-1, 1], as Real.arcSine says.

fn arcTangent

fn arcTangent(): Self

The angle in radians whose tangent is this value, in (-pi/2, pi/2).

fn arcTangentDivided

fn arcTangentDivided(by: Self): Self

The angle in radians of the point (by, self), in (-pi, pi]: the arc tangent of self / by that uses the sign of both to pick the quadrant. This is what turns a vector into a direction, and it is why it takes a label - y.arcTangentDivided(by: x) says which of the two is which, where a bare pair of arguments does not.

fn exponential

fn exponential(): Self

Real.e raised to this value: Scalar.e ** value, without a logarithm of e in between.

fn naturalLogarithm

fn naturalLogarithm(): Self

The logarithm to the base Real.e. Zero and a negative value answer what the implementor documents.

fn logarithm

fn logarithm(base: Self): Self

The logarithm to another base: (8.0).logarithm(base: 2.0) is 3.0. It is the natural logarithm of the value over the one of the base, so an implementor that has a faster way overrides it.

fn floor

fn floor(): Self

Rounded towards negative infinity.

fn ceiling

fn ceiling(): Self

Rounded towards positive infinity.

fn round

fn round(): Self

Rounded to the nearest whole value, halves away from zero.

fn halved

fn halved(): Self

Half the value. It is a member and not value / 2 at the call, because a body that is generic over the scalar cannot write the literal 2 at all: a literal has a type, and inside such a body that type is the parameter. Halving is what a midpoint, a radius and an average are made of, so the trait carries it.

fn doubled

fn doubled(): Self

Twice the value.

fn radiansOfDegrees

fn radiansOfDegrees(): Self

The angle in radians that this many degrees is.

Degrees are a member of the scalar and not a function of std/linear, because the conversion needs the value of pi in this scalar's type - and a body that is generic over the scalar can write neither pi nor 180. Each implementor carries its own exact factor.

fn degreesOfRadians

fn degreesOfRadians(): Self

The angle in degrees that this many radians is.

trait Bits

trait Bits

The bit operations of an integer, and the operators that are them: a & b is bitwiseAnd, a | b bitwiseOr, a ^ b bitwiseExclusiveOr, ~a bitwiseNot, a << n shiftedLeft(by:) and a >> n shiftedRight(by:).

const value = 0x1234
const low = value & 0xFF
const mixed = (value ^ low) << 5
print "{low} {mixed}"

The operators sit where Go and Swift put them, so there is no C trap: & binds like *, | and ^ like +, a shift between * and ** - and flags & 1 == 0 is (flags & 1) == 0.

The shift of a signed type is arithmetic (it keeps the sign), the shift of an unsigned type is logical, and the bits that leave the width are dropped. A shift by a negative amount or by the width of the type or more panics, like every other operation that leaves its range.

fn bitwiseAnd

fn bitwiseAnd(other: Self): Self

Bit-for-bit and. This is &.

fn bitwiseOr

fn bitwiseOr(other: Self): Self

Bit-for-bit or. This is |.

fn bitwiseExclusiveOr

fn bitwiseExclusiveOr(other: Self): Self

Bit-for-bit exclusive or. This is ^.

fn bitwiseNot

fn bitwiseNot(): Self

Every bit flipped. This is the prefix ~.

fn shiftedLeft

fn shiftedLeft(by: Int64): Self

Shifted towards the high bits. This is <<.

fn shiftedRight

fn shiftedRight(by: Int64): Self

Shifted towards the low bits: arithmetic for a signed type, logical for an unsigned one. This is >>.

trait Integer

trait Integer with Numeric

Numeric of a whole number, with its range: Integer.minimum, Integer.maximum, and the arithmetic that answers None exactly where the operator would panic.

a + b panics on overflow in every profile, because a wrong number is worse than a stop. The members here are its total twins (CONCEPT.md, "Error Handling"): a.addedChecked(b) is a + b where that fits the type and None where it does not, and so on for -, *, / and % - the division and the remainder also answer None for a zero divisor and for the smallest value of a signed type divided by -1. They cost a comparison or two in front of the operation, which cannot overflow any more then.

Examples

const total: Int8 = 100
print total.addedChecked(27)
print total.addedChecked(28)
print(Int.maximum.multipliedChecked(2) ?? 0)

Related

const minimum

static minimum: Self

The smallest value of the type.

const maximum

static maximum: Self

The largest value of the type.

fn addedChecked

fn addedChecked(other: Self): Self?

self + other, or None where the sum does not fit the type.

fn subtractedChecked

fn subtractedChecked(other: Self): Self?

self - other, or None where the difference does not fit the type.

fn multipliedChecked

fn multipliedChecked(other: Self): Self?

self * other, or None where the product does not fit the type.

fn dividedChecked

fn dividedChecked(other: Self): Self?

self / other, or None for a zero divisor and for the smallest value of a signed type divided by -1.

fn remainderChecked

fn remainderChecked(other: Self): Self?

self % other, or None where self / other would be None as well.

type NumberParseError

type NumberParseError with Error

What Int.tryFrom(text) fails with: the text is not a valid number.

field text

text: String

The text that does not describe a number. Every conversion that fails with it names its parameter text, which is what lets a back end build the failure without knowing anything about this type (docs/BACKEND.md, "the parameter of the same name").

type NumberRangeError

type NumberRangeError with Error

What a narrowing conversion (Int8.tryFrom, Int64.tryFrom) fails with: the value does not fit the target type.

field message

message: String

What went out of range.

type Int8

native type Int8 with Signed, Hash, Bits, Power<Int64>

An 8-bit signed integer, from Int8.minimum to Int8.maximum.

type Int16

native type Int16 with Signed, Hash, Bits, Power<Int64>

A 16-bit signed integer, from Int16.minimum to Int16.maximum.

type Int32

native type Int32 with Signed, Hash, Bits, Power<Int64>

A 32-bit signed integer, from Int32.minimum to Int32.maximum.

type Int64

native type Int64 with Signed, Hash, Bits, Power<Int64>

A 64-bit signed integer, and the type of an integer literal.

type UInt8

native type UInt8 with Numeric, Hash, Bits, Power<Int64>

An 8-bit unsigned integer, from 0 to UInt8.maximum.

type UInt16

native type UInt16 with Numeric, Hash, Bits, Power<Int64>

A 16-bit unsigned integer, from 0 to UInt16.maximum.

type UInt32

native type UInt32 with Numeric, Hash, Bits, Power<Int64>

A 32-bit unsigned integer, from 0 to UInt32.maximum.

type UInt64

native type UInt64 with Numeric, Hash, Bits, Power<Int64>

A 64-bit unsigned integer, from 0 to UInt64.maximum.

extend Int8

extend Int8

The range of an Int8, and its zero and one.

const zero

static zero: Int8 = 0

Zero, the Numeric.zero of an Int8.

const one

static one: Int8 = 1

One, the Numeric.one of an Int8.

const minimum

static minimum: Int8 = -128

The smallest Int8.

const maximum

static maximum: Int8 = 127

The largest Int8.

extend Int16

extend Int16

The range of an Int16, and its zero and one.

const zero

static zero: Int16 = 0

Zero, the Numeric.zero of an Int16.

const one

static one: Int16 = 1

One, the Numeric.one of an Int16.

const minimum

static minimum: Int16 = -32768

The smallest Int16.

const maximum

static maximum: Int16 = 32767

The largest Int16.

extend Int32

extend Int32

The range of an Int32, and its zero and one.

const zero

static zero: Int32 = 0

Zero, the Numeric.zero of an Int32.

const one

static one: Int32 = 1

One, the Numeric.one of an Int32.

const minimum

static minimum: Int32 = -2147483648

The smallest Int32.

const maximum

static maximum: Int32 = 2147483647

The largest Int32.

extend UInt8

extend UInt8

The range of a UInt8, and its zero and one.

const zero

static zero: UInt8 = 0

Zero, the Numeric.zero of a UInt8.

const one

static one: UInt8 = 1

One, the Numeric.one of a UInt8.

const minimum

static minimum: UInt8 = 0

Always 0.

const maximum

static maximum: UInt8 = 255

The largest UInt8.

extend UInt16

extend UInt16

The range of a UInt16, and its zero and one.

const zero

static zero: UInt16 = 0

Zero, the Numeric.zero of a UInt16.

const one

static one: UInt16 = 1

One, the Numeric.one of a UInt16.

const minimum

static minimum: UInt16 = 0

Always 0.

const maximum

static maximum: UInt16 = 65535

The largest UInt16.

extend UInt32

extend UInt32

The range of a UInt32, and its zero and one.

const zero

static zero: UInt32 = 0

Zero, the Numeric.zero of a UInt32.

const one

static one: UInt32 = 1

One, the Numeric.one of a UInt32.

const minimum

static minimum: UInt32 = 0

Always 0.

const maximum

static maximum: UInt32 = 4294967295

The largest UInt32.

type Float32

native type Float32 with Signed

A 32-bit floating-point number.

extend Float32

extend Float32

The zero and the one of a Float32, its two infinities, and the one value for which == is never true.

const zero

static zero: Float32 = 0.0

Zero, the Numeric.zero of an Float32.

const one

static one: Float32 = 1.0

One, the Numeric.one of an Float32.

const nan

static nan: Float32 = 0.0 / 0.0

"Not a number" - the one value for which == is never true, not even with itself. See Float64.nan.

const infinity

static infinity: Float32 = 1.0 / 0.0

Larger than every finite Float32. Float32.negativeInfinity is smaller than every finite one.

const negativeInfinity

static negativeInfinity: Float32 = -1.0 / 0.0

Smaller than every finite Float32, the one value below every finite one.

fn isInfinite

fn isInfinite(): Bool

Whether the value is Float32.infinity or Float32.negativeInfinity.

fn isFinite

fn isFinite(): Bool

Whether the value is neither nan nor infinite.

type Float64

native type Float64 with Signed, Power

A 64-bit floating-point number, and the type of a decimal literal. x ** y is C's pow, so IEEE-754 through and through - (-8.0) ** (1.0 / 3.0) is nan, and nothing panics; the power by a whole number is Power<Int64> below.

type Decimal

native type Decimal with Signed, Hash

Exact base-10 arithmetic. Decimal literals adapt to it: const price: Decimal = 19.99

Open

The type checks today, but no back end gives it a value: there is no diagnostic for using it, only the absence of anything that runs it.

extend Decimal

extend Decimal

The zero and the one of a Decimal.

const zero

static zero: Decimal = 0.0

Zero, the Numeric.zero of an Decimal.

const one

static one: Decimal = 1.0

One, the Numeric.one of an Decimal.

alias Int

type Int = Int64

The default width for a whole number: Int64.

alias UInt

type UInt = UInt64

The default width for a non-negative whole number: UInt64.

alias Float

type Float = Float64

The default width for a floating-point number: Float64.

extend Int64

extend Int64

The range of an Int64, its zero and one, and reading digits in another base.

const zero

static zero: Int64 = 0

Zero, the Numeric.zero of an Int64.

const one

static one: Int64 = 1

One, the Numeric.one of an Int64.

const minimum

static minimum: Int64 = -9223372036854775807 - 1

The smallest Int64 cannot be written as a positive literal negated (9223372036854775808 alone does not fit any integer type), so it is spelled the way C spells INT64_MIN: one more than the smallest literal there is.

const maximum

static maximum: Int64 = 9223372036854775807

The largest Int64.

fn parseDigits

native static fn parseDigits(text: String, radix: Int64): Result<Int64, NumberParseError>

Digits in another base: Int.parseDigits("ff", radix: 16). No sign, no prefix, _ is allowed between digits.

extend UInt64

extend UInt64

The range of a UInt64, its zero and one, and the two operations that wrap instead of panicking.

const zero

static zero: UInt64 = 0

Zero, the Numeric.zero of a UInt64.

const one

static one: UInt64 = 1

One, the Numeric.one of a UInt64.

const minimum

static minimum: UInt64 = 0

Always 0.

const maximum

static maximum: UInt64 = ~UInt64.minimum

18446744073709551615: all ones, which is the largest UInt64 there is.

fn addedWrapping

native fn addedWrapping(other: UInt64): UInt64

Addition that wraps around instead of panicking. Together with UInt64.multipliedWrapping this is the only arithmetic in the language that does not panic on overflow, and it exists for one reason: a hash function mixes bits and needs the wrap. Everywhere else, overflow is a bug and + says so.

An integer literal is read in the type it is expected as, so the offset basis of FNV-1a, which is above the largest Int64, is written as it is:

const bytes: List<UInt64> = [104, 105]
var hash: UInt64 = 14695981039346656037
for byte in bytes {
  hash = (hash ^ byte).multipliedWrapping(1099511628211)
}
print hash

fn multipliedWrapping

native fn multipliedWrapping(other: UInt64): UInt64

Multiplication that wraps around instead of panicking. See UInt64.addedWrapping.

extend Float64

extend Float64

The constants and the operations of a Float64 that read as its own, not as a function of a value.

const zero

static zero: Float64 = 0.0

Zero, the Numeric.zero of an Float64.

const one

static one: Float64 = 1.0

One, the Numeric.one of an Float64.

const pi

static pi: Float64 = 3.141592653589793

The ratio of a circle's circumference to its diameter: the Real.pi of a Float64.

const tau

static tau: Float64 = 6.283185307179586

A whole turn in radians, twice Float64.pi: the Real.tau of a Float64.

const epsilon

static epsilon: Float64 = 0.0000000000000002220446049250313

The gap between 1.0 and the next Float64, which is two to the power of minus 52: its Real.epsilon.

const e

static e: Float64 = 2.718281828459045

The base of the natural logarithm.

const nan

static nan: Float64 = 0.0 / 0.0

"Not a number" - the one value for which == is never true, not even with itself (Float64.isNaN). Named the way Real.pi is: the compile-time constant evaluator (ir/constant.trb) only allows this one declaration and Float32.nan to fold to it, because an ordinary expression that produces nan is a mistake and stays an error (docs/language/modules-and-packages/top-level-code.md, rule 6).

const infinity

static infinity: Float64 = 1.0 / 0.0

Larger than every finite Float64. Float64.negativeInfinity is smaller than every finite one.

const negativeInfinity

static negativeInfinity: Float64 = -1.0 / 0.0

Smaller than every finite Float64, the one value below every finite one.

fn squareRoot

native fn squareRoot(): Float64

The non-negative square root.

fn floor

native fn floor(): Float64

Rounded towards negative infinity.

fn ceiling

native fn ceiling(): Float64

Rounded towards positive infinity.

fn round

native fn round(): Float64

Rounded to the nearest integer.

fn isNaN

native fn isNaN(): Bool

Whether the value is "not a number" - the one value for which == is never true, not even with itself.

fn isInfinite

fn isInfinite(): Bool

Whether the value is Float64.infinity or Float64.negativeInfinity.

fn isFinite

fn isFinite(): Bool

Whether the value is neither nan nor infinite.

fn isCloseTo

fn isCloseTo(other: Float64, tolerance: Float64 = 0.000001): Bool

Floats are compared with a tolerance: assert(length.isCloseTo(1.0))

The exact comparisons a float has are deliberately different from each other, because IEEE-754 equality is not an equivalence relation and IEEE-754 ordering is not a total order:

  • Every operator is IEEE-754: ==, <, <=, > and >=. nan != nan, every comparison with a nan on either side is false, and 0.0 == -0.0.
  • compare is a total order: nan is above everything, and -0.0 compares equal to 0.0. So sorted terminates whatever pivot it picks, whatever is in the list.
  • A float is therefore the one type where an operator and the member behind it disagree, which is why everything that orders values calls compare and never writes <=: an IEEE <= would leave a nan where it started.
  • Float32 and Float64 are not Hash, so a float is never a Map key and the nan key does not exist.

extend Float64 with Real

extend Float64 with Real

Float64 is the fast Real: the square root, the rounding and isCloseTo are its own methods already, and the trigonometry, the exponential and the logarithm are the runtime's thin wrappers over <math.h>.

Pitfalls

  • What sine answers is what the platform's mathematics library answers, so two machines may differ in the last bit. Fixed (std/linear) is the implementor that does not.

fn sine

native fn sine(): Float64

The sine of an angle in radians.

fn cosine

native fn cosine(): Float64

The cosine of an angle in radians.

fn tangent

native fn tangent(): Float64

The tangent of an angle in radians.

fn arcSine

fn arcSine(): Float64

In radians, in [-pi/2, pi/2]; nan answers nan.

Panics

Outside [-1, 1], where no angle has this sine, exactly as Fixed.arcSine of std/linear does: the two implementors of Real agree about every value both of them hold (docs/design/LINEAR.md, "Numeric policy"). A ratio that rounding pushed a hair past one is clamped at the call, ratio.clamp(-1.0, 1.0).arcSine(), which is the total form.

fn arcCosine

fn arcCosine(): Float64

In radians, in [0, pi]; nan answers nan.

Panics

Outside [-1, 1], where no angle has this cosine, as Float64.arcSine says.

fn arcTangent

native fn arcTangent(): Float64

In radians, in (-pi/2, pi/2).

fn arcTangentDivided

native fn arcTangentDivided(by: Float64): Float64

The angle of the point (by, self), which is C's atan2(self, by).

fn exponential

native fn exponential(): Float64

e raised to this value. An infinity where it does not fit, and never a panic.

fn naturalLogarithm

native fn naturalLogarithm(): Float64

The logarithm to the base e: -infinity at zero, nan below it, and never a panic.

fn halved

fn halved(): Float64

fn radiansOfDegrees

fn radiansOfDegrees(): Float64

fn degreesOfRadians

fn degreesOfRadians(): Float64

extend Float64 with Power<Int64>

extend Float64 with Power<Int64>

x ** n for a whole exponent: 2.0 ** 10 and 10.0 ** -3. It is pow with the exponent as a Float64 rather than repeated squaring, because pow rounds once where squaring rounds at every step, and because squaring towards a negative power overflows to an infinity before it divides.

Pitfalls

  • An exponent beyond 2 ** 53 in magnitude has no exact Float64, and every Float64 that large is even. The sign an odd exponent gives a negative base is therefore put back by hand; the magnitude of such a power is an infinity, a zero or one anyway.

fn power

fn power(exponent: Int64): Float64

extend Int64 with From<Int8>

extend Int64 with From<Int8>

A narrower signed integer always fits: Int.from(byte) for an Int8.

fn from

native static fn from(value: Int8): Int64

extend Int64 with From<Int16>

extend Int64 with From<Int16>

A narrower signed integer always fits: Int.from(short) for an Int16.

fn from

native static fn from(value: Int16): Int64

extend Int64 with From<Int32>

extend Int64 with From<Int32>

A narrower signed integer always fits: Int.from(word) for an Int32.

fn from

native static fn from(value: Int32): Int64

extend Int64 with From<UInt8>

extend Int64 with From<UInt8>

Every unsigned value below the width of an Int64 fits into it: Int.from(byte) for a byte of a String.

fn from

native static fn from(value: UInt8): Int64

extend Int64 with From<UInt16>

extend Int64 with From<UInt16>

Every unsigned value below the width of an Int64 fits into it.

fn from

native static fn from(value: UInt16): Int64

extend Int64 with From<UInt32>

extend Int64 with From<UInt32>

Every unsigned value below the width of an Int64 fits into it.

fn from

native static fn from(value: UInt32): Int64

extend Float64 with From<Int64>

extend Float64 with From<Int64>

Every Int64 is representable as a Float64.

fn from

native static fn from(value: Int64): Float64

extend Float64 with From<Float32>

extend Float64 with From<Float32>

Every Float32 widens to Float64 without loss.

fn from

native static fn from(value: Float32): Float64

extend Int8 with TryFrom<String, NumberParseError>

extend Int8 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a whole number, or does not fit an Int8.

fn tryFrom

native static fn tryFrom(text: String): Result<Int8, NumberParseError>

extend Int16 with TryFrom<String, NumberParseError>

extend Int16 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a whole number, or does not fit an Int16.

fn tryFrom

native static fn tryFrom(text: String): Result<Int16, NumberParseError>

extend Int32 with TryFrom<String, NumberParseError>

extend Int32 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a whole number, or does not fit an Int32.

fn tryFrom

native static fn tryFrom(text: String): Result<Int32, NumberParseError>

extend Int64 with TryFrom<String, NumberParseError>

extend Int64 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a whole number, or does not fit an Int64.

fn tryFrom

native static fn tryFrom(text: String): Result<Int64, NumberParseError>

extend UInt8 with TryFrom<String, NumberParseError>

extend UInt8 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a non-negative whole number, or does not fit a UInt8.

fn tryFrom

native static fn tryFrom(text: String): Result<UInt8, NumberParseError>

extend UInt16 with TryFrom<String, NumberParseError>

extend UInt16 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a non-negative whole number, or does not fit a UInt16.

fn tryFrom

native static fn tryFrom(text: String): Result<UInt16, NumberParseError>

extend UInt32 with TryFrom<String, NumberParseError>

extend UInt32 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a non-negative whole number, or does not fit a UInt32.

fn tryFrom

native static fn tryFrom(text: String): Result<UInt32, NumberParseError>

extend UInt64 with TryFrom<String, NumberParseError>

extend UInt64 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a non-negative whole number, or does not fit a UInt64.

fn tryFrom

native static fn tryFrom(text: String): Result<UInt64, NumberParseError>

extend Float32 with TryFrom<String, NumberParseError>

extend Float32 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a decimal number.

fn tryFrom

native static fn tryFrom(text: String): Result<Float32, NumberParseError>

extend Float64 with TryFrom<String, NumberParseError>

extend Float64 with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a decimal number.

fn tryFrom

native static fn tryFrom(text: String): Result<Float64, NumberParseError>

extend Decimal with TryFrom<String, NumberParseError>

extend Decimal with TryFrom<String, NumberParseError>

Fails with a NumberParseError when the text is not a decimal number.

fn tryFrom

native static fn tryFrom(text: String): Result<Decimal, NumberParseError>

extend Int8 with TryFrom<Int64, NumberRangeError>

extend Int8 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 does not fit an Int8.

fn tryFrom

native static fn tryFrom(value: Int64): Result<Int8, NumberRangeError>

extend Int16 with TryFrom<Int64, NumberRangeError>

extend Int16 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 does not fit an Int16.

fn tryFrom

native static fn tryFrom(value: Int64): Result<Int16, NumberRangeError>

extend Int32 with TryFrom<Int64, NumberRangeError>

extend Int32 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 does not fit an Int32.

fn tryFrom

native static fn tryFrom(value: Int64): Result<Int32, NumberRangeError>

extend UInt8 with TryFrom<Int64, NumberRangeError>

extend UInt8 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 is negative or above 255: the one way from a number to a byte.

Examples

const byte: Result<UInt8, NumberRangeError> = UInt8.tryFrom 200
print byte.isOk()

fn tryFrom

native static fn tryFrom(value: Int64): Result<UInt8, NumberRangeError>

extend UInt16 with TryFrom<Int64, NumberRangeError>

extend UInt16 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 is negative or does not fit a UInt16.

fn tryFrom

native static fn tryFrom(value: Int64): Result<UInt16, NumberRangeError>

extend UInt32 with TryFrom<Int64, NumberRangeError>

extend UInt32 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 is negative or does not fit a UInt32.

fn tryFrom

native static fn tryFrom(value: Int64): Result<UInt32, NumberRangeError>

extend UInt64 with TryFrom<Int64, NumberRangeError>

extend UInt64 with TryFrom<Int64, NumberRangeError>

Fails with a NumberRangeError when the Int64 is negative: every other one fits a UInt64.

fn tryFrom

native static fn tryFrom(value: Int64): Result<UInt64, NumberRangeError>

extend Int64 with TryFrom<UInt64, NumberRangeError>

extend Int64 with TryFrom<UInt64, NumberRangeError>

Fails with a NumberRangeError when the UInt64 is above the largest Int64.

fn tryFrom

native static fn tryFrom(value: UInt64): Result<Int64, NumberRangeError>

extend Int64 with TryFrom<Float64, NumberRangeError>

extend Int64 with TryFrom<Float64, NumberRangeError>

Fails with a NumberRangeError when the Float64 does not fit an Int64, e.g. nan or a value out of range.

fn tryFrom

native static fn tryFrom(value: Float64): Result<Int64, NumberRangeError>

extend Int64 with From<Char>

extend Int64 with From<Char>

The code point: Int.from('A') is 65. It is here and not with Char, because an implementation belongs to the package of the type or to the package of the trait, and Int64 is this package's. The other direction (Char.tryFrom(65)) is std/text's for the same reason.

fn from

native static fn from(value: Char): Int64