Languages
[Edit]
DE

JavaScript - wie berechnet man die Wurzel eines beliebigen Grades?

3 points
Created by:
Nikki
10520

Mit JavaScript ist es möglich, die Wurzel eines beliebigen Grades auf folgende Weise zu berechnen.

1. Math.pow Methode - Beispiel

// ONLINE-RUNNER:browser;

function calculateRoot(value, degree) {
    return Math.pow(value, 1.0 / degree);
}

// Beispiele:

// Grade = 2
console.log( calculateRoot(  9  , 2 ) ); //  3
console.log( calculateRoot(  4  , 2 ) ); //  2

console.log( calculateRoot(  2  , 2 ) ); //  1.4142135623730951
console.log( calculateRoot(  0.5, 2 ) ); //  0.7071067811865476
console.log( calculateRoot(  0  , 2 ) ); //  0
console.log( calculateRoot( -1  , 2 ) ); //  NaN

// Grade = 3
console.log( calculateRoot(  8  , 3 ) ); // 2
console.log( calculateRoot(  27 , 3 ) ); // 3

console.log( calculateRoot(  2  , 3 ) ); // 1.2599210498948732
console.log( calculateRoot(  0.5, 3 ) ); // 0.7937005259840998
console.log( calculateRoot(  0  , 3 ) ); // 0 
console.log( calculateRoot( -1  , 3 ) ); // NaN

// Float-Grad
console.log( calculateRoot(  25 ,  0.5   ) ); // 625
console.log( calculateRoot(  25 , -0.5   ) ); // 0.0015999999999999999
console.log( calculateRoot(  25 ,  0.005 ) ); // 3.872591914849318e+279

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join