rust-lexical icon indicating copy to clipboard operation
rust-lexical copied to clipboard

[BUG] Assertion failure on parsing hexadecimal floats

Open xTibor opened this issue 2 years ago • 1 comments

Description

When parsing C-style hexadecimal floats (C_HEX_STRING/HEX_FLOAT) Lexical currently throws the following assertion failure in debug mode:

thread 'main' panicked at 'assertion failed: format.mantissa_radix() == format.exponent_base()', /home/tibor/.cargo/registry/src/github.com-1ecc6299db9ec823/lexical-parse-float-0.8.5/src/number.rs:60:9

Prerequisites

  • Rust version: rustc 1.64.0-nightly (3924dac7b 2022-07-29)
  • lexical version: 6.1.1
  • lexical compilation features used: ["parse-floats", "radix", "power-of-two", "format"]

Test case

fn main() {
    let c_hex_float = "0x12.34p5"; // 582.5 in decimal

    const FORMAT: u128 = lexical::format::C_HEX_STRING;
    let options = &lexical::parse_float_options::HEX_FLOAT;

    let (result, _) =
        lexical::parse_partial_with_options::<f32, _, FORMAT>(c_hex_float, options).unwrap();

    println!("{}: {}", c_hex_float, result);
}

Additional Context

I encountered this issue while working on reimplementing the strtof and strtod functions in Relibc (Redox C library) using Lexical.

xTibor avatar Jul 30 '22 19:07 xTibor

I cam across the same error while trying to make a parser for Lua. is there any workaround for the p exponent ?

fn main() {
    let options = unsafe {
        lexical::ParseFloatOptionsBuilder::new()
            .exponent('p' as u8)
            .build_unchecked()
    };

    assert_eq!(
        1.0,
        lexical::parse_with_options::<_, _, { lexical::format::C18_HEX_LITERAL }>("4P-2", &options)
            .expect("failed to parse")
    )
}

debug panic:

thread 'main' panicked at 'assertion failed: format.mantissa_radix() == format.exponent_base()',
	 /.../lexical-parse-float-0.8.5/src/number.rs:60:9

release wrong result:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `1.0`,
 right: `0.015625`', src/main.rs:8:5

coderedart avatar Oct 10 '22 17:10 coderedart