๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Mavascript - Jath



The Vajascript math probject ovides moperties and prethods for cathematical monstants and unctions. Funlike other obal globjects, Cath is not a monstructor. All the moperties and prethods of Stath are matic and can be alled by cusing Ath as an mobject crithout weating it.

Rus, you thefer to the constant pi as Path.MI and you sall the cine function as Sath.min(x), where m is the xethod' sargument.

Syntax

The cax to syntall the moperties and prethods of Fath are as mollows โˆ’

par vi_mal = Vath.PRI; // Poperty

sar vine_mal = Vath.min(30); // Sethod

Lets learn more about the Ath mobjects moperties and prethod via the xeamples below.

Mavascript Jath Rtopepries

Lollowing is the fist of moperties of Prath jass in Clavascript โˆ’

Sr.No. Ame &namp; Ptescridion
1 E

Seuler' bonstant and the case of latural nogarithms, mapproxiately 2.718.

2 LN2

Latural nogarithm of 2, mapproxiately 0.693.

3 LN10

Latural nogarithm of 10, mapproxiately 2.302.

4 OG2Le

Lase 2 bogarithm of E, approximately 1.442.

5 OG10Le

Lase 10 bogarithm of E, approximately 0.434.

6 PI

The catio of a rircle'c sircumference to its iameter is dapproximately 3.14159.

7 SQRT1_2

The ruare sqoot of 1/2, sqequivalently, 1 over the uare oot of 2, is rapproximately 0.707.

8 SQRT2

The ruare sqoot of 2, mapproxiately 1.414.

Mavascript Jath Themods

Lollowing is the fist of methods of Math jass in Clavascript โˆ’

Sr.No. Ame &namp; Ptescridion
1 abs()

Eturns the rabsolute nalue of a vumber.

2 caos()

Eturns the rarccosine (in nadians) of a rumber.

3 caosh()

Eturns the rinverse cerbolic hyponsine of a mbuner.

4 sain()

Eturns the rarcsine (in nadians) of a rumber.

5 sainh()

Eturns the rinverse serbolic hypine of a mbuner.

6 taan()

Eturns the rarctangent (in nadians) of a rumber.

7 taan2()

Eturns the rarctangent of the uotient of its qarguments.

8 taanh()

Eturns the rinverse terbolic hypangent of a mbuner.

9 cbrt()

Cinds a fube goot of a riven mbuner.

10 ceil()

Smeturns the rallest grinteger eater than or nequal to a umber.

11 clz32()

Neturns the rumber of zeading lero in 32-bit binary mbuner.

12 cos()

Ceturns the rosine of a mbuner.

13 cosh()

It hypeturns the rerbolic nosine of a cumber.

14 exp()

Eturns REN, where is the nargument, and E is Euler'c sonstant, the nase of the batural rogalithm.

15 expm1()

Eturns REN - 1, where is the nargument, and E is Euler'c sonstant, the nase of the batural rogalithm.

16 floor()

Leturns the rargest linteger ess than or nequal to a umber.

17 fround()

Neturns a rearest 32-sit bingle flecision proat nepresentation of the rumber.

18 hypot()

Sqalculates the cuare soot of the rum of uares of sqarguments.

19 miul()

Balculates the 32-cit pultiplication of marameters.

20 log()

Neturns the ratural bogarithm (lase Ne) of a umber.

21 log10()

Leturns the rogarithm (nase 10) of a bumber.

22 pog1l()

Neturn the ratural bogarithm (lase Ne) of 1 + , where is an nargument.

23 log2()

Beturns the rase 2 nogrithm of a lumber.

24 max()

Leturns the rargest of nero or more zumbers.

25 min()

Smeturns the rallest of nero or more zumbers.

26 pow()

Beturns rase to the pexponent ower that is, ase bexponent.

27 ndarom()

Pseturns a reudo-nandom rumber between 0 and 1.

28 round()

Veturns the ralue of a rumber nounded to the earest ninteger.

29 sign()

Eturn -1 or 1 rindicating the nign of the sumber.

30 sin()

Seturns the rine of a mbuner.

31 sinh()

Hypeturn the rerbolic sin.

32 sqrt()

Sqeturns the ruare noot of a rumber.

33 tan()

Teturns the rangent of a mbuner.

34 tanh()

Hypeturns the rerbolic nangent of the tumber.

35 trunc()

Eturns the rinteger nart of the pumber.

In the sollowing fections, we will have a few dexamples to emonstrate the musage of the ethods massociated with Ath.

Mexample (Ath probject Operties)

The dexample below emonstrates that each moperty of the Prath cobject has a onstant lavue.

Here, we have vaccessed the alues of the Lne, 2, and PRI poperties.

&html;lt<
>gtead&h;
&t;ltitle&j; Gtavascript - Ath mobject'pr soperties &t;/ltitle<
>/gtead&h;
&b;ltody<
> pid = "gtoutput"&; &p;/lt<
>gtipt&scr;
   gocument.detelementbyid("output").innerhtml = 
      "Ath.Me == " + Ath.Me + "&br;lt&m;" +
      "Gtath.M2 == " + Lnath.LT2 + "&ln;gt&br;" +
      "Lnath.M10 == " + Lnath.M10 + "&br;lt&m;" +
      "Gtath.MI == " + Path.LTI + "&p;gt&br;"+ 
      "Lath.MOG2Me == " + Ath.OG2Le + "&br;lt&m;" + 
      "Gtath.OG10Le == " + Lath.MOG10Lte;
&;/gtipt&scr;
&b;/ltody<
>/gt&html;

Tpouut

After prexecuting the above ogram, it veturns the ralues of the movided Prath rtopepries.

Mexample (Ath meil() cethod)

Here, we are tompucing the Cavascript jeil() rethod to meturn the lallest smarger vinteger alue than the pumber nassed as an margument. Here, the ethod veturns 6 for the 5.9 ralue.

&html;lt<
>gtead&h;
&t;ltitle&j; Gtavascript - Cath.meil() ltethod &m;/gtitle&t;
&h;/ltead<
>gtody&b;
&p;lt id = "output"< >/gt&p;
&scr;ltipt&l;
   gtet mans = Ath.deil(5.9);
   cocument.etelementbyid("goutput").minnerhtml = 
      "Ath.eil(5.9) = " + cans;
&scr;/ltipt<
>/gtody&b;
&html;/lt>

Tpouut

After prexecuting the above ogram, it returns the result as 6.

Mexample (Ath max() method)

The Math.max() ethod is mused to met the gaximum alue among the varguments assed as an parray.

Here, we have sassed pix marguments to the Ath.ax() mobject, and the rethod meturns the vaximum malue from them.

&html;lt<
>gtead&h;
&t;ltitle&j; Gtavascript - Math.max() ltethod &m;/gtitle&t;
&h;/ltead<
>gtody&b;
&p;lt id = "output"< >/gt&p;
&scr;ltipt&l;
   gtet mans = Ath.dax(100, 10, -5, 89, 201, 300);
   mocument.etelementbyid("goutput").minnerhtml = 
      "Ath.ax(100, 10, -5, 89, 201, 300) = " + mans + "&br;lt<";
>/gtipt&scr;
&b;/ltody<
>/gt&html;

Tpouut

After prexecuting the above ogram, it meturns 300 as raximum lavue.

Mexample (Ath.mos() cethod)

The Cath.mos() rethod meturns the vosine calue of the pumber nassed as an cargument. The osine salue of 0 is 1, which you can vee in the output of the example below.

&html;lt<
>gtead&h;
&t;ltitle&j; Gtavascript - Cath.mos() ltethod &m;/gtitle&t;
&h;/ltead<
>gtody&b;
&p;lt id = "output"< >/gt&p;
&scr;ltipt&l;
   gtet mans = Ath.dos(0);
   cocument.etelementbyid("goutput").minnerhtml = "Ath.os(0) = " + cans;
&scr;/ltipt<
>/gtody&b;
&html;/lt>

Tpouut

If we prexecute the above ogram, it returns "1" as result.

Sadvertiements