1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 11.5.3.js 24 ECMA Section: 11.5.3 Applying the % operator 25 Description: 26 27 The binary % operator is said to yield the remainder of its operands from 28 an implied division; the left operand is the dividend and the right operand 29 is the divisor. In C and C++, the remainder operator accepts only integral 30 operands, but in ECMAScript, it also accepts floating-point operands. 31 32 The result of a floating-point remainder operation as computed by the % 33 operator is not the same as the "remainder" operation defined by IEEE 754. 34 The IEEE 754 "remainder" operation computes the remainder from a rounding 35 division, not a truncating division, and so its behavior is not analogous 36 to that of the usual integer remainder operator. Instead the ECMAScript 37 language defines % on floating-point operations to behave in a manner 38 analogous to that of the Java integer remainder operator; this may be 39 compared with the C library function fmod. 40 41 The result of a ECMAScript floating-point remainder operation is determined by the rules of IEEE arithmetic: 42 43 If either operand is NaN, the result is NaN. 44 The sign of the result equals the sign of the dividend. 45 If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN. 46 If the dividend is finite and the divisor is an infinity, the result equals the dividend. 47 If the dividend is a zero and the divisor is finite, the result is the same as the dividend. 48 In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r 49 from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that 50 is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as 51 possible without exceeding the magnitude of the true mathematical quotient of n and d. 52 53 Author: christine (at) netscape.com 54 Date: 12 november 1997 55 */ 56 var SECTION = "11.5.3"; 57 var VERSION = "ECMA_1"; 58 startTest(); 59 var testcases = getTestCases(); 60 var BUGNUMBER="111202"; 61 62 writeHeaderToLog( SECTION + " Applying the % operator"); 63 test(); 64 65 function test() { 66 for ( tc=0; tc < testcases.length; tc++ ) { 67 testcases[tc].passed = writeTestCaseResult( 68 testcases[tc].expect, 69 testcases[tc].actual, 70 testcases[tc].description +" = "+ 71 testcases[tc].actual ); 72 73 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 74 } 75 stopTest(); 76 return ( testcases ); 77 } 78 function getTestCases() { 79 var array = new Array(); 80 var item = 0; 81 82 // if either operand is NaN, the result is NaN. 83 84 array[item++] = new TestCase( SECTION, "Number.NaN % Number.NaN", Number.NaN, Number.NaN % Number.NaN ); 85 array[item++] = new TestCase( SECTION, "Number.NaN % 1", Number.NaN, Number.NaN % 1 ); 86 array[item++] = new TestCase( SECTION, "1 % Number.NaN", Number.NaN, 1 % Number.NaN ); 87 88 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NaN", Number.NaN, Number.POSITIVE_INFINITY % Number.NaN ); 89 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NaN", Number.NaN, Number.NEGATIVE_INFINITY % Number.NaN ); 90 91 // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN. 92 // dividend is an infinity 93 94 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY ); 95 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY ); 96 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY ); 97 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY ); 98 99 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % 0", Number.NaN, Number.POSITIVE_INFINITY % 0 ); 100 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 0", Number.NaN, Number.NEGATIVE_INFINITY % 0 ); 101 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % -0", Number.NaN, Number.POSITIVE_INFINITY % -0 ); 102 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -0", Number.NaN, Number.NEGATIVE_INFINITY % -0 ); 103 104 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 1 ", Number.NaN, Number.NEGATIVE_INFINITY % 1 ); 105 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -1 ", Number.NaN, Number.NEGATIVE_INFINITY % -1 ); 106 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % 1 ", Number.NaN, Number.POSITIVE_INFINITY % 1 ); 107 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % -1 ", Number.NaN, Number.POSITIVE_INFINITY % -1 ); 108 109 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % Number.MAX_VALUE ); 110 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ); 111 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % Number.MAX_VALUE ); 112 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % -Number.MAX_VALUE ); 113 114 // divisor is 0 115 array[item++] = new TestCase( SECTION, "0 % -0", Number.NaN, 0 % -0 ); 116 array[item++] = new TestCase( SECTION, "-0 % 0", Number.NaN, -0 % 0 ); 117 array[item++] = new TestCase( SECTION, "-0 % -0", Number.NaN, -0 % -0 ); 118 array[item++] = new TestCase( SECTION, "0 % 0", Number.NaN, 0 % 0 ); 119 120 array[item++] = new TestCase( SECTION, "1 % 0", Number.NaN, 1%0 ); 121 array[item++] = new TestCase( SECTION, "1 % -0", Number.NaN, 1%-0 ); 122 array[item++] = new TestCase( SECTION, "-1 % 0", Number.NaN, -1%0 ); 123 array[item++] = new TestCase( SECTION, "-1 % -0", Number.NaN, -1%-0 ); 124 125 array[item++] = new TestCase( SECTION, "Number.MAX_VALUE % 0", Number.NaN, Number.MAX_VALUE%0 ); 126 array[item++] = new TestCase( SECTION, "Number.MAX_VALUE % -0", Number.NaN, Number.MAX_VALUE%-0 ); 127 array[item++] = new TestCase( SECTION, "-Number.MAX_VALUE % 0", Number.NaN, -Number.MAX_VALUE%0 ); 128 array[item++] = new TestCase( SECTION, "-Number.MAX_VALUE % -0", Number.NaN, -Number.MAX_VALUE%-0 ); 129 130 // If the dividend is finite and the divisor is an infinity, the result equals the dividend. 131 132 array[item++] = new TestCase( SECTION, "1 % Number.NEGATIVE_INFINITY", 1, 1 % Number.NEGATIVE_INFINITY ); 133 array[item++] = new TestCase( SECTION, "1 % Number.POSITIVE_INFINITY", 1, 1 % Number.POSITIVE_INFINITY ); 134 array[item++] = new TestCase( SECTION, "-1 % Number.POSITIVE_INFINITY", -1, -1 % Number.POSITIVE_INFINITY ); 135 array[item++] = new TestCase( SECTION, "-1 % Number.NEGATIVE_INFINITY", -1, -1 % Number.NEGATIVE_INFINITY ); 136 137 array[item++] = new TestCase( SECTION, "Number.MAX_VALUE % Number.NEGATIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.NEGATIVE_INFINITY ); 138 array[item++] = new TestCase( SECTION, "Number.MAX_VALUE % Number.POSITIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.POSITIVE_INFINITY ); 139 array[item++] = new TestCase( SECTION, "-Number.MAX_VALUE % Number.POSITIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.POSITIVE_INFINITY ); 140 array[item++] = new TestCase( SECTION, "-Number.MAX_VALUE % Number.NEGATIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.NEGATIVE_INFINITY ); 141 142 array[item++] = new TestCase( SECTION, "0 % Number.POSITIVE_INFINITY", 0, 0 % Number.POSITIVE_INFINITY ); 143 array[item++] = new TestCase( SECTION, "0 % Number.NEGATIVE_INFINITY", 0, 0 % Number.NEGATIVE_INFINITY ); 144 array[item++] = new TestCase( SECTION, "-0 % Number.POSITIVE_INFINITY", -0, -0 % Number.POSITIVE_INFINITY ); 145 array[item++] = new TestCase( SECTION, "-0 % Number.NEGATIVE_INFINITY", -0, -0 % Number.NEGATIVE_INFINITY ); 146 147 // If the dividend is a zero and the divisor is finite, the result is the same as the dividend. 148 149 array[item++] = new TestCase( SECTION, "0 % 1", 0, 0 % 1 ); 150 array[item++] = new TestCase( SECTION, "0 % -1", -0, 0 % -1 ); 151 array[item++] = new TestCase( SECTION, "-0 % 1", -0, -0 % 1 ); 152 array[item++] = new TestCase( SECTION, "-0 % -1", 0, -0 % -1 ); 153 154 // In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r 155 // from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that 156 // is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as 157 // possible without exceeding the magnitude of the true mathematical quotient of n and d. 158 159 return ( array ); 160 } 161