Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript mathematical constant


May 09, 2021 CoffeeScript


Table of contents


Mathematical constants

Problem

You need to use common mathematical constants, such π or e.

Solution

Use Javascript's Math object to provide the mathematical constants that are usually required.

Math.PI
# => 3.141592653589793

# Note: Capitalization matters! This produces no output, it's undefined.
Math.Pi
# =>

Math.E
# => 2.718281828459045

Math.SQRT2
# => 1.4142135623730951

Math.SQRT1_2
# => 0.7071067811865476

# Natural log of 2. ln(2)
Math.LN2
# => 0.6931471805599453

Math.LN10
# => 2.302585092994046

Math.LOG2E
# => 1.4426950408889634

Math.LOG10E
# => 0.4342944819032518

Discuss

Another example is the question of the use of a mathematical constant in the real world, the section of the mathematical chapter on http://coffeescript-cookbook.github.io/chapters/math/radians-degrees angles).