Parsing floats v0.1.0
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.
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.
value and overflowed must be non-null and writable.
Parameters
| Name | Type | Description |
|---|---|---|
digits | &FixedNat | the retained significant digits |
exponent | int | the power of ten they are scaled by |
truncated | bool | whether a nonzero digit was dropped past the retained bound |
negative | bool | whether the value is negative |
value | *var float64 | receives the float |
overflowed | *var bool | receives 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.
value and overflowed must be non-null and writable.
Parameters
| Name | Type | Description |
|---|---|---|
digits | &FixedNat | the retained significant digits |
exponent | int | the power of ten they are scaled by |
truncated | bool | whether a nonzero digit was dropped past the retained bound |
negative | bool | whether the value is negative |
value | *var float32 | receives the float |
overflowed | *var bool | receives 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.
Fields
| Name | Type | Description |
|---|---|---|
digits | FixedNat | The retained significant digits, as an integer. |
exponent | int | The power of ten they are scaled by, counting the decimal point and any digits dropped past the bound. |
significant | uint | How many significant digits were retained. |
truncated | bool | Whether a nonzero digit was dropped past the retained bound. |
negative | bool | Whether the value is negative, including a negative zero. |
special | int | Zero for an ordinary number, one for NaN, two for an infinity. |
ScanDecimal
pub func ScanDecimal(
str: char8[..],
reading: *var DecimalReading
) throws 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 and InvalidCharacter at the byte where the shape stopped matching.
reading must be non-null and writable; after a failure it holds an empty reading.
Parameters
| Name | Type | Description |
|---|---|---|
str | char8[..] | the text to read |
reading | *var DecimalReading | receives what the decimal spells |
Returns: success when the whole text was the grammar
ParseFloat64
pub func ParseFloat64(str: char8[..]) -> float64 throws ParseError
pub func ParseFloat64(str: StringView) -> float64 throws 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 and InvalidCharacter 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.
| Overload | Summary |
|---|---|
pub func ParseFloat64(str: char8[..]) -> float64 throws ParseError | Reads str as the float64 nearest what it spells, rounding to nearest with ties to even. |
pub func ParseFloat64(str: StringView) -> float64 throws ParseError | Provides the public ParseFloat64 operation. |
Parameters
| Name | Type | Description |
|---|---|---|
str | char8[..] / StringView | the text to read |
Returns: the value, or why the text is not one
ParseFloat32
pub func ParseFloat32(str: char8[..]) -> float32 throws ParseError
pub func ParseFloat32(str: StringView) -> float32 throws 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.
| Overload | Summary |
|---|---|
pub func ParseFloat32(str: char8[..]) -> float32 throws ParseError | Reads str as the float32 nearest what it spells, rounding to nearest with ties to even. |
pub func ParseFloat32(str: StringView) -> float32 throws ParseError | The same grammar for a view. |
Parameters
| Name | Type | Description |
|---|---|---|
str | char8[..] / StringView | the text to read |
Returns: the value, or why the text is not one
ParseWideFloat
pub func ParseWideFloat(
allocator: Allocator,
str: char8[..],
layout: &FloatLayout
) -> uint512 throws 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.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the working storage comes from |
str | char8[..] | the text to parse |
layout | &FloatLayout | which 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 throws ParseError
Reads str as an x87 extended value.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the working storage comes from |
str | char8[..] | 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 throws ParseError
Reads str as a binary128 value.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the working storage comes from |
str | char8[..] | 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 throws ParseError
Reads str as a binary256 value.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the working storage comes from |
str | char8[..] | 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 throws ParseError
Reads str as a binary512 value.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the working storage comes from |
str | char8[..] | the text to parse |
Returns: the value, or the failure and the byte offset it was found at