Skip to content

Wrong value with very long floats #4

Closed
@AnttiParaoanu

Description

@AnttiParaoanu

For some floats with a long trail of zeroes, the value is not correctly rounded after a sufficient amount of zeroes.

Code:

use fast_float::*;

fn main() {
    let base = "1.1754941406275178592461758986628081843312458647327962400313859427181746759860647699724722770042717456817626953125";
    let mut a = String::from(base);
    let mut b = a.clone();
    a.push_str(&"0".repeat(655));
    a.push_str("e-38");
    b.push_str(&"0".repeat(656));
    b.push_str("e-38");
    let a = parse::<f32, &str>(&a).unwrap();
    let b = parse::<f32, &str>(&b).unwrap();
    println!("a: {:x}", a.to_bits());
    println!("b: {:x}", b.to_bits());
}

Output:

a: 7ffffe
b: 7fffff

Works in debug and release modes

rustc --version --verbose

rustc 1.47.0-nightly (6c8927b0c 2020-07-26)
binary: rustc
commit-hash: 6c8927b0cf80ceee19386026cf9d7fd4fd9d486f
commit-date: 2020-07-26
host: x86_64-unknown-linux-gnu
release: 1.47.0-nightly
LLVM version: 10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions