0

currently working my way through the superb CS:APP whereupon a curious question arose while doing some two's complement exercises.

GNU bc 1.06, default settings - no flags:

-2 ^ 3
-8

... but then ...

-2 ^ 4
16

Question

Why is -2 ^ 4 equal to positive 16? I plugged this into Google's calculator function, and I did indeed get -16.

I've probably left the lens cap of my mind on again (with respect to Pinky & the Brain), but any hints on this behaviour is appreciated.

Thanks

sc.

swisscheese
  • 321
  • 3
  • 12

4 Answers4

4

Because (-2)·(-2)·(-2)·(-2) = 16

mike
  • 1,956
  • 16
  • 22
4

You most likely made a mistake while typing the equation. I bet what you evaluated has been the following:

-(24) = -1 × 24 = -16

When solving an exponentiation with a negative base, there's one simple and generic rule:

  • If your exponent is odd, your result will be negative.
  • If your exponent is even, your result will be positive.

This is simply due to the fact that multiplying a negative number with another negative number gets you a positive number.

-1 × -1 = 1

-1 × 1 = -1

-21 = (-2)

-22 = (-2) × (-2)

-23 = (-2) × (-2) × (-2)

-24 = (-2) × (-2) × (-2) × (-2)

Let's start with one of those multiplications:

(-2) × (-2) = 4

Therefore:

(-2) × (-2) × (-2) × (-2) = 4 × 4 = 16

Edit:

Out of interest, I've tried to put this into the exceptional Wolfram Alpha and it gets confused in a similar way:

  • (-2)^x (interpreted as (-2)x)

    (-2)^x

  • -2^x (interpreted as -(2x))

    -2^x

Community
  • 1
  • 1
Mario
  • 35,726
  • 5
  • 62
  • 78
  • 1
    I love the effort you put into this. – David May 18 '14 at 08:42
  • is probably better to say that this is a power function and if the exponent is even, the function is even, if the exponent is odd, the function will be an odd function. Not using positive, negative, or exponentiation terms. – user2485710 May 18 '14 at 08:43
  • @user2485710 that would assume that the user asking the question understands the terms power function, even function and odd function, which I don't think we could take for granted in this case. – David May 18 '14 at 08:44
  • @user2485710 And how will that help you? What is an *even function* or an *odd function*? – Mario May 18 '14 at 08:44
  • @Mario it's a more precise terms for what you are saying, "If your exponent is odd, your result will be negative" it's not strictly correct in math terms. But this is just me being pedantic. – user2485710 May 18 '14 at 08:46
  • @user2485710 Correct, but to me what I've said is more basic and easy to understand and obviously something the OP didn't understand (or notice) so far. As such being more precise won't help here, since that's the initial problem. :) – Mario May 18 '14 at 08:47
  • Thank you @Mario for the superb answers, and thanks everyone else for your suggestions! – swisscheese May 18 '14 at 09:26
2

Google said it is -16 because it interprets the question as -(2^4) which is -(16)

FaceySmile
  • 53
  • 9
1

Apparently, bc gives the minus sign of negation precedence over multiplication operations, that is, it treats this minus sign as part of the number. I believe the opposite convention is more widespread, that is, treat negation as a subtraction from zero and thus give exponentiation precedence over the negation.

And indeed, in bc 0-2^4 returns -16 as expected, whereas 0+-2^4 is not a syntax error and again gives 16 as result.

So both readings of -2^4 are correct in the corresponding conventions, which one is chosen is a matter of taste, the only important thing is to be consistent about it, and to properly announce it if the bc variant is used.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51