Skip to content

Commit

Permalink
Division for negative number was rounding up (#71)
Browse files Browse the repository at this point in the history
This is normal behavior for negative numbers on CPUs, but unexpected behavior for math.
This required changing the divisions to `div_euclid` to match the expected behavior.
  • Loading branch information
dahlend authored Jul 8, 2024
1 parent 8b8a0db commit 8178651
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/neospy_core/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ impl Time<UTC> {

let mut l = offset.div_euclid(1.0) as i64 + 68569;

let n = (4 * l) / 146097;
l -= (146097 * n + 3) / 4;
let i = (4000 * (l + 1)) / 1461001;
l -= (1461 * i) / 4 - 31;
let k = (80 * l) / 2447;
let day = l - (2447 * k) / 80;
l = k / 11;
let n = (4 * l).div_euclid(146097);
l -= (146097 * n + 3).div_euclid(4);
let i = (4000 * (l + 1)).div_euclid(1461001);
l -= (1461 * i).div_euclid(4) - 31;
let k = (80 * l).div_euclid(2447);
let day = l - (2447 * k).div_euclid(80);
l = k.div_euclid(11);

let month = k + 2 - 12 * l;
let year = 100 * (n - 49) + i + l;
Expand Down Expand Up @@ -223,6 +223,11 @@ mod tests {

let t2 = Time::<UTC>::from_year_month_day(763, 9, 18, 0.5);
assert!(t2.jd == 2000000.);

let ymd = Time::<UTC>::new(-68774.4991992591).year_month_day();
assert!(ymd.0 == -4901);
assert!(ymd.1 == 8);
assert!(ymd.2 == 8);
}

#[test]
Expand Down

0 comments on commit 8178651

Please sign in to comment.