Skip to content

Commit

Permalink
Fix round() losing precision on bigints
Browse files Browse the repository at this point in the history
The original issue can be reproduced with `X is round(2 ^ 54 + 1) - 2 ^ 54, X = 1.`
  • Loading branch information
adri326 committed Jan 17, 2025
1 parent e9f8ce6 commit a739390
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/machine/arithmetic_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,16 +1067,14 @@ pub(crate) fn truncate(n: Number, arena: &mut Arena) -> Number {
}
}

pub(crate) fn round(n: Number, arena: &mut Arena) -> Result<Number, MachineStubGen> {
let stub_gen = || {
let is_atom = atom!("is");
functor_stub(is_atom, 2)
pub(crate) fn round(num: Number, arena: &mut Arena) -> Result<Number, MachineStubGen> {
let res = match num {
Number::Fixnum(_) | Number::Integer(_) => num,
Number::Rational(rat) => Number::arena_from(rat.round(), arena),
Number::Float(f) => Number::Float(OrderedFloat((*f).round())),
};

let result = add(n, Number::Float(OrderedFloat(0.5f64)), arena);
let result = try_numeric_result!(result, stub_gen)?;

Ok(floor(result, arena))
Ok(rnd_i(&res, arena))
}

pub(crate) fn bitwise_complement(n1: Number, arena: &mut Arena) -> Result<Number, MachineStubGen> {
Expand Down

0 comments on commit a739390

Please sign in to comment.