Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding using .ceil only checks the first decimal #116

Open
andydolan94 opened this issue Sep 10, 2024 · 5 comments
Open

Rounding using .ceil only checks the first decimal #116

andydolan94 opened this issue Sep 10, 2024 · 5 comments

Comments

@andydolan94
Copy link

I'm currently attempting to use the rounding methods from this library and I'm finding that the ceil function doesn't provide the desired output

Expected Behavior

const numberToRound = new Decimal128(100.05);
console.log(numberToRound.round(0, "floor").toNumber());
// outputs 100
console.log(numberToRound.round(0, "ceil").toNumber());
// outputs 101

Current Behavior

const numberToRound = new Decimal128(100.05);
console.log(numberToRound.round(0, "floor").toNumber());
// outputs 100
console.log(numberToRound.round(0, "ceil").toNumber());
// outputs 100 - incorrect

Context (Environment)

Using the latest version of Decimal128 17.0.0 on Deno 1.46.3

@jessealama
Copy link
Owner

Can you try using "100.05" rather than 100.05? I suspect the difficulty is that 100.05 (without the quotes) is getting rounded rather badly.

@jessealama
Copy link
Owner

You might be interested to know that 100.05 is one of those "wicked" values that leads to trouble. See the discussion in the decimal proposal repo.

@jessealama
Copy link
Owner

Can you try this again with "100.05", in quotes, rather than 100.05?

@amoshydra
Copy link
Contributor

amoshydra commented Jan 6, 2025

This is reproducible when the input is a string:
https://codepen.io/amoshydra/pen/NPKXrGB?editors=0010

The output below round using "ceil" rounding mode

current:

"100.1" rounding to 0 decimals = "101"
"100.05" rounding to 0 decimals = "100"
"100.05" rounding to 1 decimals = "100.1"
"100.005" rounding to 1 decimals = "100"

expected:

"100.1" rounding to 0 decimals = "101"
"100.05" rounding to 0 decimals = "101"
"100.05" rounding to 1 decimals = "100.1"
"100.005" rounding to 1 decimals = "100.1"

@amoshydra
Copy link
Contributor

amoshydra commented Jan 8, 2025

I have added more tests for round and precision at #128. It is currently still in draft. But we should be able to verify the correctness of the tests first.

In the PR I have found the following unexpected cases for .round for the various rounding mode

rounding non-decimal > mode: floor > -1 round(0) to be -1
    
    Expected: "-1"
    Received: "-2"

rounding non-decimal > mode: floor > -1 round(1) to be -1
    
    Expected: "-1"
    Received: "-1.1"

rounding non-decimal > mode: floor > -1 round(10) to be -1
    
    Expected: "-1"
    Received: "-1.0000000001"

rounding non-decimal > mode: floor > -1.11e+10 round(0) to be -11100000000
    
    Expected: "-11100000000"
    Received: "-11100000001"

rounding non-decimal > mode: floor > -1.11e+10 round(1) to be -11100000000
    
    Expected: "-11100000000"
    Received: "-11100000000.1"

rounding non-decimal > mode: floor > -1.11e+10 round(10) to be -11100000000
    
    Expected: "-11100000000"
    Received: "-11100000000.0000000001"

rounding non-decimal > mode: floor > -0.011e+10 round(0) to be -110000000
    
    Expected: "-110000000"
    Received: "-110000001"

rounding non-decimal > mode: floor > -0.011e+10 round(1) to be -110000000
    
    Expected: "-110000000"
    Received: "-110000000.1"

rounding non-decimal > mode: floor > -0.011e+10 round(10) to be -110000000
    
    Expected: "-110000000"
    Received: "-110000000.0000000001"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.5 round(1) to be -0.5
    
    Expected: "-0.5"
    Received: "-0.6"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.5 round(1) to be -1.5
    
    Expected: "-1.5"
    Received: "-1.6"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.15 round(2) to be -0.15
    
    Expected: "-0.15"
    Received: "-0.16"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.15 round(2) to be -1.15
    
    Expected: "-1.15"
    Received: "-1.16"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.1000000000000000000000000000000015 round(34) to be -0.1000000000000000000000000000000015
    
    Expected: "-0.1000000000000000000000000000000015"
    Received: "-0.1000000000000000000000000000000016"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.100000000000000000000000000000001 round(34) to be -0.100000000000000000000000000000001
    
    Expected: "-0.100000000000000000000000000000001"
    Received: "-0.1000000000000000000000000000000011"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.000000000000000000000000000000015 round(33) to be -1.000000000000000000000000000000015
    
    Expected: "-1.000000000000000000000000000000015"
    Received: "-1.000000000000000000000000000000016"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.00000000000000000000000000000001 round(33) to be -1.00000000000000000000000000000001
    
    Expected: "-1.00000000000000000000000000000001"
    Received: "-1.000000000000000000000000000000011"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.100000000000000015 round(34) to be -0.100000000000000015
    
    Expected: "-0.100000000000000015"
    Received: "-0.1000000000000000150000000000000001"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -0.10000000000000001 round(34) to be -0.10000000000000001
    
    Expected: "-0.10000000000000001"
    Received: "-0.1000000000000000100000000000000001"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.00000000000000015 round(33) to be -1.00000000000000015
    
    Expected: "-1.00000000000000015"
    Received: "-1.000000000000000150000000000000001"

rounding decimals > unnecessary rounding should return the same output as the input > mode: floor > -1.0000000000000001 round(33) to be -1.0000000000000001
    
    Expected: "-1.0000000000000001"
    Received: "-1.000000000000000100000000000000001"

rounding decimals > "Symbol(NoExplicitMode)" > rounding decimal to integer > input: "-0.6" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "1"

rounding decimals > "Symbol(NoExplicitMode)" > rounding decimal (1) > input: "-0.06" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "0.1"

rounding decimals > "ceil" > rounding decimal to integer > input: "-0.4" > decimals: "0" > output: "-0"
    
    Expected: "-0"
    Received: "1"

rounding decimals > "ceil" > rounding decimal to integer > input: "-0.5" > decimals: "0" > output: "-0"
    
    Expected: "-0"
    Received: "1"

rounding decimals > "ceil" > rounding decimal to integer > input: "-0.6" > decimals: "0" > output: "-0"
    
    Expected: "-0"
    Received: "1"

rounding decimals > "ceil" > rounding decimal (1) > input: "-0.04" > decimals: "1" > output: "-0"
    
    Expected: "-0"
    Received: "0.1"

rounding decimals > "ceil" > rounding decimal (1) > input: "-0.05" > decimals: "1" > output: "-0"
    
    Expected: "-0"
    Received: "0.1"

rounding decimals > "ceil" > rounding decimal (1) > input: "-0.06" > decimals: "1" > output: "-0"
    
    Expected: "-0"
    Received: "0.1"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0004" > decimals: "1" > output: "0.1"
    
    Expected: "0.1"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0005" > decimals: "1" > output: "0.1"
    
    Expected: "0.1"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0006" > decimals: "1" > output: "0.1"
    
    Expected: "0.1"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0004" > decimals: "2" > output: "0.01"
    
    Expected: "0.01"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0005" > decimals: "2" > output: "0.01"
    
    Expected: "0.01"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "0.0006" > decimals: "2" > output: "0.01"
    
    Expected: "0.01"
    Received: "0"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0004" > decimals: "1" > output: "5.1"
    
    Expected: "5.1"
    Received: "5"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0005" > decimals: "1" > output: "5.1"
    
    Expected: "5.1"
    Received: "5"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0006" > decimals: "1" > output: "5.1"
    
    Expected: "5.1"
    Received: "5"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0004" > decimals: "2" > output: "5.01"
    
    Expected: "5.01"
    Received: "5"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0005" > decimals: "2" > output: "5.01"
    
    Expected: "5.01"
    Received: "5"

rounding decimals > "ceil" > rounding decimal (1) on number with much higher (> 2) precision > input: "5.0006" > decimals: "2" > output: "5.01"
    
    Expected: "5.01"
    Received: "5"

rounding decimals > "floor" > rounding decimal to integer > input: "-0.4" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal to integer > input: "-0.5" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal to integer > input: "-0.6" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) > input: "-0.04" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) > input: "-0.05" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) > input: "-0.06" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0004" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0005" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0006" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0004" > decimals: "2" > output: "-0.01"
    
    Expected: "-0.01"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0005" > decimals: "2" > output: "-0.01"
    
    Expected: "-0.01"
    Received: "-0"

rounding decimals > "floor" > rounding decimal (1) on number with much higher (> 2) precision > input: "-0.0006" > decimals: "2" > output: "-0.01"
    
    Expected: "-0.01"
    Received: "-0"

rounding decimals > "halfExpand" > rounding decimal to integer > input: "-0.5" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "1"

rounding decimals > "halfExpand" > rounding decimal to integer > input: "-0.6" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "1"

rounding decimals > "halfExpand" > rounding decimal (1) > input: "-0.05" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "0.1"

rounding decimals > "halfExpand" > rounding decimal (1) > input: "-0.06" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "0.1"

rounding decimals > "halfEven" > rounding decimal to integer > input: "-0.6" > decimals: "0" > output: "-1"
    
    Expected: "-1"
    Received: "1"

rounding decimals > "halfEven" > rounding decimal (1) > input: "-0.06" > decimals: "1" > output: "-0.1"
    
    Expected: "-0.1"
    Received: "0.1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants