Actions
Bug #17214
closedBigDecimal exponentiation gives incorrect results
Bug #17214:
BigDecimal exponentiation gives incorrect results
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
Description
This is an incorrect value:
This is the correct value (within Float precision):
As the Base gets larger, the problem is more visible. Wrong value, number of trailing zeroes increase:
Nearing maximum Float precision:
Updated by sawa (Tsuyoshi Sawada) almost 6 years ago
- Description updated (diff)
Updated by nobu (Nobuyoshi Nakada) almost 6 years ago
Updated by karatedog (Földes László) almost 6 years ago
If I increase the precision of the two arguments ("2222", "3.5"), but leave the operation's precision on default, the result still will be wrong.
This is incorrect with #power as well:
So the good result relies only on the precision of the operation which is not straightforward immediately for the developer (because the statement will return a value).
To sum up:
- Only #power can be used for proper exponentiation, as #** cannot be given any argument and thus will give almost always wrong results if used with large numbers. Existing code might need to be rewritten, changing class from Float to BigDecimal will not be idempotent.
- BigDecimal is advertised as an "arbitrary precision library" yet using the defaults is less precise than Float. This is misleading, it will bite newcomers in the back.
I think this should be documented and/or fine-tuned, the above two violates the Principle of Least Surprise.
Actions