I decided the other day to make for myself a small, but very convenient bike for calculating all the useful mathematical functions. It is worth noting that I am writing in different languages, and this time the choice fell on C ++. I drank, which means that this wonderful three-wheeled vehicle and at the same time I am engaged in unit testing of newly created functions ... And here you are, hello - one of the tests gives me a completely different result than I expected. Ready?!
Zero to degree zero equals one !!! (significant tragic glance)
Well, I think it happens all sorts of things. I decided to check the function pow () separately, naively believing that I would at least catch an exception:
#include <cmath> #include <iostream> #include <stdexcept> int main(void) { try { std::cout << "zeroth power of zero: " << std::pow(0, 0) << std::endl; } catch(std::exception &ex) { std::cerr << ex.what() << std::endl; } return 0; }
Expectations were confirmed - no exceptions (all in full compliance with the documentation), the result in the console - persistent unit.
')
Beg a small digression for a moment of science. If it is very short and simplistic, then:
Exponentiation is a binary operation in which the number
a is multiplied by itself as many times as indicated in exponent
b . This is written as
a b and reads "
a to the power of
b ". For example:
3 5 = 3 * 3 * 3 * 3 * 3 or
5 2 = 5 * 5But what if the exponent is zero? What should the record look like after the equal sign? .. The solution turned out to be quite simple - due to the properties of the degree, the number
a b can be represented as
a b = a c * a (bc) . For clarity:
4 7 = (4 * 4 * 4) * (4 * 4 * 4 * 4) = 4 3 * 4 4The number in the zero degree can be represented as
a 0 = a b * a -b . But what is this negative degree how to understand? The answer is quite simple: the opposite of multiplication is division. Thus, a number in the negative degree means a unit divided by a number in the positive degree -
a -b = 1 / a b .
Based on the above, we can draw a rather simple conclusion:
a 0 = a b * a -b = a b / a b = 1But what if a = 0? A rather unpleasant situation arises - the division of zero by zero, into which division is strictly forbidden under the fear of the occurrence of supermassive black holes.
There are other methods for calculating zero to zero degree and some give a unit, others zero, and others again divide by zero. Such a situation, when something cannot be calculated, is called
uncertainty in mathematics (it is impossible to determine unequivocally).
Well, a brief educational program is over.
Now let's go back to programming ... I decided to check how things are in other languages and started Python:
import math try: print("0**0:", 0**0) print("pow(0, 0)", math.pow(0, 0)) except: print("Exception")
And this snake gives out one and no errors. But what such? Got into these your Internet and came across here such a
list . After all, the guys have sub-foiled us - in almost all languages, a unit is issued, instead of the expected error, such as when dividing by zero.
And this is found in browser calculators of the same corporation of good, Yandex, standard system. And this, in my humble opinion, is a damn serious mistake. Therefore, do not be surprised when you see in the code:
T correct_pow(T a, T b) { if(a == 0 && b == 0) { throw zero_power_zero(); } } return(pow(a, b));