-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
math.numeric(x, 'bigint') produces inconsistent/erroneous results for other custom numeric types. #3366
Comments
Note there are similar (but not quite identical) bugs in the "constructor" function |
Adds options to the bigint constructor function `math.bigint()` to control whether/how non-integer inputs are rounded or errors thrown, and whether "unsafe" values outside the range where integers can be uniquely represented are converted. Changes `math.numeric(x, 'bigint')` to call the bigint constructor configured to allow unsafe values but throw on non-integers (closest approximation to its previous buggy behavior). Also restores documentation generation for constructor functions, and initiates History sections in function doc pages. Resolves josdejong#3366. Resolves josdejong#3368. Resolves josdejong#3341.
Thanks Glen. I see the implementation of |
Yes #3375 does exactly that, so I think when we get it whipped into shape, this will be resolved. |
Ah but you also want to switch numeric to round rather than throwing? That's fine, just a change in the config passed to the bigint constructor. |
Yes I would love to have the function numeric round when converting input to |
Describe the bug
When
x
has a type other than string or number,math.numeric(x, 'bigint')
does not necessarily succeed and when it does return a value, the value may not be correct.To Reproduce
Discussion
All math types are being converted to bigint via
BigInt(x)
. I believe that is the built-inBigInt
, and I think that when x is not numeric it is converting to string and then from that to bigint (?). The BigNumber and Fraction classes were never designed to support the built-in conversion in this way via BigInt. We should instead have dedicated conversions for each of our custom numeric types.I am comfortable with the conversions throwing when their arguments are not integers, but this should happen consistently.
The conversions should preserve the full precision of the argument, whatever it may be.
For conversions from number and BigNumber outside their respective ranges of being able to represent each distinct integer precisely, there is a question of what the behavior should be: (A) throw, (B) warn, or (C) silently return the integer nominally represented by that value, even though it may be a non-fully-precise result of an apparently exact integer calculation (like
3**50
). I think the current behavior is (C) under the philosophy that this is an explicit, rather than implicit, conversion request. (If so, then there needs to be a way for the user to do a "safety check". That exists when the operand isnumber
with JavaScript's MAX_SAFE_INTEGER; is there something corresponding for BigNumber?)I will submit a PR fixing these issues, adhering to the apparent design decisions detailed above; if a different design is preferred (e.g. converting to the floor of the operand when it is non-integer, or (A) or (B) instead of (C) when the operand is outside a "safe integer" range), I can adjust the PR.
The text was updated successfully, but these errors were encountered: