Hi,
First time on here and also new to programming so hopefully you can forgive my poor lingo (language) I'm working on a project that includes the MPU6050 accelerometer. When working with the raw data I receive from the accelerometer I need to manipulate it to get some values that are... lets just say easier to work with.
As part of this I use the pow function that I think and please correct me I'm wrong is part go the math.h library?:/ Anyway the real problem is that I need the function to return both negative and positive values. If I enter a negative number as the base i.e.
`pow (-5.2, 2)`

I get 27.04 returned instead of -27.04. I've had a good look around on the internet and it hasn't returned anything useful yet. Is there something obvious I'm missing or a solution that someone knows of.
Best Regards
Danny J

```
pow (-5.2, 2)
```

...is equivalent to...

```
-5.2 * -5.2
```

...which can never be negative.

The only way to get a negative result after squaring is if you are working with imaginary numbers.

Welcome,

you can do something like this:

```
x = -5.2;
y = pow(x, 2);
if ( x < 0 )
y = -y;
```

or:

```
x = -5.2;
y = x * fabs(x);
```

I get 27.04 returned instead of -27.04

That's because the code is working properly. It is your expectations that are wrong.

Thank Guix and Coding badly, hmmmm (-3)^2 = 9 but -3^2 = -9...... feel cheated with that I was after a more elequant solution though. Can you use brackets in the Pow function ?

Why do you "need" the square of -3 to be -9? It isn't. Period.

-3^2 = -9

That's because when the compiler parses the expression, in the absence of the parentheses, exponentiation has a higher precedence than the unary minus so the expression will give the same result as if it had been written -(3^2).

You can use brackets in the pow function but that isn't going to change the basic truth that -3 squared is +9.

Pete

Its not though :/ work it out (-3)^2 = 9 -3^2 = -9 Its explained here really well http://www.purplemath.com/modules/negative4.htm

Hi,

-3^2 = -9 is not true....

-(3^2)= -9 is what you are trying to do.

You always want a negative number?

-1*(3^2)=-9

Tom...... :)

No it is not, they are really ambiguous as to what the "-" means and its precedence.

I would interpret -3^2 as (-3) * (-3). I would write the other as -(3^2) to remove ambiguity.

In any case, pow(-3,2) is exceedingly clear and will be positive.

Nice explanation on how the function elaborates the input to evaluate an answer though pete. Ha thanks for the reply Tom but no I need neg and pos returns. I've gone with Guix solution with the if statement that works treat.... -3^2 = -9 though on this I know. But after trying a multiple calculators I do get different results ?? :/ ow well ;) depends on how you read it

OOOooopoppsss. I'm forgetting that C doesn't even have an exponentiation operator! :-[

Pete

but no I need neg and pos returns

You want (-3)^2=-9 and 3^2=9 ? If so, WHY?

But after trying a multiple calculators I do get different results ?

Presumably, they differ because they have different rules as to how to parse the expression. If they don't use operator precedence at all then -3^2 is (-3)^2=9 but if they use precedence and ^ is higher than unary minus, then -3^2 = -(3^2) = -9

Pete

ve03vsx: When working with the raw data I receive from the accelerometer I need to manipulate it to get some values that are... lets just say easier to work with.

To get a real answer to your question, you should explain why you are having difficulty working with the values. You got the quick fix, but you probably missed out on the real value of learning a better way.

That is why I like RPN. 8^)

Hmmmm No actually the real answer to my question was answered. Thanks for the off topic confuscious thing you got going on, though like it

ve03vsx: Hmmmm No actually the real answer to my question was answered. Thanks for the off topic confuscious thing you got going on, though :) like it

"Grasshopper", read more threads here and you will understand. :)

As everybody else has pointed out, you cannot correctly take powers of negative numbers without using complex numbers.

The powers of a negative number -x follow the path of a logarithmic spiral around the complex plane centered at 0. Actually, the powers of any complex number s follow any possible logarithmic spiral that passes though 1 and s - it's just that for any s and for a rational value of the power, all those possible spirals intersect at a limited number of locations.

Is anyone else smelling an xy problem here? OP: what are you actually trying to calculate?

Hi,

Are we talking about to the power of , or square root?

Admittedly a number to the power of 1/2 is the square root. But I usually associate power function with a number to the power of an integer greater than or equal to 1.

You got into the world of imaginary numbers when you take the square root of a negative number.

That's how I use it in my simple world..

Tom... :)

PaulMurrayCbr: As everybody else has pointed out, you cannot correctly take powers of negative numbers without using complex numbers.

No one pointed that out because it is wrong for integer powers, which is what is under discussion here. If complex numbers were involved we could not multiply *any* negative numbers without using complex math.

The correct result of -3^2 is -9. It is so because exponentation has a higher precedence than unary minus, so the expression is evaluated as -(3^9). Look here (last paragraph). However, the C power function works with the contents of a variable, so the whole variable, including the minus sign, is multiplied by itself.

To see the difference, call the python shell and try this:

```
>>> -3 ** 2
-9
# but...
>>> a = -3
>>> a ** 2
9
```