Math Object In TypeScript: Part 4

Math Object In TypeScript: Part 4 

 
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 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.
 

Sin() Method

 
In TypeScript, the sin() method is used to get the sine of a number. It returns a numeric value between -1 and 1. In this method, the numeric value represents the sine of the parameter.
 
Syntax
  1. sin(number)  
F
unction
  1. Sin(num: number)  
  2. {  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

Cos() Method

 
In TypeScript, the cos() method gets the cosine of a number. It returns a numeric value between -1 and 1. In this method, the numeric value represents the cosine of the angle.
 
Syntax
  1. cos(number)  
Function
  1. Cos(num: number)  
  2. {  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

Tan() Method

 
In TypeScript, this method is used to get the tangent of a number. In this method, the numeric value represents the tangent of the angle.
 
Syntax
  1. tan(number)  
Function
  1. Tan(num: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);  
  4.  document.body.appendChild(span);  
  5. }  

Complete Program

 
Sin_Cos_Tan.ts
  1. class Math_function {  
  2.  Sin(num: number) {  
  3.   var span = document.createElement("span");  
  4.   span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";  
  5.   document.body.appendChild(span);  
  6.  }  
  7.  Cos(num: number) {  
  8.   var span = document.createElement("span");  
  9.   span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";  
  10.   document.body.appendChild(span);  
  11.  }  
  12.  Tan(num: number) {  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var obj = new Math_function();  
  20.  var num = parseFloat(prompt("Enter a number for calculate Sin, Cos and Tan"));  
  21.  obj.Sin(num);  
  22.  obj.Cos(num);  
  23.  obj.Tan(num);  
  24. };  
Sin_Cos_Tan.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "Sin_Cos_Tan.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 > Sin, Cos and Tan Math Function in TypeScript < /h3>  
  23.  <  
  24.  div id = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  
Sin_Cos_Tan.js
  1. var Math_function = (function() {  
  2.  function Math_function() {}  
  3.  Math_function.prototype.Sin = function(num) {  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";  
  6.   document.body.appendChild(span);  
  7.  };  
  8.  Math_function.prototype.Cos = function(num) {  
  9.   var span = document.createElement("span");  
  10.   span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";  
  11.   document.body.appendChild(span);  
  12.  };  
  13.  Math_function.prototype.Tan = function(num) {  
  14.   var span = document.createElement("span");  
  15.   span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return Math_function;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new Math_function();  
  22.  var num = parseFloat(prompt("Enter a number for calculate Sin, Cos and Tan"));  
  23.  obj.Sin(num);  
  24.  obj.Cos(num);  
  25.  obj.Tan(num);  
  26. };  
Output 1
 
enter-value.gif
 
Output 2
 
result.gif