michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 11.5.3.js michael@0: ECMA Section: 11.5.3 Applying the % operator michael@0: Description: michael@0: michael@0: The binary % operator is said to yield the remainder of its operands from michael@0: an implied division; the left operand is the dividend and the right operand michael@0: is the divisor. In C and C++, the remainder operator accepts only integral michael@0: operands, but in ECMAScript, it also accepts floating-point operands. michael@0: michael@0: The result of a floating-point remainder operation as computed by the % michael@0: operator is not the same as the "remainder" operation defined by IEEE 754. michael@0: The IEEE 754 "remainder" operation computes the remainder from a rounding michael@0: division, not a truncating division, and so its behavior is not analogous michael@0: to that of the usual integer remainder operator. Instead the ECMAScript michael@0: language defines % on floating-point operations to behave in a manner michael@0: analogous to that of the Java integer remainder operator; this may be michael@0: compared with the C library function fmod. michael@0: michael@0: The result of a ECMAScript floating-point remainder operation is determined by the rules of IEEE arithmetic: michael@0: michael@0: If either operand is NaN, the result is NaN. michael@0: The sign of the result equals the sign of the dividend. michael@0: If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN. michael@0: If the dividend is finite and the divisor is an infinity, the result equals the dividend. michael@0: If the dividend is a zero and the divisor is finite, the result is the same as the dividend. michael@0: In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r michael@0: from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that michael@0: is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as michael@0: possible without exceeding the magnitude of the true mathematical quotient of n and d. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: var SECTION = "11.5.3"; michael@0: var VERSION = "ECMA_1"; michael@0: var BUGNUMBER="111202"; michael@0: startTest(); michael@0: michael@0: michael@0: writeHeaderToLog( SECTION + " Applying the % operator"); michael@0: michael@0: // if either operand is NaN, the result is NaN. michael@0: michael@0: new TestCase( SECTION, "Number.NaN % Number.NaN", Number.NaN, Number.NaN % Number.NaN ); michael@0: new TestCase( SECTION, "Number.NaN % 1", Number.NaN, Number.NaN % 1 ); michael@0: new TestCase( SECTION, "1 % Number.NaN", Number.NaN, 1 % Number.NaN ); michael@0: michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NaN", Number.NaN, Number.POSITIVE_INFINITY % Number.NaN ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NaN", Number.NaN, Number.NEGATIVE_INFINITY % Number.NaN ); michael@0: michael@0: // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN. michael@0: // dividend is an infinity michael@0: michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.NEGATIVE_INFINITY ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.NEGATIVE_INFINITY ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.NEGATIVE_INFINITY % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY", Number.NaN, Number.POSITIVE_INFINITY % Number.POSITIVE_INFINITY ); michael@0: michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % 0", Number.NaN, Number.POSITIVE_INFINITY % 0 ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 0", Number.NaN, Number.NEGATIVE_INFINITY % 0 ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % -0", Number.NaN, Number.POSITIVE_INFINITY % -0 ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -0", Number.NaN, Number.NEGATIVE_INFINITY % -0 ); michael@0: michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % 1 ", Number.NaN, Number.NEGATIVE_INFINITY % 1 ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -1 ", Number.NaN, Number.NEGATIVE_INFINITY % -1 ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % 1 ", Number.NaN, Number.POSITIVE_INFINITY % 1 ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % -1 ", Number.NaN, Number.POSITIVE_INFINITY % -1 ); michael@0: michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % Number.MAX_VALUE ); michael@0: new TestCase( SECTION, "Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.NEGATIVE_INFINITY % -Number.MAX_VALUE ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % Number.MAX_VALUE ); michael@0: new TestCase( SECTION, "Number.POSITIVE_INFINITY % -Number.MAX_VALUE ", Number.NaN, Number.POSITIVE_INFINITY % -Number.MAX_VALUE ); michael@0: michael@0: // divisor is 0 michael@0: new TestCase( SECTION, "0 % -0", Number.NaN, 0 % -0 ); michael@0: new TestCase( SECTION, "-0 % 0", Number.NaN, -0 % 0 ); michael@0: new TestCase( SECTION, "-0 % -0", Number.NaN, -0 % -0 ); michael@0: new TestCase( SECTION, "0 % 0", Number.NaN, 0 % 0 ); michael@0: michael@0: new TestCase( SECTION, "1 % 0", Number.NaN, 1%0 ); michael@0: new TestCase( SECTION, "1 % -0", Number.NaN, 1%-0 ); michael@0: new TestCase( SECTION, "-1 % 0", Number.NaN, -1%0 ); michael@0: new TestCase( SECTION, "-1 % -0", Number.NaN, -1%-0 ); michael@0: michael@0: new TestCase( SECTION, "Number.MAX_VALUE % 0", Number.NaN, Number.MAX_VALUE%0 ); michael@0: new TestCase( SECTION, "Number.MAX_VALUE % -0", Number.NaN, Number.MAX_VALUE%-0 ); michael@0: new TestCase( SECTION, "-Number.MAX_VALUE % 0", Number.NaN, -Number.MAX_VALUE%0 ); michael@0: new TestCase( SECTION, "-Number.MAX_VALUE % -0", Number.NaN, -Number.MAX_VALUE%-0 ); michael@0: michael@0: // If the dividend is finite and the divisor is an infinity, the result equals the dividend. michael@0: michael@0: new TestCase( SECTION, "1 % Number.NEGATIVE_INFINITY", 1, 1 % Number.NEGATIVE_INFINITY ); michael@0: new TestCase( SECTION, "1 % Number.POSITIVE_INFINITY", 1, 1 % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "-1 % Number.POSITIVE_INFINITY", -1, -1 % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "-1 % Number.NEGATIVE_INFINITY", -1, -1 % Number.NEGATIVE_INFINITY ); michael@0: michael@0: new TestCase( SECTION, "Number.MAX_VALUE % Number.NEGATIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.NEGATIVE_INFINITY ); michael@0: new TestCase( SECTION, "Number.MAX_VALUE % Number.POSITIVE_INFINITY", Number.MAX_VALUE, Number.MAX_VALUE % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "-Number.MAX_VALUE % Number.POSITIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "-Number.MAX_VALUE % Number.NEGATIVE_INFINITY", -Number.MAX_VALUE, -Number.MAX_VALUE % Number.NEGATIVE_INFINITY ); michael@0: michael@0: new TestCase( SECTION, "0 % Number.POSITIVE_INFINITY", 0, 0 % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "0 % Number.NEGATIVE_INFINITY", 0, 0 % Number.NEGATIVE_INFINITY ); michael@0: new TestCase( SECTION, "-0 % Number.POSITIVE_INFINITY", -0, -0 % Number.POSITIVE_INFINITY ); michael@0: new TestCase( SECTION, "-0 % Number.NEGATIVE_INFINITY", -0, -0 % Number.NEGATIVE_INFINITY ); michael@0: michael@0: // If the dividend is a zero and the divisor is finite, the result is the same as the dividend. michael@0: michael@0: new TestCase( SECTION, "0 % 1", 0, 0 % 1 ); michael@0: new TestCase( SECTION, "0 % -1", -0, 0 % -1 ); michael@0: new TestCase( SECTION, "-0 % 1", -0, -0 % 1 ); michael@0: new TestCase( SECTION, "-0 % -1", 0, -0 % -1 ); michael@0: michael@0: // In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r michael@0: // from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that michael@0: // is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as michael@0: // possible without exceeding the magnitude of the true mathematical quotient of n and d. michael@0: michael@0: test(); michael@0: