]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Revert "Merge pull request #5597 from Alanscut:issue-5596"
authorIskren Chernev <me@iskren.info>
Tue, 26 Dec 2023 17:51:59 +0000 (19:51 +0200)
committerIskren Chernev <me@iskren.info>
Tue, 26 Dec 2023 17:51:59 +0000 (19:51 +0200)
This reverts commit b4e0676927f8acae8a3e8fea45fe47a8f1ddc62f, reversing
changes made to cbcd0c5116e43ebd569e050934aeaef622ba7b64.

src/lib/duration/constructor.js
src/lib/utils/float-calculate.js [deleted file]
src/test/moment/duration.js

index a315cd1fe4073e0c0ef1e83a5d775db1a26755d3..6033769b70336520ec90c7b61970e8777c82a9e9 100644 (file)
@@ -1,7 +1,6 @@
 import { normalizeObjectUnits } from '../units/aliases';
 import { getLocale } from '../locale/locales';
 import isDurationValid from './valid.js';
-import multiply from '../utils/float-calculate';
 
 export function Duration(duration) {
     var normalizedInput = normalizeObjectUnits(duration),
@@ -20,10 +19,9 @@ export function Duration(duration) {
     // representation for dateAddRemove
     this._milliseconds =
         +milliseconds +
-        // reduce floating point rounding errors
-        multiply(seconds, 1e3) +
-        multiply(minutes, 6e4) +
-        multiply(hours, 36e5);
+        seconds * 1e3 + // 1000
+        minutes * 6e4 + // 1000 * 60
+        hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978
     // Because of dateAddRemove treats 24 hours as different from a
     // day when working around DST, we need to store them separately
     this._days = +days + weeks * 7;
diff --git a/src/lib/utils/float-calculate.js b/src/lib/utils/float-calculate.js
deleted file mode 100644 (file)
index 3753a60..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-export default function multiply(a, b) {
-    var bi1 = toBigInt(parseFloat(a)),
-        bi2 = toBigInt(parseFloat(b)),
-        mag1 = bi1.magnification,
-        mag2 = bi2.magnification,
-        num1 = bi1.num,
-        num2 = bi2.num;
-    return (num1 * num2) / (mag1 * mag2);
-}
-
-function toBigInt(floatNum) {
-    var bigInt = {
-            num: 0,
-            magnification: 1,
-        },
-        strNum,
-        len,
-        mag,
-        intNum;
-
-    if (Math.floor(floatNum) === floatNum) {
-        bigInt.num = floatNum;
-        return bigInt;
-    }
-
-    strNum = floatNum.toString();
-    len = strNum.length - strNum.indexOf('.') - 1;
-    mag = Math.pow(10, len);
-    intNum = Number(floatNum.toString().replace('.', ''));
-    bigInt.num = intNum;
-    bigInt.magnification = mag;
-    return bigInt;
-}
index 5b832f4928b8a80551a30cf0461d31f51502b5a7..7c1846596f07899f68e3582fe305887d67052e4b 100644 (file)
@@ -248,15 +248,6 @@ test('instantiation from another duration', function (assert) {
 });
 
 test('explicit cloning', function (assert) {
-    var duration = moment.duration(1.1234, 'hours');
-    assert.equal(
-        duration.milliseconds(),
-        240,
-        'Float number calculation will not lose accuracy'
-    );
-});
-
-test('float number calculation', function (assert) {
     var durationA = moment.duration(5, 'milliseconds'),
         durationB = durationA.clone();
     durationA.add(5, 'milliseconds');