main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 | var isNaN = require('math-intrinsics/isNaN');
|
---|
5 | var isFinite = require('math-intrinsics/isFinite');
|
---|
6 |
|
---|
7 | var truncate = require('../truncate');
|
---|
8 |
|
---|
9 | // https://262.ecma-international.org/14.0/#sec-numeric-types-number-remainder
|
---|
10 |
|
---|
11 | module.exports = function NumberRemainder(n, d) {
|
---|
12 | if (typeof n !== 'number' || typeof d !== 'number') {
|
---|
13 | throw new $TypeError('Assertion failed: `n` and `d` arguments must be Numbers');
|
---|
14 | }
|
---|
15 |
|
---|
16 | // If either operand is NaN, the result is NaN.
|
---|
17 | // If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
|
---|
18 | if (isNaN(n) || isNaN(d) || !isFinite(n) || d === 0) {
|
---|
19 | return NaN;
|
---|
20 | }
|
---|
21 |
|
---|
22 | // If the dividend is finite and the divisor is an infinity, the result equals the dividend.
|
---|
23 | // If the dividend is a zero and the divisor is nonzero and finite, the result is the same as the dividend.
|
---|
24 | if (!isFinite(d) || n === 0) {
|
---|
25 | return n;
|
---|
26 | }
|
---|
27 |
|
---|
28 | if (!isFinite(n) || !isFinite(d) || n === 0 || d === 0) {
|
---|
29 | throw new $TypeError('Assertion failed: `n` and `d` arguments must be finite and nonzero');
|
---|
30 | }
|
---|
31 | var quotient = n / d;
|
---|
32 | var q = truncate(quotient);
|
---|
33 | var r = n - (d * q);
|
---|
34 | if (r === 0 && n < 0) {
|
---|
35 | return -0;
|
---|
36 | }
|
---|
37 | return r;
|
---|
38 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.