Skip to content

Commit 5e19467

Browse files
authored
fix: More proper Math.round (#1857)
1 parent 3d52206 commit 5e19467

File tree

4 files changed

+1699
-1570
lines changed

4 files changed

+1699
-1570
lines changed

std/assembly/math.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,8 @@ export namespace NativeMath {
14371437
// @ts-ignore: decorator
14381438
@inline
14391439
export function round(x: f64): f64 {
1440-
return builtin_copysign<f64>(builtin_floor<f64>(x + 0.5), x);
1440+
let roundUp = builtin_ceil<f64>(x);
1441+
return select<f64>(roundUp, roundUp - 1.0, roundUp - 0.5 <= x);
14411442
}
14421443

14431444
// @ts-ignore: decorator
@@ -2732,7 +2733,8 @@ export namespace NativeMathf {
27322733
// @ts-ignore: decorator
27332734
@inline
27342735
export function round(x: f32): f32 {
2735-
return builtin_copysign<f32>(builtin_floor<f32>(x + 0.5), x);
2736+
let roundUp = builtin_ceil<f32>(x);
2737+
return select<f32>(roundUp, roundUp - 1.0, roundUp - 0.5 <= x);
27362738
}
27372739

27382740
// @ts-ignore: decorator

0 commit comments

Comments
 (0)