sindresorhus/round-to

Do you want to work on this issue?

You can request for a bounty in order to promote it!

-0 and negative high precision values? #28

yvele posted onGitHub

Is it on purpose to round -6.578040334659363e-10 to -0 instead of 0? 🤔 Shouldn't be better to never round to -0 but 0 instead 🤔

See my PR that add unit tests with high precision values:


I've added more tests.. This is super strange as -6.578040334659363e-10 gives -0. But -0 gives 0.

Looks like the zero sign is not preserved consistently: https://github.com/sindresorhus/round-to/pull/27/commits/db7eaa31a17129dd058e4d956cad6411dcad57d8

We could add a condition to preserve -0 sign.

if (Object.is(number, -0)) {
  return number;
}
posted by yvele over 1 year ago

It's not intended. -0 is almost never useful, so I agree that it should be 0.

posted by sindresorhus over 1 year ago

Hum I don't think we should change the sign of the provided number if it is already rounded. But when we need to round a number, I agree that it should NEVER returns -0.

This is wrong anyway: https://github.com/sindresorhus/round-to/blob/6885adef26882b186704e4c8fc414b785947bb92/test.js#L27

This is my proposal:

test('roundTo()', t => {
    t.is(roundTo(0.129, 3), 0.129);
    t.is(roundTo(0.129, 2), 0.13);
    t.is(roundTo(0.129, 1), 0.1);
    t.is(roundTo(1.005, 2), 1.01);
    t.is(roundTo(1.005, 0), 1);
    t.is(roundTo(111.1, -2), 100);
    t.is(roundTo(-0.375, 2), -0.38);
    t.false(Number.isNaN(roundTo(10_000_000_000_000, 8)));
    t.is(roundTo(0.375_423_234_234_234_324_324_324_324_32, 8), 0.375_423_23); // eslint-disable-line no-loss-of-precision
    t.is(roundTo(0.123_178_263_8, Number.POSITIVE_INFINITY), 0.123_178_263_8);
    t.is(roundTo(0.5, 0), 1);
    t.is(roundTo(-0.5, 0), -1);
    t.is(roundTo(5.12, 1), 5.1);
    t.is(roundTo(-5.12, 1), -5.1);
    t.is(roundTo(1.005, 2), 1.01);
    t.is(roundTo(-1.005, 2), -1.01);
    t.is(roundTo(39.425, 2), 39.43);
    t.is(roundTo(-39.425, 2), -39.43);
    t.is(roundTo(1262.48, -1), 1260);
    t.is(roundTo(1262.48, -2), 1300);
    t.is(roundTo(0.597 / 6, 3), 0.1);
    t.is(roundTo(6.578_040_334_659_363e-10, 6), 0);
-    t.is(roundTo(-6.578_040_334_659_363e-10, 6), -0);
+    t.is(roundTo(-6.578_040_334_659_363e-10, 6), 0);
    t.is(roundTo(0, 1), 0);
-    t.is(roundTo(-0, 1), 0);
+    t.is(roundTo(-0, 1), -0);
});

Does that make sense?

posted by yvele over 1 year ago

Agreed

posted by sindresorhus over 1 year ago

Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests