CoffeeScript is a lightweight language that compiles into JavaScript. As compared to JavaScript, it provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by languages such as JavaScript, YAML, Ruby, Python and has also influenced languages that are LiveScript, MoonScript.
Installation of CoffeeScript:
To install locally, use the below command:
npm install --save-dev coffeescript
What is math functions in CoffeeScript?
Math object of JavaScript provides different properties and methods to perform mathematical tasks on numbers. This Math object is static so it does not have a constructor. We can access all the methods and properties of the Math object without creating an object of it. As we know, the golden rule of CoffeeScript is: “It’s just JavaScript.” So we can use all the JavaScript math methods in CoffeeScript.
Syntax:
Math.method(number)
In the above syntax, Math is class along with method names and pass argument as integer number
Function 1: Math.abs(): The Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value.
Javascript
value = Math.abs(10); console.log "The absolute value of 10 is : " + value |
Output:
The absolute value of 10 is : 10
Function 2: Math.ceil( ): The Math.ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in Upward direction of rounding i.e towards the greater value.
Javascript
value = Math.ceil (90.15) console.log "The ceil value of 90.15 is : " + value |
Output:
The ceil value of 90.15 is : 91
Function 3: Math floor(): The Math.floor method is used to round off the number passed as a parameter to its nearest integer in Downward direction of rounding i.e. towards the lesser value.
Javascript
value = Math.floor (10.3) console.log "The floor value of 10.3 is : " + value |
Output:
The floor value of 10.3 is : 10
Function 4: Math log(): The Math.log() method used to return the natural logarithm (base e) of a number. The JavaScript Math.log() method is equivalent to ln(x) in mathematics. If the value of x is negative, then math.log() method return NaN.
Javascript
value = Math.log (10) console.log "The log value of 10 is : " + value |
Output:
The log value of 10 is : 2.302585092994046
Function 5: Math max(): The Math.max() method is used to return the largest of zero or more numbers. The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number.
Javascript
value = Math.max(50, 90, -1, 100); console.log "The max value among (50, 90, -1, 100) is : " + value |
Output:
The max value among (50, 90, -1, 100) is : 100
Function 6: Math min(): The Math.min() method is used to return the lowest-valued number passed in the method. The Math.min() method returns NaN if any parameter isn’t a number and can’t be converted into one. The min() is a static method of Math, therefore, it is always used as Math.min(), rather than as a method of a Math object created.
Javascript
value = Math.min (50, 90, -1, 100) console.log "The min value among (50, 90, -1, 100) is : " + value |
Output:
The min value among (50, 90, -1, 100) is : -1
Function 7: Math pow(): The Math.pow() method is used to power of a number i.e., the value of number raised to some exponent. Since the Math.pow() is a static method of Math and therefore it is always used as Math.pow() and not as a method of an object created of Math class.
Javascript
value = Math.pow (5,2) console.log "The value of pow(5,2) is : " + value |
Output:
The value of pow(5,2) is : 25
Function 8: Math round(): The Math.round() function in JavaScript is used to round the number passed as parameter to its nearest integer.
Javascript
value = Math.round (26.7) console.log "The nearest integer to 26.7 is : " + value |
Output:
The nearest integer to 26.7 is : 26