- CoffeeScript - SQLite
- CoffeeScript - MongoDB
- CoffeeScript - jQuery
- CoffeeScript - Ajax
- CoffeeScript - Classes and Inheritance
- CoffeeScript - Regular Expressions
- CoffeeScript - Exception Handling
- CoffeeScript - Math
- CoffeeScript - Date
- CoffeeScript - Splat
- CoffeeScript - Ranges
- CoffeeScript - Objects
- CoffeeScript - Arrays
- CoffeeScript - Strings
- CoffeeScript - Functions
- CoffeeScript - Comprehensions
- CoffeeScript - Loops
- CoffeeScript - Conditionals
- CoffeeScript - Operators and Aliases
- CoffeeScript - Variables
- CoffeeScript - Data Types
- CoffeeScript - Syntax
- CoffeeScript - command-line utility
- CoffeeScript - Environment
- CoffeeScript - Overview
- CoffeeScript - Home
CoffeeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CoffeeScript - Math
The Math object of JavaScript provides you properties and methods for mathematical constants and functions. Unpke 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 pke pi or e we can use them using the JavaScript s Math object.
Following is the pst of the Math constants provided by the Math object of JavaScript
S.No. | Property & Description |
---|---|
1 | E Euler s constant and the base of natural logarithms, approximately 2.718. |
2 | LN2 Natural logarithm of 2, approximately 0.693. |
3 | LN10 Natural logarithm of 10, approximately 2.302. |
4 | LOG2E Base 2 logarithm of E, approximately 1.442. |
5 | LOG10E Base 10 logarithm of E, approximately 0.434. |
6 | PI Ratio of the circumference of a circle to its diameter, approximately 3.14159. |
7 | SQRT1_2 Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. |
8 | 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 compipng, 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 pst of methods of the Math object of JavaScript. Cpck on the name of these methods to get an example demonstrating their usage in CoffeeScript.
S.No. | Method & Description |
---|---|
1 | Returns the absolute value of a number. |
2 | Returns the arccosine (in radians) of a number. |
3 | Returns the arcsine (in radians) of a number. |
4 | Returns the arctangent (in radians) of a number. |
5 | Returns the arctangent of the quotient of its arguments. |
6 | Returns the smallest integer greater than or equal to a number. |
7 | Returns the cosine of a number. |
8 | Returns EN, where N is the argument, and E is Euler s constant, the base of the natural logarithm. |
9 | Returns the largest integer less than or equal to a number. |
10 | Returns the natural logarithm (base E) of a number. |
11 | Returns the largest of zero or more numbers. |
12 | Returns the smallest of zero or more numbers. |
13 | Returns base to the exponent power, that is, base exponent. |
14 | Returns a pseudo-random number between 0 and 1. |
15 | Returns the value of a number rounded to the nearest integer. |
16 | Returns the sine of a number. |
17 | Returns the square root of a number. |
18 | Returns the tangent of a number. |