logo

Math pow() methode in Java met voorbeeld

De java.lang.Math .pow() wordt gebruikt om een ​​getal te berekenen dat tot de macht van een ander getal stijgt. Deze functie accepteert twee parameters en retourneert de waarde van de eerste parameter verhoogd naar de tweede parameter. Er zijn enkele speciale gevallen, zoals hieronder vermeld:

  • Als de tweede parameter positief of negatief nul is, is het resultaat 1,0.
  • Als de tweede parameter 1,0 is, is het resultaat hetzelfde als dat van de eerste parameter.
  • Als de tweede parameter NaN is, zal het resultaat ook NaN zijn.
  • De functie java.lang.Math.pow() retourneert altijd een dubbel datatype.

Syntaxis :



 public static double pow(double a, double b) Parameter : a : this parameter is the base b : this parameter is the exponent. Return : This method returns ab.>

voorbeeld 1 : Om de werking te laten zien java.lang.Math.pow() methode.

Java




verilog altijd





// Java program to demonstrate working> // of java.lang.Math.pow() method> import> java.lang.Math;> class> Gfg {> >// driver code> >public> static> void> main(String args[])> >{> >double> a =>30>;> >double> b =>2>;> >System.out.println(Math.pow(a, b));> >a =>3>;> >b =>4>;> >System.out.println(Math.pow(a, b));> >a =>2.5>;> >b =>6.9>;> >System.out.println(Math.pow(a, b));> >}> }>

>

js settime-out

>

Uitgang:

900.0 81.0 556.9113382296638>

Tijdcomplexiteit: O(logboek(b))

Hulpruimte: O(1)

Java




// Java program to demonstrate working> // of java.lang.Math.pow() method> import> java.lang.Math;>// importing java.lang package> public> class> GFG {> >public> static> void> main(String[] args)> >{> >double> nan = Double.NaN;> >double> result;> >// Here second argument is NaN,> >// output will be NaN> >result = Math.pow(>2>, nan);> >System.out.println(result);> >// Here second argument is zero> >result = Math.pow(>1254>,>0>);> >System.out.println(result);> >// Here second argument is one> >result = Math.pow(>5>,>1>);> >System.out.println(result);> >}> }>

int in tekenreeks

>

>

Uitgang:

NaN 1.0 5.0>

Tijdcomplexiteit: O(logboek(b))

Hulpruimte: O(1)