Why Does Math.pow(Math.cos(2 * Math.PI / 3), 0.84) Return NaN in JavaScript?

Why Does Math.pow(Math.cos(2 * Math.PI / 3), 0.84) Return NaN in JavaScript?
Photo by Joshua Hoehne / Unsplash

When you first start working with JavaScript's math functions, encountering something like NaN (which stands for "Not-a-Number") can be confusing, especially when you think you're working with straightforward numbers. One example of this happens when you try to evaluate something like Math.pow(Math.cos(2 * Math.PI / 3), 0.84). Instead of getting a nice numeric result, you get NaN. Why?

Let’s break this down step by step.

💡
Quick demo here that return NaN.

Understanding the Problem

The expression Math.cos(2 * Math.PI / 3) gives us the cosine of an angle. If you're unfamiliar with trigonometry, cosine is a function that tells you how far along the x-axis a point on a unit circle is for a given angle.

When you compute Math.cos(2 * Math.PI / 3):

  • The angle 2 * Math.PI / 3 corresponds to 120 degrees.
  • The cosine of 120 degrees is -0.5. So, Math.cos(2 * Math.PI / 3) returns -0.5.

That’s where things get tricky.

Raising Negative Numbers to Fractional Powers

Next, you are raising this result (-0.5) to the power of 0.84 using Math.pow(). The problem here is that raising negative numbers to fractional powers doesn’t work in the realm of real numbers. Mathematically, doing so results in complex numbers (numbers that include imaginary parts, like i).

However, JavaScript’s Math.pow() function doesn't handle complex numbers. It expects to deal only with real numbers. When you try to raise -0.5 to 0.84, JavaScript can't produce a valid real number result, and it returns NaN instead. That’s JavaScript’s way of saying, “I don’t know how to deal with this.”

The Broader Lesson on NaN

This isn’t just limited to Math.pow() and cosine. Anytime you attempt operations that don’t have well-defined results in the real number system, JavaScript will give you NaN. You might run into this with square roots of negative numbers, logarithms of non-positive values, or divisions by zero.

For example:

  • Math.sqrt(-1) returns NaN because, again, this is a complex number in math, not something JavaScript’s basic math functions can compute.
  • Math.log(0) returns -Infinity but attempting Math.log(-1) would return NaN.

How to Avoid NaN in This Case?

So, what can you do if you’re running into NaN? Well, one option is to check if the result of Math.cos() is negative before raising it to a fractional power.

Here’s a simple workaround:

let cosValue = Math.cos(2 * Math.PI / 3); // returns -0.5
if (cosValue < 0) {
    // Handle negative case; maybe convert to positive
    cosValue = Math.abs(cosValue);
}
let result = Math.pow(cosValue, 0.84); // Now it works
console.log(result); // Output: a valid number

By converting the negative value to its absolute value, you avoid the issue. This approach makes sure you’re always working with positive numbers, so you won’t run into the problem of fractional exponents on negative bases.

Should You Always Use Absolute Values?

That depends. Using absolute values changes the meaning of the calculation. If your specific problem requires working with negative values and you actually need the result of a fractional exponent applied to a negative base, you may need to work in a system that supports complex numbers.

In JavaScript, there are libraries like math.js that can handle complex numbers:

const math = require('mathjs');

let cosValue = math.cos(2 * math.pi / 3); // -0.5
let result = math.pow(cosValue, 0.84); // Complex number result
console.log(result.toString()); // A complex number output

With libraries like these, you can expand beyond real numbers and start dealing with the more advanced math concepts like imaginary and complex numbers.

The ESM version is below.

import { cos, pi, pow } from 'mathjs';

const cosValue = cos(2 * pi / 3); // -0.5
const result = pow(cosValue, 0.84); // Complex number result
console.log(result.toString()); // A complex number output
💡
Here is the demo that using mathjs

A Few More Things to Keep in Mind

  1. Precision Matters: When working with trigonometric functions in JavaScript, the values you get are often not perfect. Due to the way floating-point numbers are handled in computers, the result of Math.cos(2 * Math.PI / 3) might be slightly off from -0.5. This usually isn't a big deal, but it’s good to be aware of.
  2. Handling Edge Cases: Always think about edge cases where your calculations might return unexpected values like NaN or Infinity. For example, using logarithms on negative numbers, taking square roots of negatives, or dividing by zero. You can guard against these with simple checks like:
if (value < 0) {
    // Handle negative values
}

or

if (isNaN(result)) {
    console.log("Something went wrong in the calculation!");
}
  1. Trigonometry: If you’re not familiar with trigonometry, the key functions you'll run into are sin, cos, and tan, all of which work on angles. It’s helpful to know that these functions can return negative values, depending on the angle.

Finally

The NaN you see when running Math.pow(Math.cos(2 * Math.PI / 3), 0.84) happens because JavaScript can’t handle raising a negative number to a fractional power without getting into complex numbers. The simplest solution is to work with the absolute value if you want to avoid NaN. If complex numbers are important to you, you can look into specialized libraries like math.js for more advanced computations.

Understanding where NaN comes from and why it appears is important for anyone working with math in JavaScript. It’s not always an error; often, it’s the system’s way of telling you that you’re asking for something it doesn’t know how to compute in the real world of numbers.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe