Math Object in TypeScript: Part 3

In this article, you will learn about the math object's methods in TypeScript.

Math Object in TypeScript: Part 3 

 
Before reading this article, please go through the following articles:
  1. asin(), acos() and atan() method in typescript
  2. atan2(), exp() and ceil() method in typescript

Introduction

 
In TypeScript, the math object is a built-in object that provides many specialized mathematical methods and numerical values. The math object is used to handle 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 some of the TypeScript math functions.
 

Floor() Method

 
In TypeScript, the floor() method of the Math object is the opposite of the ceil method. It used to round a number downwards to the nearest integer. It returns the largest integer less than or equal to a number. If the argument value is an integer then the value will not be rounded.
 
Syntax
  1. floor (number)  
Function
  1. Floor(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";  
  4.  document.body.appendChild(span);  
  5. }  

Log() Method

 
In TypeScript the log() method returns the logarithm of a number. By default the number base is E. If the specified number is negative then Nan is returned and if the number is 0 then -Infinity is returned.
 
Syntax
  1. log (number)  
Function
  1. Log(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n";  
  4.  document.body.appendChild(span);  
  5. }  

Round() Method

 
In TypeScript, this method returns the value rounded to the nearest integer.
 
For example, 4.45 will be rounded down and 4.5 will be rounded up.
 
Syntax
  1. round(number) 
Function
  1. Round(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The Round value of " + num + " is -> " + Math.round(num);  
  4.  document.body.appendChild(span);  
  5. }  

Complete Program

 
Floor_Log_Round.ts
  1. class Math_Method  
  2. {  
  3.  Floor(num: number)  
  4.  {  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9.  Log(num: number)  
  10.  {  
  11.   var span = document.createElement("span");  
  12.   span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n"// By default base E of number  
  13.   document.body.appendChild(span);  
  14.  }  
  15.  Round(num: number)  
  16.  {  
  17.   var span = document.createElement("span");  
  18.   span.innerText = "The Round value of " + num + " is -> " + Math.round(num);  
  19.   document.body.appendChild(span);  
  20.  }  
  21. }  
  22. window.onload = () =>  
  23.  {  
  24.   var obj = new Math_Method();  
  25.   var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));  
  26.   obj.Floor(num);  
  27.   obj.Log(num);  
  28.   obj.Round(num);  
  29.  };  
Floor_Log_Round_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="Floor_Log_Round.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Floor, Log and Round Math Function in TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
Floor_Log_Round.js
  1. var Math_Method = (function() {  
  2.  function Math_Method() {}  
  3.  Math_Method.prototype.Floor = function(num) {  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";  
  6.   document.body.appendChild(span);  
  7.  };  
  8.  Math_Method.prototype.Log = function(num) {  
  9.   var span = document.createElement("span");  
  10.   span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n";  
  11.   document.body.appendChild(span);  
  12.  };  
  13.  Math_Method.prototype.Round = function(num) {  
  14.   var span = document.createElement("span");  
  15.   span.innerText = "The Round value of " + num + " is -> " + Math.round(num);  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return Math_Method;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new Math_Method();  
  22.  var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));  
  23.  obj.Floor(num);  
  24.  obj.Log(num);  
  25.  obj.Round(num);  
  26. };  
Output 1
 
enter-num.gif
 
Output 2
 
result.gif