MCPcopy Create free account
hub / github.com/mozilla/rhino / pow

Method pow

rhino/src/main/java/org/mozilla/javascript/NativeMath.java:521–576  ·  view source on GitHub ↗
(
            Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args)

Source from the content-addressed store, hash-verified

519
520 // See Ecma 15.8.2.13
521 private static Object pow(
522 Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
523 double x = ScriptRuntime.toNumber(args, 0);
524 double y = ScriptRuntime.toNumber(args, 1);
525 double result;
526 if (Double.isNaN(y)) {
527 // y is NaN, result is always NaN
528 result = y;
529 } else if (y == 0) {
530 // Java's pow(NaN, 0) = NaN; we need 1
531 result = 1.0;
532 } else if (x == 0) {
533 // Many differences from Java's Math.pow
534 if (1 / x > 0) {
535 result = (y > 0) ? 0 : Double.POSITIVE_INFINITY;
536 } else {
537 // x is -0, need to check if y is an odd integer
538 long y_long = (long) y;
539 if (y_long == y && (y_long & 0x1) != 0) {
540 result = (y > 0) ? -0.0 : Double.NEGATIVE_INFINITY;
541 } else {
542 result = (y > 0) ? 0.0 : Double.POSITIVE_INFINITY;
543 }
544 }
545 } else {
546 result = Math.pow(x, y);
547 if (Double.isNaN(result)) {
548 // Check for broken Java implementations that gives NaN
549 // when they should return something else
550 if (y == Double.POSITIVE_INFINITY) {
551 if (x < -1.0 || 1.0 < x) {
552 result = Double.POSITIVE_INFINITY;
553 } else if (-1.0 < x && x < 1.0) {
554 result = 0;
555 }
556 } else if (y == Double.NEGATIVE_INFINITY) {
557 if (x < -1.0 || 1.0 < x) {
558 result = 0;
559 } else if (-1.0 < x && x < 1.0) {
560 result = Double.POSITIVE_INFINITY;
561 }
562 } else if (x == Double.POSITIVE_INFINITY) {
563 result = (y > 0) ? Double.POSITIVE_INFINITY : 0.0;
564 } else if (x == Double.NEGATIVE_INFINITY) {
565 long y_long = (long) y;
566 if (y_long == y && (y_long & 0x1) != 0) {
567 // y is odd integer
568 result = (y > 0) ? Double.NEGATIVE_INFINITY : -0.0;
569 } else {
570 result = (y > 0) ? Double.POSITIVE_INFINITY : 0.0;
571 }
572 }
573 }
574 }
575 return ScriptRuntime.wrapNumber(result);
576 }
577
578 private static Object random(

Callers 15

testMinPositiveNormalMethod · 0.80
testUnderflowToZeroMethod · 0.80
testPowersOfTwoMethod · 0.80
exponentiateMethod · 0.80
doFastArithemticMethod · 0.80
readFloat16Method · 0.80

Calls 3

toNumberMethod · 0.95
wrapNumberMethod · 0.95
isNaNMethod · 0.45

Tested by 14

testMinPositiveNormalMethod · 0.64
testUnderflowToZeroMethod · 0.64
testPowersOfTwoMethod · 0.64
testFunction · 0.64
testFunction · 0.64
testFunction · 0.64