Skip to content

Commit

Permalink
[dart2wasm] Fix double.pow
Browse files Browse the repository at this point in the history
Handle these two edge cases documented in `pow`:

- if x is 1.0, the result is always 1.0
- if y is Infinity and x is -1, the result is 1.0

New passing tests:

- co19/LibTest/math/pow_A04_t01
- co19/LibTest/math/pow_A14_t01
- co19/LibTest/math/pow_A16_t01
- lib/math/double_pow_test

Fixes #50505

Change-Id: I42266e9b52edd853133db8aeaafe9d5aa73a1449
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268960
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
  • Loading branch information
osa1 authored and Commit Queue committed Nov 18, 2022
1 parent 2846cb8 commit 50c5e8f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sdk/lib/_internal/wasm/lib/math_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ num pow(num x, num exponent) {
if ((x is int) && (exponent is int) && (exponent >= 0)) {
return _intPow(x, exponent);
}
return _doublePow(x.toDouble(), exponent.toDouble());

double xDouble = x.toDouble();

if (xDouble == 1.0) {
return 1.0;
}

double exponentDouble = exponent.toDouble();

if (xDouble == -1.0 && exponent.isInfinite) {
return 1.0;
}

return _doublePow(xDouble, exponentDouble);
}

@pragma("wasm:import", "Math.pow")
Expand Down

0 comments on commit 50c5e8f

Please sign in to comment.