Math Object In TypeScript: Part 1

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.
 

Asin() function

 
In TypeScript the asin() method returns a numeric value between -pi/2 and pi/2 radians for x between -1 and 1. If the value of a number is outside this range then it returns NaN.
 
Syntax
  1. asin (number)  
function
  1. Asin(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The arcsine of " + num + " is -> " + Math.asin(num) + "\n";  
  4.  document.body.appendChild(span);  
  5. }  

Acos() function

 
In TypeScript the acos method returns a numeric value between 0 and pi radians for x between -1 and 1. If the value of a number is outside this range then it returns NaN.
 
Syntax
  1. acos (number)  
function
  1. Acos(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The arccosine of " + num + " is -> " + Math.acos(num) + "\n";  
  4.  document.body.appendChild(span);  
  5. }  

Atan() function

 
In TypeScript the atan() method returns a numeric value between -pi/2 and pi/2 radians.
 
Syntax
  1. acos(number)  
function
  1. Atan(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The arctangent of " + num + " is -> " + Math.atan(num);  
  4.  document.body.appendChild(span);  
  5. }  

Complete Program

 
Function.ts
  1. class Greeter {  
  2.  Asin(num: number) {  
  3.   var span = document.createElement("span");  
  4.   span.innerText = "The arcsine of " + num + " is -> " + Math.asin(num) + "\n";  
  5.   document.body.appendChild(span);  
  6.  }  
  7.  Acos(num: number) {  
  8.   var span = document.createElement("span");  
  9.   span.innerText = "The arccosine of " + num + " is -> " + Math.acos(num) + "\n";  
  10.   document.body.appendChild(span);  
  11.  }  
  12.  Atan(num: number) {  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "The arctangent of " + num + " is -> " + Math.atan(num);  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var obj = new Greeter();  
  20.  var num = parseFloat(prompt("Enter a number for calculate Asin,Acos and Atan"));  
  21.  obj.Asin(num);  
  22.  obj.Acos(num);  
  23.  obj.Atan(num);  
  24. };  
default.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="app.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h2>Asin,Acos and Atan Math Function in TypeScript</h2>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
app.js
  1. var Greeter = (function() {  
  2.  function Greeter() {}  
  3.  Greeter.prototype.Asin = function(num) {  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "The arcsine of " + num + " is -> " + Math.asin(num) + "\n";  
  6.   document.body.appendChild(span);  
  7.  };  
  8.  Greeter.prototype.Acos = function(num) {  
  9.   var span = document.createElement("span");  
  10.   span.innerText = "The arccosine of " + num + " is -> " + Math.acos(num) + "\n";  
  11.   document.body.appendChild(span);  
  12.  };  
  13.  Greeter.prototype.Atan = function(num) {  
  14.   var span = document.createElement("span");  
  15.   span.innerText = "The arctangent of " + num + " is -> " + Math.atan(num);  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return Greeter;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new Greeter();  
  22.  var num = parseFloat(prompt("Enter a number for calculate Asin,Acos and Atan"));  
  23.  obj.Asin(num);  
  24.  obj.Acos(num);  
  25.  obj.Atan(num);  
  26. };  
Output 1
 
enter-num.gif
 
Output 2
 
result.gif


Similar Articles