eobermuhlner/big-math

Doesn't work for the code below

Closed this issue · 3 comments

BigDecimal number = new BigDecimal("-68.42");

BigDecimal otherNumber = new BigDecimal("0.30274591");

BigDecimalMath.pow(number,otherNumber,new MathContext(100));

The Android's calculator can handle such an expression:

whatsapp image 2018-09-02 at 16 00 36

PS.: version: 2.0.1

Hi Daniel

Thanks for proposing this extension.

This was a bit of a difficult decision, because as you say some calculators and programming languages actually do provide this feature while other don't.

From my understanding I would say that the power of negative values to a fractionaly exponent is actually undefined in the domain of real numbers.
https://en.wikipedia.org/wiki/Exponentiation#Real_exponents_with_negative_bases

Because of this I decided to not implement this special case.
This is also consistent with the behaviour of Math.pow() which returns NaN.

Please note that BigDecimalMath.pow() works as expected for integral values -> even exponents give a positive result, odd exponents give a negative result for a positive base.

If you need the correct results for these cases you should use the class BigComplexMath which calculates the results in the complex domain.

By the way - did you verify that the result given by the Android Calculator is actually what you think it is?
(on my Android phone another calculator is preinstalled so I cannot test this)

On most calculators the ^ operator has higher predence than - so what they show for the input
-3^2.5
is actually
-(3^2.5)
and not
(-3)^2.5

I really did not pay attention to the domain and also when typing the parentheses in the android calculator. Thank you very much for the explanation.