stencillogic / astro-float

Arbitrary precision floating point numbers library
MIT License
101 stars 4 forks source link

Subtraction gives wrong result #33

Open dzamkov opened 3 months ago

dzamkov commented 3 months ago

I'm using astro_float to validate function approximations in my code. I noticed that one of my tests fails with a massive discrepancy in computed results. Here's a minimal example which reproduces the issue:

use astro_float::ctx::Context;
use astro_float::{expr, Consts, RoundingMode};

fn main() {
    let mut ctx = Context::new(
        1024,
        RoundingMode::Down,
        Consts::new().expect("constants cache initialized"),
        -10000,
        10000,
    );
    let z = &astro_float::BigFloat::from(-2.0);
    let d = &astro_float::BigFloat::from(1.0e-64);
    println!("{}", expr!(z - d, ctx));
}

Output:

-2.00000000000000000000000000000000999999999999999965305738833546928712902976607280783480372213247877639491996226104668964320054101346916438695416432929769423252076208907803604252402073697828855693148231154054883518256247043609619140625e-32

Note the exponent: e-32. The exponent should be e+0. Interestingly, this gives the correct result when using a precision of 64, 128, 256 and 512, but it fails with 1024.

stencillogic commented 3 months ago

Seems like a bug in decimal representation of the result. Thanks for reporting.