How to compare 2 potentials

Asked

Viewed 681 times

1

I need to compare 2 potentials that are not formatted..

Example:
2,2 = 2²
3,3 = 3³

if (2² > 3³)

the numbers arrive in an array, it comes like this [2,2,3,3], I need to separate them in pairs and I need to turn a power...

  • 1

    How do you want to represent them before formatting? Two numbers in an array [2,2], a string "2,2", another format?

  • 1

    Well, the simplest thing seems to be to calculate them and compare the results: if ( Math.pow(arr[0], arr[1]) > Math.pow(arr[2], arr[3]) ). P.S. Could anyone comment on the reason for the votes against?

  • You can use the function Math.pow(base, expoente);

  • 1

    Our @mgibsonbr, the phrase in my reply was almost identical to your comment.

  • 2

    And, if the potentials are too large, which can overflow, Voce can compare the logs.

  • 2

    Curiosity: If you are interested in the mathematical aspect, How to compare powers: http://math.stackexchange.com/questions/97022/comparing-numbers-in-form-xy

Show 1 more comment

1 answer

7

Use Math.pow to calculate power, and compare results. For example:

var entrada = [2,2,3,3];
// confiando que entrada.length >= 4
if(Math.pow(entrada[0], entrada[1]) > Math.pow(entrada[2], entrada[3])) {
   // a primeira dupla é maior
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.