Skip to content
Advertisement

How do I handle cases where using a fractional exponent gives me NaN?

My Javascript code is trying to multiple three decimal or integer numbers and raise them to the exponent 0.16 but the exponentiation results in NaN. In the specific case I am testing, the three decimal numbers I’m multiplying are the constant 0.3965, and the variables -40 and 40 which yields a product of -634.4 (to one decimal place). The formula in question is the first one shown (the one used by Environment Canada) in this subsection of the Wikipedia article on Wind Chill and I’m getting the NaN on the fourth term of the equation.

After some googling, I’ve learned that when an exponent is fractional, the result is sometimes a real number and sometimes an imaginary number. Am I right in assuming that I’m getting NaN when the result is an imaginary number?

Regardless of why I’m getting NaN, how do I change my code so that I get a meaningful result that is a number, at least for reasonable values of the variables like I am using? I have no prior experience with exponentiation of a fractional number in Javascript (or any other programming language for that matter).

Here’s the essence of my code:

var TempC = -40; //Temperature in Celsius
var WindKph = 40; //Wind speed in kph
var TempWindChill; //Temperature in Celsius with wind chill applied
TempWindChill = 13.12 + (0.6215 * TempC) - ((11.37 * WindKph) ** 0.16) + ((0.3965 * TempC * WindKph) ** 0.16);
console.log("TempWindChill = " + TempWindChill);

If you’d like to play with the code a bit, you can clone it here.

Also, I’m puzzled by a related matter. When I look at the arithmetic operators in Javascript (at W3Schools), there is no exponentiation operator! I can successfully exponentiate in Javascript using the ** operator yet that operator isn’t listed in the documentation. Here’s a link to the W3Schools documentation. Is their documentation just faulty or is exponentiation a feature that is not officially present in the language?

Advertisement

Answer

Your formula for Windchill is incorrect, hence the complex number issue that Jiří Cihelka cites in their answer.

TempWindChill = 13.12 + (0.6215 * TempC) - ((11.37 * WindKph) ** 0.16) + ((0.3965 * TempC * WindKph) ** 0.16);

Referencing this site: Should be:

Wind chill = 13.12 + 0.6215T – 11.37 (V^0.16) + 0.3965T (V^0.16)

Let’s fix your code:

var TempC = -40; //Temperature in Celsius
var WindKph = 40; //Wind speed in kph
var v = WindKph ** .16;
var TempWindChill = 13.12 + .6215*TempC - 11.37*v + .3965*TempC*v;
console.log("TempWindChill = " + TempWindChill);

result:

TempWindChill = -60.873447728482546

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement