Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Calculator Error
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericfourfour




PostPosted: Wed Apr 04, 2007 3:19 pm   Post subject: Calculator Error

This is something that happened in math class today.

If you enter the equation, (-27)^(2/3), into your calculator, you will get an error. However, this should not happen.

What happens when the equation is expanded and then solved?

(-27)^(2/3)
= ((-27)^(1/3))^2)
= (cubed root (-27)) ^ 2
= (-3) ^ 3
= 9

Using the original equation:
My calculator says, "Domain Error"
Turing says, "Attempt to raise a negative number to a real exponent."
it returns 1 in Ruby
Google calculator says, "(-27)^(2 / 3) = -4.5 + 7.79422863 i"

I thought I would post this on a programming forum because I believe this is programming related and has to do with how calculators deal with powers. Does anyone understand why an error is occurring?
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Wed Apr 04, 2007 5:43 pm   Post subject: Re: Calculator Error

ericfourfour wrote:
it returns 1 in Ruby

That's because you did this :
(-27)**(2/3)
And remember Ruby thinks you want an integer result from the "2/3", and will evaluate it to 0. So you're basically doing (-27)**0.

You have to do it like this :
Ruby:
(-27)**(2/3.0)

which will return NaN (which I think means error)

So Ruby also has this problem because, as you pointed out, the answer should be 9. You can't even do this in Ruby :
(-8) ** (1/3.0)

So, I guess Ruby has just disabled negative numbers to be raised to a non-integer exponent because the way they evaluate exponents will lead to incorrect results sometimes.
Skynet




PostPosted: Wed Apr 04, 2007 7:58 pm   Post subject: Re: Calculator Error

Here's my hypothesis:

I think your calculator uses Newton's Method to solve these types of problems.

Newton's Method is a method used for finding approximate roots of a function. (Approximate points at which a function is equal to 0. It's an iterative algorithm that looks like this:

x_n+1 = x_n - f(x_n)/f'(x_n)

x_0 is initialized to some guess (usually not equal to 0), and the formula iterates (n increments) until an end condition is reached. (end conditions may vary)

f(x_n) is the function evaluated at x_n, f'(x_n) is the first derivative at x_n.

So, if we wanted to use this to evaluate powers, we can do this:
x = -27^(2/3)
x^(3/2) = -27
0 = x^(3/2) + 27
f(x) = x^(3/2) + 27

In a general form for Ans = y^x, that would be:
f(x) = Ans^(1/x) - y

Now, we use Newton's Method to find a point at which f(x) = 0, thus making x = -27^(2/3)
f(x) = x^(3/2) + 27
f'(x) = (3/2)x^(1/2)
x_n+1 = x_n/3 - 18/sqrt(x_n)

So far, so good. However, look at the second term- there's a sqrt(x_n). If we don't know how to handle negative square roots, we have problems. Of course, x_n won't be negative all the time, but the way the results jump on their way to the answer means that they will be <0 at some point and we'll have to do a negative square root. Simple calculators and simple math libraries won't cope well with that.

Also, if you're solving this step by step, -27 has two cube roots. One is -3, the other is 1.5 + 2.5981i. Square the second and you get -4.5 + 7.7942i. Notice that this works properly in reverse: (-4.5 + 7.7942i)^(3/2) = -27. If you use -3 as your cube root and square to get 9, it breaks when you try to go backwards: 9^(3/2) = 27.

I wrote a quick MATLAB program to verify this, and it gives me the same answer as Google. You can see the convergence below for a few different guesses. f(x) is approaching 0, and the answer is jumping around in the complex plane (Re(x) and Im(x)). The lines being straight and "rough looking" is good - it means that the algorithm gets the answer very quickly.

Posted Image, might have been reduced in size. Click Image to view fullscreen.
ericfourfour




PostPosted: Wed Apr 04, 2007 8:30 pm   Post subject: RE:Calculator Error

Very nice Skynet! I'm going to have to write this down. I'll try to make sense of it tomorrow.

@richcash Oh, of course! How did I miss that Smile

btw. where I wrote (-3)^3, in my original post, it should be (-3)^2
Skynet




PostPosted: Wed Apr 04, 2007 9:52 pm   Post subject: Re: Calculator Error

Ah, missed something. Newton's method is really simple for square roots:
x_n+1 = 0.5x_n + 0.5c/x_n (c is the number you're taking the square root of)

However, if we want to use it for other exponents, the exponent doesn't go away so nicely. We'll still have to evaluate another exponent in the equation. (It's the sqrt(x_n) in mine) We can do this recursively if we want, but it'll take time.

Here's another idea, and it fits with capabilities a scientific calculator already needs to have around:
a = y ^ x
ln a = ln (y^x)
ln a = x * ln y
a = e ^ (x * ln y)

This needs forward and reverse lookup tables (or formulas) for the natural log and its inverse. Calculators need those already and they can be approximated in quite a few different ways. Since the ln of a negative number is undefined, a domain error makes sense.

One day, I'd like to be able to forget logarithms...I really don't like them. I blame the highschool math curriculum.
Brightguy




PostPosted: Fri Apr 06, 2007 11:35 pm   Post subject: Re: Calculator Error

Note that all nonzero numbers have n distinct nth roots. Any number can be written in the form Posted Image, might have been reduced in size. Click Image to view fullscreen. (for some nonnegative real r and real θ), and the nth roots can be found with Posted Image, might have been reduced in size. Click Image to view fullscreen. by taking k=0,1,...,n-1.

So, if we want to define a root function, which one of these do we take? For positive real numbers, the obvious choice is the unique positive real root. In general, though, I believe it's usually defined as the root with the smallest absolute value of the argument (the polar angle) with the positive ones favored. You can still use the definition Skynet posted, because ln can be defined over the complex numbers if you limit Posted Image, might have been reduced in size. Click Image to view fullscreen. (this is called the principal branch).

The problem is that if n is odd and you are taking the root of a negative number then there will be a real (negative) solution. Often people are only concerned with real solutions, so this is the root they want. Most calculators don't deal with complex numbers anyway so they'll just output the real solution. Mathematically it's kind of ugly to make an exception for these cases. (e.g., Maple and MATLAB don't do it by default.) However, Google does treat it as a special case if an exponent is of the form 1/n with n odd. So you would want to enter ((-27)^(1/3))^2. If you want the usual complex root, use Skynet's definition.

Skynet @ Wed Apr 04, 2007 9:52 pm wrote:
One day, I'd like to be able to forget logarithms...I really don't like them. I blame the highschool math curriculum.

Eh? Exponentiation is one the most important operations in math, so consequently logarithms are as well.
Drakain Zeil




PostPosted: Sat Apr 07, 2007 6:30 am   Post subject: RE:Calculator Error

Highscool is a monopoly for money, not education Razz

Problem with that system? A few teachers care. Otherwise, it would be quite worthless to you.
Skynet




PostPosted: Mon Apr 09, 2007 12:32 am   Post subject: Re: Calculator Error

Brightguy @ Fri Apr 06, 2007 11:35 pm wrote:
Eh? Exponentiation is one the most important operations in math, so consequently logarithms are as well.

Oh, I know...especially after ODE's and my Signals course. Doesn't mean I have to like them. Razz
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: