Parsing floats v0.1.0

Source
Reading decimal text into the nearest float32, float64 or wide float, exactly.

RetainedDecimalDigits

pub const RetainedDecimalDigits: uint = 768;

How many significant digits a narrow float reader keeps before the rest becomes a sticky bit. Seven hundred and sixty-eight, which is the longest exact decimal expansion any midpoint between two adjacent float64 values has. Keeping that many means a tie is detected exactly, and a nonzero digit beyond them means the value cannot be a tie at all — so the bound is where the two arguments meet rather than a round number.

Source

ExactFloat64

pub func ExactFloat64(
    digits: &FixedNat,
    exponent: int,
    truncated: bool,
    negative: bool,
    value: *var float64,
    overflowed: *var bool
) -> bool

Reads the decimal digits * 10^exponent into the nearest float64, with ties to even. Answers false for a value past the format's range or a workspace failure, with overflowed saying which.

Source

Parameters

NameTypeDescription
digits&FixedNatthe retained significant digits
exponentintthe power of ten they are scaled by
truncatedboolwhether a nonzero digit was dropped past the retained bound
negativeboolwhether the value is negative
value*var float64receives the float
overflowed*var boolreceives whether the failure was the value being too large

Returns: true when the value was read

ExactFloat32

pub func ExactFloat32(
    digits: &FixedNat,
    exponent: int,
    truncated: bool,
    negative: bool,
    value: *var float32,
    overflowed: *var bool
) -> bool

Reads the decimal digits * 10^exponent into the nearest float32, with ties to even. Read at this width's own precision rather than at a float64's and narrowed: narrowing rounds a second time, and a value that lands on a float32 boundary can round the wrong way when it has already been rounded to fifty-three bits on the way. Answers false for a value past the format's range or a workspace failure, with overflowed saying which.

Source

Parameters

NameTypeDescription
digits&FixedNatthe retained significant digits
exponentintthe power of ten they are scaled by
truncatedboolwhether a nonzero digit was dropped past the retained bound
negativeboolwhether the value is negative
value*var float32receives the float
overflowed*var boolreceives whether the failure was the value being too large

Returns: true when the value was read

DecimalReading

pub struct DecimalReading {
    pub digits: FixedNat;
    pub exponent: int;
    pub significant: uint;
    pub truncated: bool;
    pub negative: bool;
    pub special: int;
}

What a decimal spells, before any format decides what to do with it. Copy, holding the retained digits and the two flags the rounding needs.

Source

Fields

NameTypeDescription
digitsFixedNatThe retained significant digits, as an integer.
exponentintThe power of ten they are scaled by, counting the decimal point and any digits dropped past the bound.
significantuintHow many significant digits were retained.
truncatedboolWhether a nonzero digit was dropped past the retained bound.
negativeboolWhether the value is negative, including a negative zero.
specialintZero for an ordinary number, one for NaN, two for an infinity.

ScanDecimal

pub func ScanDecimal(
    str: char8[..],
    reading: *var DecimalReading
) -> ! ParseError

Reads the decimal grammar out of str, without deciding what float it names. One scanner for both narrow widths, so what one reads the other reads: the grammar is [+-]?(digits[.digits?]?|.digits)([eE][+-]?digits)?, plus the two special spellings NaN and Inf exactly as formatting writes them. The digits are kept exactly up to RetainedDecimalDigits, past which a nonzero one sets truncated and moves the exponent instead. Reports Empty for no bytes, InvalidCharacter at the byte where the shape stopped matching, and OutOfMemory where the retained digits would not fit the workspace.

Source

Parameters

NameTypeDescription
strchar8[..]the text to read
reading*var DecimalReadingreceives what the decimal spells

Returns: success when the whole text was the grammar

ParseFloat64

pub func ParseFloat64(str: char8[..]) -> float64 ! ParseError
pub func ParseFloat64(str: StringView) -> float64 ! ParseError

Reads str as the float64 nearest what it spells, rounding to nearest with ties to even. Exact: the value is formed as a ratio of integers and the quotient is taken with a sticky bit, so what comes back is the nearest float64 and never one step off it. NaN, Inf and -Inf are read exactly as formatting writes them. Reports Empty, InvalidCharacter and OutOfMemory as ScanDecimal does, and Overflow at the text's length for a value past the largest finite float64 — refused rather than rounded to an infinity, since that would turn a number the caller wrote into one they did not. A value below the smallest subnormal is the signed zero it rounds to.

OverloadSummary
pub func ParseFloat64(str: char8[..]) -> float64 ! ParseErrorReads str as the float64 nearest what it spells, rounding to nearest with ties to even.
pub func ParseFloat64(str: StringView) -> float64 ! ParseErrorProvides the public ParseFloat64 operation.

Source

Parameters

NameTypeDescription
strchar8[..] / StringViewthe text to read

Returns: the value, or why the text is not one

ParseFloat32

pub func ParseFloat32(str: char8[..]) -> float32 ! ParseError
pub func ParseFloat32(str: StringView) -> float32 ! ParseError

Reads str as the float32 nearest what it spells, rounding to nearest with ties to even. Read at this width's own precision rather than at a float64's and narrowed. Narrowing rounds a second time, and a value that lands on a float32 boundary can round the wrong way once it has already been rounded to fifty-three bits on the way there. The failures ParseFloat64 reports, at this width's range: Overflow for a value past the largest finite float32, and the signed zero for one below the smallest subnormal.

OverloadSummary
pub func ParseFloat32(str: char8[..]) -> float32 ! ParseErrorReads str as the float32 nearest what it spells, rounding to nearest with ties to even.
pub func ParseFloat32(str: StringView) -> float32 ! ParseErrorThe same grammar for a view.

Source

Parameters

NameTypeDescription
strchar8[..] / StringViewthe text to read

Returns: the value, or why the text is not one

ParseWideFloat

pub func ParseWideFloat(
    allocator: Allocator,
    str: char8[..],
    layout: &FloatLayout
) -> uint512 ! ParseError

Reads str as a value under layout, rounding to nearest with ties to even, and reports the bits. Overflow is refused rather than rounded to infinity, as ParseFloat64 refuses it; a value too small for the format becomes the signed zero. NaN, Inf and -Inf are read exactly as formatting writes them.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the working storage comes from
strchar8[..]the text to parse
layout&FloatLayoutwhich wide floating-point format the bits are in

Returns: the bit pattern, or the failure and where it was found

ParseFloat80

pub func ParseFloat80(
    allocator: Allocator,
    str: char8[..]
) -> Float80 ! ParseError

Reads str as an x87 extended value.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the working storage comes from
strchar8[..]the text to parse

Returns: the value, or the failure and the byte offset it was found at

ParseFloat128

pub func ParseFloat128(
    allocator: Allocator,
    str: char8[..]
) -> Float128 ! ParseError

Reads str as a binary128 value.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the working storage comes from
strchar8[..]the text to parse

Returns: the value, or the failure and the byte offset it was found at

ParseFloat256

pub func ParseFloat256(
    allocator: Allocator,
    str: char8[..]
) -> Float256 ! ParseError

Reads str as a binary256 value.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the working storage comes from
strchar8[..]the text to parse

Returns: the value, or the failure and the byte offset it was found at

ParseFloat512

pub func ParseFloat512(
    allocator: Allocator,
    str: char8[..]
) -> Float512 ! ParseError

Reads str as a binary512 value.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the working storage comes from
strchar8[..]the text to parse

Returns: the value, or the failure and the byte offset it was found at