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

floor (number)


F
unction

Floor(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Floor value of " +num+ " is -> "+ Math.floor(num)+"\n";

        document.body.appendChild(span); 

    }

 

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

log (number)


F
unction

Log(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Logarithm of " +num+ " is -> "+ Math.log(num)+"\n"; // By default base E of number

        document.body.appendChild(span); 

    }

 

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

round(number)

Function

Round(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Round value of " +num+ " is -> "+ Math.round(num);

        document.body.appendChild(span); 

    }

 

Complete Program

Floor_Log_Round.ts

class Math_Method

{

    Floor(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Floor value of " +num+ " is -> "+ Math.floor(num)+"\n";

        document.body.appendChild(span); 

    }

    Log(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Logarithm of " +num+ " is -> "+ Math.log(num)+"\n"; // By default base E of number

        document.body.appendChild(span); 

    }

    Round(num:number)

    {

        var span = document.createElement("span");

        span.innerText ="The Round value of " +num+ " is -> "+ Math.round(num);

        document.body.appendChild(span); 

    }

}

window.onload = () =>

{

    var obj = new Math_Method();

    var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));

    obj.Floor(num);

    obj.Log(num);

    obj.Round(num);   

};

 

Floor_Log_Round_Demo.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="Floor_Log_Round.js"></script>

</head>

<body>

    <h3>Floor, Log and Round Math Function in TypeScript</h3>

    <div id="content"/>

</body>

</html>

 

Floor_Log_Round.js

var Math_Method = (function () {

    function Math_Method() { }

    Math_Method.prototype.Floor = function (num) {

        var span = document.createElement("span");

        span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";

        document.body.appendChild(span);

    };

    Math_Method.prototype.Log = function (num) {

        var span = document.createElement("span");

        span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n";

        document.body.appendChild(span);

    };

    Math_Method.prototype.Round = function (num) {

        var span = document.createElement("span");

        span.innerText = "The Round value of " + num + " is -> " + Math.round(num);

        document.body.appendChild(span);

    };

    return Math_Method;

})();

window.onload = function () {

    var obj = new Math_Method();

    var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));

    obj.Floor(num);

    obj.Log(num);

    obj.Round(num);

};

//@ sourceMappingURL=Floor_Log_Round.js.map

 

Output 1


enter-num.gif


Output 2

result.gif

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all