You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You need to use common mathematical constants like pi or e.
10
+
11
+
h2. Solution
12
+
13
+
Use Javascript's Math object to provide commonly needed mathematical constants.
14
+
15
+
{% highlight coffeescript %}
16
+
Math.PI
17
+
# => 3.141592653589793
18
+
19
+
# Note: Capitalization matters! This produces no output, it's undefined.
20
+
Math.Pi
21
+
# =>
22
+
23
+
Math.E
24
+
# => 2.718281828459045
25
+
26
+
Math.SQRT2
27
+
# => 1.4142135623730951
28
+
29
+
Math.SQRT1_2
30
+
# => 0.7071067811865476
31
+
32
+
# Natural log of 2. ln(2)
33
+
Math.LN2
34
+
# => 0.6931471805599453
35
+
36
+
Math.LN10
37
+
# => 2.302585092994046
38
+
39
+
Math.LOG2E
40
+
# => 1.4426950408889634
41
+
42
+
Math.LOG10E
43
+
# => 0.4342944819032518
44
+
45
+
{% endhighlight %}
46
+
47
+
h2. Discussion
48
+
49
+
For another example of how a math constant is used in a real world problem, refer to the <a href="http://coffeescriptcookbook.com/chapters/math/radians-degrees">'Converting Radians and Degrees'</a> section of this Math chapter.
0 commit comments