math-extras
Version:
Useful mathematical functions which should be part of the JavaScript Math object.
238 lines (162 loc) • 8.31 kB
HTML
<html>
<head>
<title>math-extras.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="index.html">
index.coffee
</a>
<a class="source" href="math-extras.html">
math-extras.coffee
</a>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>math-extras.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<h4 id="useful-mathematical-functions-which-should-be-part-of-the-javascript-math-object-">Useful mathematical functions which should be part of the JavaScript Math object.</h4>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Compute conversion from degrees to radians.</p>
</div>
<div class="content"><div class='highlight'><pre>Math.radians = <span class="hljs-function"><span class="hljs-params">(degrees)</span> -></span>
degrees * Math.PI / <span class="hljs-number">180</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Compute conversion from radians to degrees.</p>
</div>
<div class="content"><div class='highlight'><pre>Math.degrees = <span class="hljs-function"><span class="hljs-params">(radians)</span> -></span>
radians * <span class="hljs-number">180</span> / Math.PI</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Obtain the sign of the given number.</p>
</div>
<div class="content"><div class='highlight'><pre>Math.sign = <span class="hljs-function"><span class="hljs-params">(num)</span> -></span>
<span class="hljs-keyword">if</span> num
<span class="hljs-keyword">if</span> num < <span class="hljs-number">0</span>
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Compute conversion from degrees to radians.</p>
</div>
<div class="content"><div class='highlight'><pre>Math.factorial = <span class="hljs-function"><span class="hljs-params">(x)</span> -></span>
<span class="hljs-keyword">if</span> x < <span class="hljs-number">2</span>
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> Math.factorial(x-<span class="hljs-number">1</span>)*x</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Compute the base-10 logarithm of x.</p>
</div>
<div class="content"><div class='highlight'><pre>Math.log10 = <span class="hljs-function"><span class="hljs-params">(x)</span> -></span>
Math.log(x) / Math.LN10</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Compute the error function of x.</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Error_function">http://en.wikipedia.org/wiki/Error_function</a></li>
<li><a href="https://github.com/ghewgill/picomath">https://github.com/ghewgill/picomath</a></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre>Math.erf = <span class="hljs-function"><span class="hljs-params">(x)</span> -></span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Constants</p>
</div>
<div class="content"><div class='highlight'><pre> a1 = <span class="hljs-number">0.254829592</span>
a2 = -<span class="hljs-number">0.284496736</span>
a3 = <span class="hljs-number">1.421413741</span>
a4 = -<span class="hljs-number">1.453152027</span>
a5 = <span class="hljs-number">1.061405429</span>
p = <span class="hljs-number">0.3275911</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Save the sign of x</p>
</div>
<div class="content"><div class='highlight'><pre> sign = <span class="hljs-number">1</span>
sign = -<span class="hljs-number">1</span> <span class="hljs-keyword">if</span> x < <span class="hljs-number">0</span>
x = Math.abs(x)</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Formula 7.1.26 from:</p>
<ul>
<li>Abramowitz, M. and Stegun, I. A. (Eds.). “Error Function and Fresnel Integrals.” Ch. 7 in Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables, 9th printing. New York: Dover, pp. 297-309, 1972.</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> t = <span class="hljs-number">1.0</span> / (<span class="hljs-number">1.0</span> + p * x)
y = <span class="hljs-number">1.0</span> - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x)
sign * y</pre></div></div>
</li>
</ul>
</div>
</body>
</html>