# Are you a math genius?

• Do you consider yourself a math genius? Even if you're not, see if you can answer this question:

What is -12?

If
you're stumped, well, I hear great things about McDonald's Hamburger
University. For you learnèd scholars, congratulations; you're better at
math than a \$15,000 calculator.

I spent all day trying to figure
out why our company's robot was faulting out for apparently no reason,
and narrowed it down to one line of code I had been sure wasn't the problem. It had executed fine other times:

Variable1 = Variable1 ^ 2

After
scouring the 500MB of documentation for information about the caret
operator, I accidentally found a caveat in the small print about the
"POW(base,exponent)" function: "In an exponential calculation, a domain
error occurs if the first argument is negative." Rather than fix a
small, embarrassing shortcoming of their robot with an otherwise
relatively rich set of mathematical functions, they chose to document
it in as obscure a way as possible.

• -1^2 is -1.  (-1)^2 is 1.

And it's not as easy a fix as you think.  Sure, with even powers it looks easy, but how about (-1)^(0.5) ?

They probably copied from the GNU documentation

Mathematically, `pow` would return a complex number when <var>base</var>
is negative and <var>power</var> is not an integral value. `pow` can't
do that, so instead it signals a domain error. `pow` may also
underflow or overflow the destination type.

• @fluffy777 said:

-1^2 is -1.  (-1)^2 is 1.

And it's not as easy a fix as you think.  Sure, with even powers it looks easy, but how about (-1)^(0.5) ?

They probably copied from the GNU documentation

Mathematically, `pow` would return a complex number when <var>base</var>
is negative and <var>power</var> is not an integral value. `pow` can't
do that, so instead it signals a domain error. `pow` may also
underflow or overflow the destination type.

And thats why you probably shouldn't use pow when the second argument is just an integer.

• Ugh, I didn't say anything about the exponent. The function only allows unsigned integers for the exponent, which is reasonable. But considering it can do all the trig functions, it should be able to figure out that -1 ^ 2 is 1 and that -1 ^ 3 is -1.

And it's not a C-derived language (notice no semicolon), it's based off of Visual Basic. Not that having a weird order of operations has anything to do with the complexity of the POW function implementation, anyway.

• yeah, I'm gonna say that's a bit of a WTF there. Why not just give an error if the exponent isn't an integer, otherwise perform the calculation? There's nothing difficult about -1^1, -1^2, -1^3, -1^0, -1^-1, -1^-2, etc.

FYI: in Javascript:
Math.pow(-1, 2) == 1
Math.pow(-1, 0.5) == NaN

• <sarcasm>TRWTF is in Javasript Math.pow(-1, 0.5) == NaN   and not 'i' </sarcasm>

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.