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

Big(1).divideWithMathContext(Big(3), new MC(0, RoundingMode.CEILING)).numberValue() #65

Closed
qingfengmy opened this issue Apr 28, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@qingfengmy
Copy link

Describe the bug
Big(1).divideWithMathContext(Big(3), new MC(0, RoundingMode.CEILING)).numberValue()

                throw new RangeError('Non-terminating decimal expansion; no exact representable decimal result.');

1/3 ceiling

i want the result is 1. What should I do?

@qingfengmy qingfengmy added the bug Something isn't working label Apr 28, 2022
@srknzl
Copy link
Owner

srknzl commented Apr 28, 2022

Hi,

Doing the same thing also throws in java. It is because precision is 0. As I know, 0 precision means unlimited precision and means give me the exact result. So, we are asking the exact result from this operation but the exact result cannot be obtained with a non-terminating decimal expansion(0.3333...).

This code

new BigDecimal("1").divide(new BigDecimal("3"), new MathContext(0, RoundingMode.CEILING));

throws

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
	at java.math.BigDecimal.divide(BigDecimal.java:1693)
	at java.math.BigDecimal.divide(BigDecimal.java:1726)
	at Main.main(Main.java:23)

You can do this:

Big(1).divide(Big(3), undefined, RoundingMode.CEILING).numberValue()
// 1

which is equiavalent of Java's:

new BigDecimal(1).divide(new BigDecimal(3), RoundingMode.CEILING).intValue()
// 1

@srknzl
Copy link
Owner

srknzl commented Apr 29, 2022

Closing because I think my reply solves your problem, reopen the issue if it does not.

@srknzl srknzl closed this as completed Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants