Nanyx
PlaygroundPackages

prelude: conversions

Converting between int, float and the sized number types

Conversions between int, float and the sized number types, and reading the sized number types from text. They are in the prelude, so no import is needed. A conversion that cannot hold every value gives back #outOfRange for one it cannot.

Reference

int.toI8

def int.toI8: int -> #some(I8) | #outOfRange

int.toI16

def int.toI16: int -> #some(I16) | #outOfRange

int.toI32

def int.toI32: int -> #some(I32) | #outOfRange

int.toU8

def int.toU8: int -> #some(U8) | #outOfRange

int.toU16

def int.toU16: int -> #some(U16) | #outOfRange

int.toU32

def int.toU32: int -> #some(U32) | #outOfRange

int.toU64

def int.toU64: int -> #some(U64) | #outOfRange

int.toF32

def int.toF32: int -> F32

I8.toInt

def I8.toInt: I8 -> int

I8.toI16

def I8.toI16: I8 -> I16

I8.toI32

def I8.toI32: I8 -> I32

I8.toU8

def I8.toU8: I8 -> #some(U8) | #outOfRange

I8.toU16

def I8.toU16: I8 -> #some(U16) | #outOfRange

I8.toU32

def I8.toU32: I8 -> #some(U32) | #outOfRange

I8.toU64

def I8.toU64: I8 -> #some(U64) | #outOfRange

I8.toFloat

def I8.toFloat: I8 -> float

I8.toF32

def I8.toF32: I8 -> F32

I16.toInt

def I16.toInt: I16 -> int

I16.toI8

def I16.toI8: I16 -> #some(I8) | #outOfRange

I16.toI32

def I16.toI32: I16 -> I32

I16.toU8

def I16.toU8: I16 -> #some(U8) | #outOfRange

I16.toU16

def I16.toU16: I16 -> #some(U16) | #outOfRange

I16.toU32

def I16.toU32: I16 -> #some(U32) | #outOfRange

I16.toU64

def I16.toU64: I16 -> #some(U64) | #outOfRange

I16.toFloat

def I16.toFloat: I16 -> float

I16.toF32

def I16.toF32: I16 -> F32

I32.toInt

def I32.toInt: I32 -> int

I32.toI8

def I32.toI8: I32 -> #some(I8) | #outOfRange

I32.toI16

def I32.toI16: I32 -> #some(I16) | #outOfRange

I32.toU8

def I32.toU8: I32 -> #some(U8) | #outOfRange

I32.toU16

def I32.toU16: I32 -> #some(U16) | #outOfRange

I32.toU32

def I32.toU32: I32 -> #some(U32) | #outOfRange

I32.toU64

def I32.toU64: I32 -> #some(U64) | #outOfRange

I32.toFloat

def I32.toFloat: I32 -> float

I32.toF32

def I32.toF32: I32 -> F32

U8.toInt

def U8.toInt: U8 -> int

U8.toI8

def U8.toI8: U8 -> #some(I8) | #outOfRange

U8.toI16

def U8.toI16: U8 -> I16

U8.toI32

def U8.toI32: U8 -> I32

U8.toU16

def U8.toU16: U8 -> U16

U8.toU32

def U8.toU32: U8 -> U32

U8.toU64

def U8.toU64: U8 -> U64

U8.toFloat

def U8.toFloat: U8 -> float

U8.toF32

def U8.toF32: U8 -> F32

U16.toInt

def U16.toInt: U16 -> int

U16.toI8

def U16.toI8: U16 -> #some(I8) | #outOfRange

U16.toI16

def U16.toI16: U16 -> #some(I16) | #outOfRange

U16.toI32

def U16.toI32: U16 -> I32

U16.toU8

def U16.toU8: U16 -> #some(U8) | #outOfRange

U16.toU32

def U16.toU32: U16 -> U32

U16.toU64

def U16.toU64: U16 -> U64

U16.toFloat

def U16.toFloat: U16 -> float

U16.toF32

def U16.toF32: U16 -> F32

U32.toInt

def U32.toInt: U32 -> int

U32.toI8

def U32.toI8: U32 -> #some(I8) | #outOfRange

U32.toI16

def U32.toI16: U32 -> #some(I16) | #outOfRange

U32.toI32

def U32.toI32: U32 -> #some(I32) | #outOfRange

U32.toU8

def U32.toU8: U32 -> #some(U8) | #outOfRange

U32.toU16

def U32.toU16: U32 -> #some(U16) | #outOfRange

U32.toU64

def U32.toU64: U32 -> U64

U32.toFloat

def U32.toFloat: U32 -> float

U32.toF32

def U32.toF32: U32 -> F32

U64.toInt

def U64.toInt: U64 -> #some(int) | #outOfRange

U64.toI8

def U64.toI8: U64 -> #some(I8) | #outOfRange

U64.toI16

def U64.toI16: U64 -> #some(I16) | #outOfRange

U64.toI32

def U64.toI32: U64 -> #some(I32) | #outOfRange

U64.toU8

def U64.toU8: U64 -> #some(U8) | #outOfRange

U64.toU16

def U64.toU16: U64 -> #some(U16) | #outOfRange

U64.toU32

def U64.toU32: U64 -> #some(U32) | #outOfRange

U64.toFloat

def U64.toFloat: U64 -> float

U64.toF32

def U64.toF32: U64 -> F32

float.toF32

def float.toF32: float -> F32

x as the nearest F32, or an infinity past the largest F32.

F32.toFloat

def F32.toFloat: F32 -> float

x as a float, which holds every F32 exactly.

float.toInt

def float.toInt: float, #truncate | #round | #floor | #ceil -> int

x as an int, made whole as mode says: float.toInt(2.5, #round) is 3. A value that is NaN, infinite or past the range of int stops the program; see float.tryToInt.

float.tryToInt

def float.tryToInt: float, #truncate | #round | #floor | #ceil -> #some(int) | #outOfRange

x as an int, made whole as mode says, or #outOfRange when it is NaN, infinite or past the range of int.

F32.toInt

def F32.toInt: F32, #truncate | #round | #floor | #ceil -> int

x as an int, as float.toInt makes it one.

F32.tryToInt

def F32.tryToInt: F32, #truncate | #round | #floor | #ceil -> #some(int) | #outOfRange

x as an int, or #outOfRange, as float.tryToInt makes it one.

float.format

def float.format: (float, decimals: int) -> string

x in decimal with decimals digits after the point, rounded to the nearest such decimal with halves away from zero, as float.toInt rounds with #round: float.format(2.5, decimals = 0) is "3" and float.format(-1.875, decimals = 2) is "-1.88". It is how an interpolation hole writes a float to a given precision: "{price \float.format(decimals = 2)}".

It rounds x as it is held, which is often a little above or below the decimal it was written as: 1.005 is held as 1.00499999…, so with 2 decimals it is "1.00". Digits past the 15th or so significant one are written as zeros. A value that rounds to zero has no sign: -0.001 with 2 decimals is "0.00". With decimals of 0 or less there is no point. NaN, the infinities and a number past the range of int are written as float.toString writes them.

I8.fromString

def I8.fromString: string -> #some(I8) | #invalidI8

The I8 s writes in decimal, or #invalidI8 for anything else, a number past the range of I8 included.

I16.fromString

def I16.fromString: string -> #some(I16) | #invalidI16

The I16 s writes in decimal, or #invalidI16 for anything else, a number past the range of I16 included.

I32.fromString

def I32.fromString: string -> #some(I32) | #invalidI32

The I32 s writes in decimal, or #invalidI32 for anything else, a number past the range of I32 included.

U8.fromString

def U8.fromString: string -> #some(U8) | #invalidU8

The U8 s writes in decimal, or #invalidU8 for anything else, a leading - or a number past the range of U8 included.

U16.fromString

def U16.fromString: string -> #some(U16) | #invalidU16

The U16 s writes in decimal, or #invalidU16 for anything else, a leading - or a number past the range of U16 included.

U32.fromString

def U32.fromString: string -> #some(U32) | #invalidU32

The U32 s writes in decimal, or #invalidU32 for anything else, a leading - or a number past the range of U32 included.

U64.fromString

def U64.fromString: string -> #some(U64) | #invalidU64

The U64 s writes in decimal, with an optional leading +, or #invalidU64 for anything else, a leading - or a number past 18446744073709551615 included. It reads its digits itself, as the largest U64s are past the range of int.

F32.fromString

def F32.fromString: string -> #some(F32) | #invalidF32

The F32 nearest the float s writes, as float.fromString reads it, or #invalidF32 for anything else. A number too large for an F32 is infinity.