Why are there such tricks with degrees?

 

I need to write a function that works with powers and in the process I've discovered something interesting:

If I substitute a negative fractional number to a negative fractional power, MQL5 writes -nan.

double n = MathPow(-5.5,-0.2);

I checked it in C++ and got the same result (I don't know what version of the language was used, because I was too lazy to compile and I've just tested it by cpp.sh).

#include <iostream>
#include <string>
#include <tgmath.h>

int main()
{
    double _pow = pow(-5.5,-0.2);    
  
  std::cout << _pow << "\n";
}


I think there is no error in MQL5 if the answer is the same in pluses! But the question is why it happens that way? If we do the maths then the answer should be there. Mathematically, a negative number is equal to one divided by the same number to the positive power. In other words:

-5.5^-0.2 = 1/(-5.5^0.2) = −0,711094733;

And the calculator on the computer confirms it. So what is this trick with grades?

 
double n = -1/MathPow(5.5, 0.2);
Print(n);
 
Andrey Azatskiy:

I need to write a function that works with powers and in the process I've discovered something interesting:

If I substitute a negative fractional number to a negative fractional power, MQL5 writes -nan.

I checked it in C++ and got the same result (I don't know what version of the language was used, because I was too lazy to compile and I've just tested it by cpp.sh).


The pluses give the same answer, there is no error in MQL5! But what is the reason for such an answer? If we use mathematics, the answer should be given. Mathematically, a number to the negative power is equal to one divided by the same number to the positive power. In other words:

And the calculator on the computer confirms it. So what is this thing with degrees?

In R:

(-5.5)^-0.2=NaN

-5.5^-0.2=-(5.5^-0.2)=-0.7110947

 
Aleksey Nikolayev:

in R:

(-5.5)^-0.2=NaN

-5.5^-0.2=-(5.5^-0.2)=-0.7110947

Perhaps the legs are growing from the fact that you can't take an even root from a negative number ? I'm already somewhat confused... And most importantly how to get around it ?

 
Andrey Azatskiy:

And most importantly how to get around it ?

m = 2

n = 10

double result = pow(pow(-5.5,2.0),-0.1); 
Print("result = ",result);                      //result = 0.7110947333604484
without brackets, the minus sign can be interpreted differently, imho
 
Roman:

There will be a problem with the signs. The root of an even power is always positive

 
Andrey Azatskiy:

There will be a problem with the signs. The root of an even power is always positive.

In calculator where the degree is fractional

1/(-5.5 ^ -0.2)
-0,7110947333604484236326007791589

In mql where the degree is fractional

double n = 1/MathPow(-5.5, -0.2);
Print(n);

zero divide in 'Pow.mq5' 

In mql where the degree is integer.

double n = 1/MathPow(-3.0, -3.0);
Print(n);

-27.0

Conclusion, the function does not correctly count fractional powers, and returns zero divide.

 
Igor Makanu:

m = 2

n = 10

without brackets the minus sign can be interpreted differently, imho

Thank you for the answer, but in general if we take an arithmetically correct solution, it seems that only complex numbers can be used to implement such a solution... In your proposed method it is necessary to divide the degree so that the underlying value always has a positive degree and the answer will always be positive. But if you take without this fitting - we come only to complex numbers, because according to generally accepted algebraic model as far as I know (I'm not a mathematician by education) - the root of a negative number will be a complex number.

 
Roman:

In mql where is the fractional degree

In mql where the degree is fractional

In mql where the degree is integer.

Conclusion, the function does not correctly count fractional degrees, and returns zero divide.

The function is correct. The standard maths just doesn't work there)

 
Correct me if I misunderstood, maybe there is a solution outside the complex numbers after all ?
 
Andrey Azatskiy:

The function is correct. The standard maths just doesn't fit there)

-0.2 is not zero to return a division error by zero.

Reason: