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
- Sin(num: number)
- {
- var span = document.createElement("span");
- span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";
- document.body.appendChild(span);
- }
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
Function
- Cos(num: number)
- {
- var span = document.createElement("span");
- span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";
- document.body.appendChild(span);
- }
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
Function
- Tan(num: number) {
- var span = document.createElement("span");
- span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);
- document.body.appendChild(span);
- }
Complete Program
Sin_Cos_Tan.ts
- class Math_function {
- Sin(num: number) {
- var span = document.createElement("span");
- span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";
- document.body.appendChild(span);
- }
- Cos(num: number) {
- var span = document.createElement("span");
- span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";
- document.body.appendChild(span);
- }
- Tan(num: number) {
- var span = document.createElement("span");
- span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);
- document.body.appendChild(span);
- }
- }
- window.onload = () => {
- var obj = new Math_function();
- var num = parseFloat(prompt("Enter a number for calculate Sin, Cos and Tan"));
- obj.Sin(num);
- obj.Cos(num);
- obj.Tan(num);
- };
Sin_Cos_Tan.htm
- < !DOCTYPE html >
- <
- html lang = "en"
- xmlns = "http://www.w3.org/1999/xhtml" >
- <
- head >
- <
- meta charset = "utf-8" / >
- <
- title > TypeScript HTML App < /title>
- <
- link rel = "stylesheet"
- href = "app.css"
- type = "text/css" / >
- <
- script src = "Sin_Cos_Tan.js" > < /script>
- <
- /head>
- <
- body >
- <
- h3 > Sin, Cos and Tan Math Function in TypeScript < /h3>
- <
- div id = "content" / >
- <
- /body>
- <
- /html>
Sin_Cos_Tan.js
- var Math_function = (function() {
- function Math_function() {}
- Math_function.prototype.Sin = function(num) {
- var span = document.createElement("span");
- span.innerText = "The sin value of " + num + " is -> " + Math.sin(num) + "\n";
- document.body.appendChild(span);
- };
- Math_function.prototype.Cos = function(num) {
- var span = document.createElement("span");
- span.innerText = "The cos of " + num + " is -> " + Math.cos(num) + "\n";
- document.body.appendChild(span);
- };
- Math_function.prototype.Tan = function(num) {
- var span = document.createElement("span");
- span.innerText = "The tan value of " + num + " is -> " + Math.tan(num);
- document.body.appendChild(span);
- };
- return Math_function;
- })();
- window.onload = function() {
- var obj = new Math_function();
- var num = parseFloat(prompt("Enter a number for calculate Sin, Cos and Tan"));
- obj.Sin(num);
- obj.Cos(num);
- obj.Tan(num);
- };
Output 1
Output 2