Math Object In TypeScript: Part 5

Math Object In TypeScript: Part 5 

 
Before reading this article, please go through the following articles:

Introduction

 
In TypeScript, the math object is a built-in object that provides many specialized mathematical methods and numerical values. The math object handles values within the range of integer and float types. The math object also provides many trigonometric and logarithmic methods. If you have the appropriate mathematical background, you should be able to use these methods with no problem.
 
In this article, I am describing the TypeScript math functions Pow, Sqrt, and Random.
 

Pow() Method

 
In TypeScript the pow() method is used to return the base to the exponent power. In this method we need to pass two parameters, the first is the base to which the power will be calculated and the other is the exponent value. The power parameter of the pow method can be a fractional value.
 
In this example, we declare a variable v and it's value 2. We use this variable as an exponent.  
 
Syntax
  1. pow(x,y)   
where x is the number and y is the power parameter.
 
F
unction
  1. Pow(num: number) {  
  2.  var v = 2;  
  3.  var span = document.createElement("span");  
  4.  span.style.color = "green";  
  5.  span.innerText = +v + " Power of random number is -> " + Math.pow(num, 2) + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

Sqrt() Method

 
In TypeScript the sqrt() method determines the square root of a number. If the value of the number is negative then the sqrt() method returns none.
 
Syntax
  1. sqrt(number)  
Function
  1. Sqrt(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.style.color = "blue";  
  4.  span.innerText = "Square Root of random number is -> " + Math.sqrt(num) + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

Random() Method

 
In TypeScript the random() method generates a random number that is equal to or greater than zero but less than one. Random numbers can be used for testing and are often used in games or applications that provide animation.
 
Syntax
  1. random()  
Function
  1. var random = Math.floor(Math.random() * 10);  

Complete Program

 
Pow_Sqrt_Random.ts
  1. class Pow_Sqrt_Random_Function {  
  2.  Pow(num: number) {  
  3.   var v = 2;  
  4.   var span = document.createElement("span");  
  5.   span.style.color = "green";  
  6.   span.innerText = +v + " Power of random number is -> " + Math.pow(num, 2) + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9.  Sqrt(num: number) {  
  10.   var span = document.createElement("span");  
  11.   span.style.color = "blue";  
  12.   span.innerText = "Square Root of random number is -> " + Math.sqrt(num) + "\n";  
  13.   document.body.appendChild(span);  
  14.  }  
  15. }  
  16. window.onload = () => {  
  17.  var obj = new Pow_Sqrt_Random_Function();  
  18.  var random = Math.floor(Math.random() * 10);  
  19.  var span = document.createElement("span");  
  20.  span.innerText = "Random number between 1-10 is -> " + random + "\n";  
  21.  document.body.appendChild(span);  
  22.  obj.Pow(random);  
  23.  obj.Sqrt(random);  
  24. };  
Pow_Sqrt_Random_Demo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="Pow_Sqrt_Random.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h2>Pow, Sqrt and Random Math Function in TypeScript</h2>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
Pow_Sqrt_Random.js
  1. var Pow_Sqrt_Random_Function = (function() {  
  2.  function Pow_Sqrt_Random_Function() {}  
  3.  Pow_Sqrt_Random_Function.prototype.Pow = function(num) {  
  4.   var v = 2;  
  5.   var span = document.createElement("span");  
  6.   span.style.color = "green";  
  7.   span.innerText = +v + " Power of random number is -> " + Math.pow(num, 2) + "\n";  
  8.   document.body.appendChild(span);  
  9.  };  
  10.  Pow_Sqrt_Random_Function.prototype.Sqrt = function(num) {  
  11.   var span = document.createElement("span");  
  12.   span.style.color = "blue";  
  13.   span.innerText = "Square Root of random number is -> " + Math.sqrt(num) + "\n";  
  14.   document.body.appendChild(span);  
  15.  };  
  16.  return Pow_Sqrt_Random_Function;  
  17. })();  
  18. window.onload = function() {  
  19.  var obj = new Pow_Sqrt_Random_Function();  
  20.  var random = Math.floor(Math.random() * 10);  
  21.  var span = document.createElement("span");  
  22.  span.innerText = "Random number between 1-10 is -> " + random + "\n";  
  23.  document.body.appendChild(span);  
  24.  obj.Pow(random);  
  25.  obj.Sqrt(random);  
  26. };  
Output 
 
 Final-Output.gif