1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 ECMA Section: 11.5.3 Applying the % operator
44 The binary % operator is said to yield the remainder of its operands from
45 an implied division; the left operand is the dividend and the right operand
46 is the divisor. In C and C++, the remainder operator accepts only integral
47 operands, but in ECMAScript, it also accepts floating-point operands.
49 The result of a floating-point remainder operation as computed by the %
50 operator is not the same as the "remainder" operation defined by IEEE 754.
51 The IEEE 754 "remainder" operation computes the remainder from a rounding
52 division, not a truncating division, and so its behavior is not analogous
53 to that of the usual integer remainder operator. Instead the ECMAScript
54 language defines % on floating-point operations to behave in a manner
55 analogous to that of the Java integer remainder operator; this may be
56 compared with the C library function fmod.
58 The result of a ECMAScript floating-point remainder operation is determined by the rules of IEEE arithmetic:
60 If either operand is NaN, the result is NaN.
61 The sign of the result equals the sign of the dividend.
62 If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
63 If the dividend is finite and the divisor is an infinity, the result equals the dividend.
64 If the dividend is a zero and the divisor is finite, the result is the same as the dividend.
65 In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r
66 from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that
67 is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as
68 possible without exceeding the magnitude of the true mathematical quotient of n and d.
70 Author: christine@netscape.com
71 Date: 12 november 1997
73 var SECTION = "11.5.3";
74 var VERSION = "ECMA_1";
77 var BUGNUMBER="111202";
79 writeHeaderToLog( SECTION + " Applying the % operator");
81 // if either operand is NaN, the result is NaN.
83 new TestCase( SECTION, "Number.NaN % Number.NaN", Number.NaN, Number.NaN % Number.NaN );
84 new TestCase( SECTION, "Number.NaN % 1", Number.NaN, Number.NaN % 1 );
85 new TestCase( SECTION, "1 % Number.NaN", Number.NaN, 1 % Number.NaN );
87 new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NaN", Number.NaN, Number.POSITIVE_INFINITY % Number.NaN );
88 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NaN", Number.NaN, Number.NEGATIVE_INFINITY % Number.NaN );
90 // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
91 // dividend is an infinity
93 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY );
94 new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY );
95 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY );
96 new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY );
98 new TestCase( SECTION, "Number.POSITIVE_INFINITY % 0", Number.NaN, Number.POSITIVE_INFINITY % 0 );
99 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 0", Number.NaN, Number.NEGATIVE_INFINITY % 0 );
100 new TestCase( SECTION, "Number.POSITIVE_INFINITY % -0", Number.NaN, Number.POSITIVE_INFINITY % -0 );
101 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -0", Number.NaN, Number.NEGATIVE_INFINITY % -0 );
103 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 1 ", Number.NaN, Number.NEGATIVE_INFINITY % 1 );
104 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -1 ", Number.NaN, Number.NEGATIVE_INFINITY % -1 );
105 new TestCase( SECTION, "Number.POSITIVE_INFINITY % 1 ", Number.NaN, Number.POSITIVE_INFINITY % 1 );
106 new TestCase( SECTION, "Number.POSITIVE_INFINITY % -1 ", Number.NaN, Number.POSITIVE_INFINITY % -1 );
108 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % Number.MAX_VALUE );
109 new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % -Number.MAX_VALUE );
110 new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % Number.MAX_VALUE );
111 new TestCase( SECTION, "Number.POSITIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % -Number.MAX_VALUE );
114 new TestCase( SECTION, "0 % -0", Number.NaN, 0 % -0 );
115 new TestCase( SECTION, "-0 % 0", Number.NaN, -0 % 0 );
116 new TestCase( SECTION, "-0 % -0", Number.NaN, -0 % -0 );
117 new TestCase( SECTION, "0 % 0", Number.NaN, 0 % 0 );
119 new TestCase( SECTION, "1 % 0", Number.NaN, 1%0 );
120 new TestCase( SECTION, "1 % -0", Number.NaN, 1%-0 );
121 new TestCase( SECTION, "-1 % 0", Number.NaN, -1%0 );
122 new TestCase( SECTION, "-1 % -0", Number.NaN, -1%-0 );
124 new TestCase( SECTION, "Number.MAX_VALUE % 0", Number.NaN, Number.MAX_VALUE%0 );
125 new TestCase( SECTION, "Number.MAX_VALUE % -0", Number.NaN, Number.MAX_VALUE%-0 );
126 new TestCase( SECTION, "-Number.MAX_VALUE % 0", Number.NaN, -Number.MAX_VALUE%0 );
127 new TestCase( SECTION, "-Number.MAX_VALUE % -0", Number.NaN, -Number.MAX_VALUE%-0 );
129 // If the dividend is finite and the divisor is an infinity, the result equals the dividend.
131 new TestCase( SECTION, "1 % Number.NEGATIVE_INFINITY", 1, 1 % Number.NEGATIVE_INFINITY );
132 new TestCase( SECTION, "1 % Number.POSITIVE_INFINITY", 1, 1 % Number.POSITIVE_INFINITY );
133 new TestCase( SECTION, "-1 % Number.POSITIVE_INFINITY", -1, -1 % Number.POSITIVE_INFINITY );
134 new TestCase( SECTION, "-1 % Number.NEGATIVE_INFINITY", -1, -1 % Number.NEGATIVE_INFINITY );
136 new TestCase( SECTION, "Number.MAX_VALUE % Number.NEGATIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.NEGATIVE_INFINITY );
137 new TestCase( SECTION, "Number.MAX_VALUE % Number.POSITIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.POSITIVE_INFINITY );
138 new TestCase( SECTION, "-Number.MAX_VALUE % Number.POSITIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.POSITIVE_INFINITY );
139 new TestCase( SECTION, "-Number.MAX_VALUE % Number.NEGATIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.NEGATIVE_INFINITY );
141 new TestCase( SECTION, "0 % Number.POSITIVE_INFINITY", 0, 0 % Number.POSITIVE_INFINITY );
142 new TestCase( SECTION, "0 % Number.NEGATIVE_INFINITY", 0, 0 % Number.NEGATIVE_INFINITY );
143 new TestCase( SECTION, "-0 % Number.POSITIVE_INFINITY", -0, -0 % Number.POSITIVE_INFINITY );
144 new TestCase( SECTION, "-0 % Number.NEGATIVE_INFINITY", -0, -0 % Number.NEGATIVE_INFINITY );
146 // If the dividend is a zero and the divisor is finite, the result is the same as the dividend.
148 new TestCase( SECTION, "0 % 1", 0, 0 % 1 );
149 new TestCase( SECTION, "0 % -1", -0, 0 % -1 );
150 new TestCase( SECTION, "-0 % 1", -0, -0 % 1 );
151 new TestCase( SECTION, "-0 % -1", 0, -0 % -1 );
153 // In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r
154 // from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that
155 // is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as
156 // possible without exceeding the magnitude of the true mathematical quotient of n and d.