-
Notifications
You must be signed in to change notification settings - Fork 18
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
What library features should BigDecimal have? #14
Comments
For some use cases it would be very useful to have |
BigDecimal(.1234567).round(4) === 0.1235m;
// or
Object.is(BigDecimal(123.4567).round(-1), 120m); , or an exponent parameter BigDecimal(.1234567).round(-4) === 0.1235m;
// or
Object.is(BigDecimal(123.4567).round(1), 120m); The latter seems more intuitive to me. |
Thinking again, number of decimal places could be more intuitive if we'll have // `round` takes a number of decimal places
bd1.round(bd2.decimalPlaces()); // instead of `-bd2.decimalPlaces()`
// also, `bd2.decimalPlaces` getter? Alternatively we can introduce // `round` takes an exponent
bd1.round(bd2.precision()); // or `bd2.precision` getter |
If we have a round method, should we have options for different modes for how to round |
Yes, rounding modes are very useful indeed! For reference: |
Can you say more about use cases for these that you're aware of? I can see that different Decimal libraries have decided to include different numbers of rounding modes. Which should we include? |
|
|
Well, JS Number arithmetic operations round half-even, so maybe you have used it! |
|
@littledan I didn't knew about that. Could you provide some example to illustrate this? |
@qzb it's in the IEEE 754 standard, I believe |
It's not about Math.round, but rather all operations normally. For example, see the definition of multiplication on Numbers, whose last bullet point mentions
I think this mode will be important if we want to support high precision numerical calculations, to reduce errors. |
QuickJS's 2020-01-05 release includes
Personally, with this feedback, I'm leaning towards including |
What do you mean, omit the restricted form? Are you referring to fabrice’s suggestion of only positive exponents? |
@ljharb Yes. |
Of the calculation methods in the current proposal, perhaps the only exotic one is the "partition" method. I haven't seen a method like that in any arbitrary-precision number library, nor does a similar method appear in the General Decimal Arithmetic Specification or any other specification or standard I am aware of. Usually some kind of remainder method or a divide-and-remainder method (e.g., in Java's BigDecimal) occurs in libraries and specifications instead. In any case, the "partition" method, if its definition is as suggested in #13 (comment), could get unwieldy and take unnecessary memory if the number of partitions gets very large. |
I'd like to offer some support for square roots as an "advanced" function that ought to be included in the standard library. One reason for including Another reason: although the bulk of the feedback we've received from JS developers suggests that basic arithmetic (addition, multiplication, etc.) is likely to be sufficient, square roots do indeed pop up occasionally. They are used for computing distances and indeed presenting such numbers for human consumption. |
I'd like to argue that we don't need trigonometric functions in Decimal. The That sounds like a lot of juggling, but it works. I'm having trouble seeing the added value of trig functions in Decimal, given that they already exist out-of-the-box in From a mathematical point of view, the trig functions are (almost) never going to produce an exact decimal value (which are all rational numbers). (By the way, this is as true of BigDecimal as it is of Decimal128.) Thus, the exact semantics that Decimal offers is a bit of an illusion when it comes to such functions. The computation of such functions, whether in decimal or binary floats, is going to have to stop after a certain number of digits, and the result is a best-effort approximation. What are some use cases for trig functions that would require, say, 20 or more decimal digits of precision (something that we could get with Decimal128)? In other words, do |
Just extending my previous argument against the trig functions: I think Decimal also ought to exclude logarithms (whether natural or base ten) and exponentiation (whether natural exponentiation or a two-argument One counterargument I can entertain is that exponentiation and logarithm do show up in some business/financial calculations, such as computing compound interest of an investment, economic growth/decay formulas, and so on. But, again, the counterargument would be: does |
Exponentiation is very critical, and has an operator, **. I don’t see how it can be excluded. |
It's not just a question of accuracy, but of IEEE-754 confusion, especially in the world of finance. Enough so that there are dozens of user-land libs that attempt to address the problem. The issue is especially prevalent with crypto-currency, where figures regularly sit well beyond the upper-bounds of JavaScript's |
One version that I think would be valuable would be to have exponentiation for integer exponents (negative or not). It's a pretty straightforward implementation. Exponentiation for non-integer exponents is a bit more exotic. There are some use cases in business/finance calculations. But the question, to my mind, would be whether a Decimal version of this would have any added value compared to |
Quite right! Decimal does aim to provide a big improvement over JS's |
During BigInt, multiple delegates made clear their extreme discomfort with surprising value-dependent semantics for number operations, so I'm not sure if this would be viable. |
Just to reformulate: Do you mean that we ought to support |
The former, since decimal numbers without exponentiation seems untenable to me. |
The line of reasoning make sense. I'm curious to know why a version of Decimal without exponentiation would be untenable. I find myself conflicted about whether Decimal needs things like exp, log, sqrt, and trig functions. Part of me says "hell yeah!" Another part of me looks at the JS developer survey data and finds that there's very little expressed need for those. What's your take on the argument that, if one needs exp, log, etc., just use |
The primary benefit to me for Decimal is if it can replace Number entirely. |
On BigDecimal.prototype, what sorts of methods should we have, for mathematical calculations?
toPrecision
,toExponential
andtoFixed
(analogous to Number) seem like givens. Other possibilities:quantum
?compareTotal
? (discussed in 11-year-old es-discuss threads)partition
?divmod
?div
? (discussed in BigDecimal with division and other operations which need to round #13)round
?sqrt
? trig fns?The text was updated successfully, but these errors were encountered: