Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interesting Maths (**) Question (-1.#IND)

Status
Not open for further replies.

timtrust123

Programmer
Dec 19, 2003
19
GB
If i run the following code

$number = (5.74462/100 - 1.0001);
print $number ** 0.56,"\n";

I get the this printed "-1.#IND"

However, if i run the following (change the $number to a number)

$number = 0.9426538;
print $number ** 0.56,"\n";

I get the correct answer printed (-0.26646)

It seem that if it load a number directly into the variable there is no problem but if the variable is based on a formula it does not work.

Any Maths experts out there who can explain what is happening.

Cheers

Tim
 
In your first example, $number is negative 0.9426538 but you have a positive number in you hard-coded $number. Mathematically, by definition you can't raise a negative number to a fractional power, so that's what your problem is.
 
In your first example, (5.74462/100 - 1.0001) evaluates to a negative number. In your second example, your value is positive.

If you run it with $number = -0.9426538; you get the same error.

Raising a number to a fractional power is the same as taking the root {x ** 0.5 == sqrt(x)}. The error is probably because the implementers know that you can't have a (real) root of a negative number.
 
Yep.
You're info is not quite correct.
The result of (5.74462/100 - 1.0001) is actually [red]-[/red]0.9426538 and if you put that in $number you end up with an error also.
The reason is that you are raising to a fractional power (effectively a root (almost like square root)) but you cannot take the root of a negative number because no number multiplied by itself will ever generate a negative answer.

So there you go! :)


Trojan.

 
Crikey!
We all got there almost at the same time!
Drinks all round maybe?!!!


hehehe


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top