cmpute / dashu

A library set of arbitrary precision numbers implemented in Rust.
Apache License 2.0
77 stars 9 forks source link

[help] Float Addition #28

Closed ryanrossiter closed 1 year ago

ryanrossiter commented 1 year ago

This is such a basic case that I assume I've just missed something obvious, but I can't seem to get FBig addition to work.

Here is my test case:

let a = FBig::<Zero, 10>::from(ubig!(10));
let b = FBig::<Zero, 10>::from(ubig!(5));
println!("{} {}", a, b);
println!("{}", a + b);

Which produces:

10 5
10

Even more confusingly, when I change the base to 2 the result becomes 14 (1110):

let a = FBig::<Zero, 2>::from(ubig!(10));
let b = FBig::<Zero, 2>::from(ubig!(5));
println!("{} {}", a, b);
println!("{}", a + b);

Output:

1010 101
1110

What am I missing here? Thanks in advance

ryanrossiter commented 1 year ago

Ah, it seems From<IBig> for FBig sets the precision to the number of digits in the IBig, which is applied to the addition, and since 10 only has 1 non-zero digit the precision is 1?

cmpute commented 1 year ago

Yes, that's related to the precision. But even in decimal, 10 should have a precision of 2 (two digits). This should be a bug, thanks for reporting this, I will check it later.

cmpute commented 1 year ago

I have pushed the fix for this problem. I will try to publish a new version for this in a few days.

cmpute commented 1 year ago

A new version of dashu-float (v0.3.2) is published. Feel free to reopen if the problem still exists.