Skip to main content

math object in java script

Hi, ✋
this is another new tutorial which is explain Math object in java script. Using math object we can access lots of mathematical constants and functions.
So, let’s start math object.👇
Think you need to use the cos function. So you just have to type Math.cos(x).
It’s simple!
Here I have listed some Math properties.

      À     Math.PI                = this returns the value of π. (3.14)
      À     Math.E                 = this returns the Euler’s constant. (2.718)
      À     Math.LN2            = this returns the natural logarithm of 2. (0.693)
      À     Math.LOG2E       = this returns the base 2 logarithm of E. (1.442)
      À     Math.LOG10E     = this returns the base 10 logarithm of E. (0.434)
      À     Math.SQRT2       = this returns the square root of 2. (1.414)

So we can get the output as above values by using above functions.
So, I think it is not necessary to describe more about Math properties. Let’s move to Math methods. It is the most important topic.

Math methods
OK. Here is some Math methods I have listed below. Let’s identify them and learn them with a simple example.
      À     Math.abs()
      À     Math .sin()
      À     Math .cos()
      À     Math .exp(x)       = returns the exponential value of x
      À     Math.ceil(x)        = returns the smallest integer that is greater than or equal to x
      À     Math.floor(x)      = returns the largest integer that is less than or equal to x
      À     Math.max(x,y,z) = returns the largest number from x, y and z. If there are no numbers it gives       infinity
      À     Math.min(x,y,z)  = converse of Math.max(x,y,z)
      À     Math.power(x,y)=here x is base and y is exponent ,So it returns xy
      À     Math .random() = returns any random number between 0 and 1 (with 0)
      À     Math.round(x)    = returns the nearest integer
      À     Math .sqrt(y)       = returns the square root of y

<html>
<title> math functions </title>
<body>
<script type="text/javascript">
var number_1 = 2.89;
var number_2 =-2.89;
document.write("abs value of -2.89 = "+Math.abs(number_2)+"<br>");
document.write("abs value of 2.89  = "+Math.abs(number_1)+"<br>");
document.write("cos(30)  = "+Math.cos(30)+"<br>");
document.write("sin(30)  = "+Math.sin(30)+"<br>");
document.write("exponential value of 1 = "+Math.exp(1)+"<br>");
document.write("Math.floor(2.89) = "+Math.floor(number_1)+"<br>");
document.write("Math.ceil(2.89) = "+Math.ceil(number_1)+"<br>");
document.write("Math.max(12,445,67,908) = "+Math.max(12,445,67,908)+"<br>");
document.write("Math.min(23,-98,43,78,0) = "+Math.min(23,-98,43,78,0)+"<br>");
document.write("Math.random() = "+Math.random()+"<br>");
document.write("Math.round(2.89) = "+Math.round(2.89)+"<br>");
document.write("Math .sqrt(4) = "+Math .sqrt(4)+"<br>");
</script>
</body>
</html>


Here is the result set:


Figure 1

Ok. That’s all about math object and you can use these methods to perform any kind of mathematical functions. Try out with different kind of examples. Good luck!
Thankyou! 👋

Comments

Post a Comment