- CoffeeScript Tutorial
- CoffeeScript - Home
- CoffeeScript - Overview
- CoffeeScript - Environment
- CoffeeScript - command-line utility
- CoffeeScript - Syntax
- CoffeeScript - Data Types
- CoffeeScript - Variables
- CoffeeScript - Operators and Aliases
- CoffeeScript - Conditionals
- CoffeeScript - Loops
- CoffeeScript - Comprehensions
- CoffeeScript - Functions
- CoffeeScript Object Oriented
- CoffeeScript - Strings
- CoffeeScript - Arrays
- CoffeeScript - Objects
- CoffeeScript - Ranges
- CoffeeScript - Splat
- CoffeeScript - Date
- CoffeeScript - Math
- CoffeeScript - Exception Handling
- CoffeeScript - Regular Expressions
- CoffeeScript - Classes and Inheritance
- CoffeeScript Advanced
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite

- CoffeeScript Useful Resources
- CoffeeScript - Quick Guide
- CoffeeScript - Useful Resources
- CoffeeScript - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# CoffeeScript - Math

The **Math** object of JavaScript provides you properties and methods for mathematical constants and functions. Unlike other global objects, **Math** is not a constructor. All the properties and methods of **Math** are static and can be called by using Math as an object without creating it.

Thus, you refer to the constant **pi** as **Math.PI** and you call the sine function as **Math.sin(x)**, where x is the method's argument. We can use the JavaScript's Math object in our CoffeeScript code to perform math operations.

## Mathematical constants

If we want to use any common mathematical constants like pi or e we can use them using the JavaScript's **Math** object.

Following is the list of the Math constants provided by the Math object of JavaScript

S.No. | Property & Description |
---|---|

1 |
Euler's constant and the base of natural logarithms, approximately 2.718. |

2 |
Natural logarithm of 2, approximately 0.693. |

3 |
Natural logarithm of 10, approximately 2.302. |

4 |
Base 2 logarithm of E, approximately 1.442. |

5 |
Base 10 logarithm of E, approximately 0.434. |

6 |
Ratio of the circumference of a circle to its diameter, approximately 3.14159. |

7 |
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. |

8 | SQRT2
Square root of 2, approximately 1.414. |

### Example

The following example demonstrates the usage of the mathematical constants provided by JavaScript in CoffeeScript. Save this code in a file with name **math_example.coffee**

e_value = Math.E console.log "The value of the constant E is: " + e_value LN2_value = Math.LN2 console.log "The value of the constant LN2 is: " + LN2_value LN10_value = Math.LN10 console.log "The value of the constant LN10 is: " + LN10_value LOG2E_value = Math.LOG2E console.log "The value of the constant LOG2E is: " + LOG2E_value LOG10E_value = Math.LOG10E console.log "The value of the constant LOG10E is: " + LOG10E_value PI_value = Math.PI console.log "The value of the constant PI is: " + PI_value SQRT1_2_value = Math.SQRT1_2 console.log "The value of the constant SQRT1_2 is: " + SQRT1_2_value SQRT2_value = Math.SQRT2 console.log "The value of the constant SQRT2 is: " + SQRT2_value

Open the **command prompt** and compile the .coffee file as shown below.

c:\> coffee -c math_example.coffee

On compiling, it gives you the following JavaScript.

// Generated by CoffeeScript 1.10.0 (function() { var LN10_value, LN2_value, LOG10E_value, LOG2E_value, PI_value, SQRT1_2_value, SQRT2_value, e_value; e_value = Math.E; console.log("The value of the constant E is: " + e_value); LN2_value = Math.LN2; console.log("The value of the constant LN2 is: " + LN2_value); LN10_value = Math.LN10; console.log("The value of the constant LN10 is: " + LN10_value); LOG2E_value = Math.LOG2E; console.log("The value of the constant LOG2E is: " + LOG2E_value); LOG10E_value = Math.LOG10E; console.log("The value of the constant LOG10E is: " + LOG10E_value); PI_value = Math.PI; console.log("The value of the constant PI is: " + PI_value); SQRT1_2_value = Math.SQRT1_2; console.log("The value of the constant SQRT1_2 is: " + SQRT1_2_value); SQRT2_value = Math.SQRT2; console.log("The value of the constant SQRT2 is: " + SQRT2_value); }).call(this);

Now, open the **command prompt** again and run the CoffeeScript file as shown below.

c:\> coffee math_example.coffee

On executing, the CoffeeScript file produces the following output.

The value of the constant E is: 2.718281828459045 The value of the constant LN2 is: 0.6931471805599453 The value of the constant LN10 is: 2.302585092994046 The value of the constant LOG2E is: 1.4426950408889634 The value of the constant LOG10E is: 0.4342944819032518 The value of the constant PI is: 3.141592653589793 The value of the constant SQRT1_2 is: 0.7071067811865476 The value of the constant SQRT2 is: 1.4142135623730951

## Math Methods

In addition to properties, the Math object also provides methods. Following is the list of methods of the **Math** object of JavaScript. Click on the name of these methods to get an example demonstrating their usage in CoffeeScript.

S.No. | Method & Description |
---|---|

1 | abs()
Returns the absolute value of a number. |

2 | acos()
Returns the arccosine (in radians) of a number. |

3 | asin()
Returns the arcsine (in radians) of a number. |

4 | atan()
Returns the arctangent (in radians) of a number. |

5 | atan2()
Returns the arctangent of the quotient of its arguments. |

6 | ceil()
Returns the smallest integer greater than or equal to a number. |

7 | cos()
Returns the cosine of a number. |

8 | exp()
Returns E |

9 | floor()
Returns the largest integer less than or equal to a number. |

10 | log()
Returns the natural logarithm (base E) of a number. |

11 | max()
Returns the largest of zero or more numbers. |

12 | min()
Returns the smallest of zero or more numbers. |

13 | pow()
Returns base to the exponent power, that is, base exponent. |

14 | random()
Returns a pseudo-random number between 0 and 1. |

15 | round()
Returns the value of a number rounded to the nearest integer. |

16 | sin()
Returns the sine of a number. |

17 | sqrt()
Returns the square root of a number. |

18 | tan()
Returns the tangent of a number. |