Explain various math functions that can be used in CoffeeScript


CoffeeScript was introduced in 2009, which compiles JavaScript. The simple difference between CoffeeScript and JavaScript is that of syntax. The syntax of CoffeeScript is very simple.

As CoffeeScript compiles JavaScript, we can use every method of JavaScript in CoffeeScript. So, we will explain every math function of a Math object we can use in CoffeeScript.

In JavaScript, Math is a static object, so we can use it directly without taking the reference of any element. We can call the methods of the Math object by taking the ‘Math’ keyword as a reference.

Math functions in CoffeeScript

Math.abs()

The abs() method of the Math object takes the number value as a parameter and returns the absolute value of the number.

Example

We have taken the different numbers in this example and used the Math.abs() method to get their absolute value.

absolute_value = Math.abs(-30)
console.log "The absolute value of -30 is " + absolute_value
absolute_value = Math.abs(30)
console.log "The absolute value of -30 is " + absolute_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var absolute_value;

   absolute_value = Math.abs(-30);

   console.log("The absolute value of -30 is " + absolute_value);

   absolute_value = Math.abs(30);

   console.log("The absolute value of -30 is " + absolute_value);

}).call(this);

Output

On executing, it produces the following output −

The absolute value of -30 is 30
The absolute value of -30 is 30

Math.sin()

We can use the sin() method of Math object to get the sine value of any real numbers.

Example

In the example below, we have used the sin() method to get the sine value of the different real numbers. Users can observe that the output returns values between -1 and 1.

sine_value = Math.sin(0)
console.log "The sine value of 0 is " + sine_value

sine_value = Math.sin(3434343232340)
console.log "The sine value of 3434343232340 is " + sine_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var sine_value;

   sine_value = Math.sin(0);

   console.log("The sine value of 0 is " + sine_value);

   sine_value = Math.sin(3434343232340);

   console.log("The sine value of 3434343232340 is " + sine_value);

}).call(this);

Output

On executing, it produces the following output.

The sine value of 0 is 0
The sine value of 3434343232340 is 0.6305294679473046

Math.cos()

The cos() method of the Math object is used to get the cosine value of any real number, and it returns the output between -1 and 1.

Example

In the example below, we have used the cos method by passing different values as a parameter.

cosine_value = Math.cos(-4545)
console.log "The cosine value of -4545 is " + cosine_value

cosine_value = Math.cos(1)
console.log "The cosine value of 1 is " + cosine_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var cosine_value;

   cosine_value = Math.cos(-4545);

   console.log("The cosine value of -4545 is " + cosine_value);

   cosine_value = Math.cos(1);

   console.log("The cosine value of 1 is " + cosine_value);

}).call(this);

Output

On executing, it produces the following output.

The cosine value of -4545 is -0.6336224240941999
The cosine value of 1 is 0.5403023058681398

Math.floor()

The floor() method of the Math object round down the number we pass as a parameter. It returns the greatest less than or equal to the number value passed as a parameter.

Example

We have taken the different numbers and used the floor method to round down them. Users can observe in the output that 0.999 is nearest to 1. Still, it is downgraded by the floor() method.

floor_value = Math.floor(544.4343)
console.log "The floor value of 5444.4343 is " + floor_value

floor_value = Math.floor(0.9999)
console.log "The floor value of 0.9999 is " + floor_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var floor_value;

   floor_value = Math.floor(544.4343);

   console.log("The floor value of 5444.4343 is " + floor_value);

   floor_value = Math.floor(0.9999);

   console.log("The floor value of 0.9999 is " + floor_value);

}).call(this);

Output

On executing, it produces the following output.

The floor value of 5444.4343 is 544
The floor value of 0.9999 is 0

Math.ceil()

The ceil() method of the Math object is used to round up the number. It returns the smallest integer greater than or equal to the number passed as a parameter.

Example

In the output of the example below, we can see that even if 0.001 is nearest to 0. Still, ceil() method returns one as it always rounds up the numbers.

ceil_value = Math.ceil(0.001)
console.log "The ceil value of 0.001 is " + ceil_value

ceil_value = Math.ceil(-10)
console.log "The ceil value of -10 is " + ceil_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var ceil_value;

   ceil_value = Math.ceil(0.001);

   console.log("The ceil value of 0.001 is " + ceil_value);

   ceil_value = Math.ceil(-10);

   console.log("The ceil value of -10 is " + ceil_value);

}).call(this);

Output

On executing, it produces the following output.

The ceil value of 0.001 is 1
The ceil value of -10 is -10

Math.log()

The log() method of the Math object is used to find the natural logarithm of any positive numeric value. It calculates the logarithm of the number by taking the base E, where E is Euler’s constant.

Example

We have found the natural logarithm of different positive values using the log() method.

log_value = Math.log(10)
console.log "The log value of 10 is " + log_value

log_value = Math.log(4545)
console.log "The log value of 4545 is " + log_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
  var log_value;

  log_value = Math.log(10);

  console.log("The log value of 10 is " + log_value);

  log_value = Math.log(4545);

  console.log("The log value of 4545 is " + log_value);

}).call(this);

Output

On executing, it produces the following output.

The log value of 10 is 2.302585092994046
The log value of 4545 is 8.421783006611578

Math.random()

We can use the random() method of the Math object to find the random number in CoffeeScript. It returns the random number between 0 and 1.

Example

In this example, we have invoked the random() method multiple times, and users can see the different values in the output.

random_value = Math.random()
console.log "The random value is " + random_value

random_value = Math.random()
console.log "The random value is " + random_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var random_value;

   random_value = Math.random();

   console.log("The random value is " + random_value);

   random_value = Math.random();

   console.log("The random value is " + random_value);

}).call(this);

Output

On executing, it produces the following output.

The random value is 0.3468464659210826
The random value is 0.4870361090153488

Math.max()

Users can use the max() method of the Math object to find the maximum value from the multiple number values.

Example

In this example, we have passed the multiple values as a parameter of the max() method to find the maximum among them.

max_value = Math.max(20, 345, 23, 23, 12, 12)
console.log "The max value among 20, 345, 23, 23, 12, 12 is " + max_value

max_value = Math.max(43, 43, 43)
console.log "The max value among 43, 43, 43 is " + max_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var max_value;

   max_value = Math.max(20, 345, 23, 23, 12, 12);

   console.log("The max value among 20, 345, 23, 23, 12, 12 is " + max_value);

   max_value = Math.max(43, 43, 43);

   console.log("The max value among 43, 43, 43 is " + max_value);

}).call(this);

Output

On executing, it produces the following output.

The max value among 20, 345, 23, 23, 12, 12 is 345
The max value among 43, 43, 43 is 43

Math.min()

We can use the min() method of the Math object to find the minimum value from the two or more numeric values. It returns a single value, the minimum among all values passed as a parameter.

Example

We have taken different values in this example and used the min() method of the Math object to find the minimum among them.

min_value = Math.min(20, 345, 23, 23, 12, 12)
console.log "The min value among 20, 345, 23, 23, 12, 12 is " + min_value

min_value = Math.min(43, 43, 43)
console.log "The min value among 43, 43, 43 is " + min_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var min_value;

   min_value = Math.min(20, 345, 23, 23, 12, 12);

   console.log("The min value among 20, 345, 23, 23, 12, 12 is " + min_value);

   min_value = Math.min(43, 43, 43);

   console.log("The min value among 43, 43, 43 is " + min_value);

}).call(this);

Output

On executing, it produces the following output.

The min value among 20, 345, 23, 23, 12, 12 is 12
The min value among 43, 43, 43 is 43

Math.sqrt()

The sqrt() method of Math object takes a single value as a parameter and returns the square root of the number.

Example

In this example, we have used the sqrt() method to find the square root of the different real numbers.

sqrt_value = Math.sqrt(12)
console.log "The sqrt of the 12 is " + sqrt_value

sqrt_value = Math.sqrt(1.32)
console.log "The sqrt of 1.32 is " + sqrt_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var sqrt_value;

   sqrt_value = Math.sqrt(12);

   console.log("The sqrt of the 12 is " + sqrt_value);

   sqrt_value = Math.sqrt(1.32);

   console.log("The sqrt of 1.32 is " + sqrt_value);

}).call(this);

Output

On executing, it produces the following output.

The sqrt of the 12 is 3.4641016151377544
The sqrt of 1.32 is 1.1489125293076057

Math.exp()

We can use the exp() method to find the value of the Ex, where E is the Euler’s constant and x is the value we pass as a parameter.

Example

In this example, we have used the exp() method and passed the different real numbers as a parameter of the exp() method to get the value of the Ex.

exp_value = Math.exp(19)
console.log "The exp value of the 19 is " + exp_value

exp_value = Math.exp(2.21)
console.log "The exp of value 2.21 is " + exp_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var exp_value;

   exp_value = Math.exp(19);

   console.log("The exp value of the 19 is " + exp_value);

   exp_value = Math.exp(2.21);

   console.log("The exp of value 2.21 is " + exp_value);

}).call(this);

Output

On executing, it produces the following output.

The exp value of the 19 is 178482300.96318728
The exp of value 2.21 is 9.115716393040305

Math.round()

We can use the round() method of the math object to round the numbers to their nearest integer value.

Example

In the example, users can observe how 1.49 is rounded to 1 and 1.51 is rounded to 2.

round_value = Math.round(1.49)
console.log "The round value of the 1.49 is " + round_value

round_value = Math.round(1.51)
console.log "The round of value 1.51 is " + round_value

On compiling, the above CoffeeScript produces the following JavaScript −

// Generated by CoffeeScript 2.4.1
(function() {
   var round_value;

   round_value = Math.round(1.49);

   console.log("The round value of the 1.49 is " + round_value);

   round_value = Math.round(1.51);

   console.log("The round of value 1.51 is " + round_value);

}).call(this);

Output

On executing, it produces the following output.

The round value of the 1.49 is 1
The round of value 1.51 is 2

We have covered the different functions of the Math object in this tutorial. Also, we can see that as we execute the Math methods in JavaScript, we have executed them here. Also, there are some other methods that the Math object contains in CoffeeScript, and users can also explore that.

Updated on: 05-Apr-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements