Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reading floating-point text

Floating-point parsing accepts decimal/exponent forms and surrounding whitespace. Overflow returns OutOfRange; underflow can round to zero. Explicit inf, infinity, and nan are accepted case-insensitively, with optional signs.

def main() -> Result[(), AllocError | IoError]:
    print(str.repr(f32.parse("1.25e2"))?)?
    print(str.repr(f32.parse("1e100"))?)?
    print(str.repr(f64.parse("-0.0"))?)?
    Ok(())
Result[f32, ParseError].Ok(125.0)
Result[f32, ParseError].Err(ParseError.OutOfRange)
Result[f64, ParseError].Ok(-0.0)