Sign in to follow this  
Followers 0
valkk

Raising a "negative" number to the power problem

10 posts in this topic

#1 ·  Posted (edited)

I keep getting -1.#IND as a result for raising a negative number to a power.

Positive numbers work, but negative don't.

4^2=16

3^3=27

(-4)^2=16 ( I get 1.#IND )

(-3)^3=-27 ( I get 1.#IND )

Ironicly if you calculate it manually in autoit, it works ^^.

(-4)*(-4)=16

(-3)*(-3)*(-3)=-27

Makes no sense. Why is this and how do I get around it? You can't square root a negative number, but you can sure raise it to a power.

Edited by valkk

Share this post


Link to post
Share on other sites



I keep getting -1.#IND as a result for raising a negative number to a power. 

(-4)^2=16 ( I get 1.#IND )

<{POST_SNAPBACK}>

and it does work when you do:

0-4^2

:ph34r:


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Wouldn't the order of operations make that:

0 - ( 4 ^ 2) ??

<{POST_SNAPBACK}>

No, I think he means (0 - 4) ^ 2

Share this post


Link to post
Share on other sites

No, I think he means (0 - 4) ^ 2

<{POST_SNAPBACK}>

Sure, that is what he means, but it too fails just the same as (-4) ^ 2 fails. Try this:

$ret = (0 - 4) ^ 2
MsgBox(0, "", $ret)

Same problem.


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

True... I wonder why....

Because I assumed that AutoIt handles all math.

(Can't make a good English sentence of it...)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If we remember our algebra lessons, raising an expression x to a negative power is equivalent to dividing 1 by your x expression. However, if your x expression reaches a zero value, your division will not be defined, so, one way of solving your problem is to divide 1 by your x expression and making sure X does not reach a zero value.

IVAN

Edited by ivan

Share this post


Link to post
Share on other sites

0-4^2 will do the power first (remember order of operations) and then the subtraction. That is, 0 - (4^2) = 0 - 16 = -16.

Hmm, Negative numbers to integer powers should work, but they do not. The problem comes from the situation when a negative is raised to a non-integer power. The results can be quite complex (as in the complex number set). I will check what is happening and see if I can improve it.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

I have submitted a fix for this problem. I expect it will appear in the unstable soon.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0