Popular recipes tagged "maths" but not "complex"http://code.activestate.com/recipes/tags/maths-complex/2012-04-27T02:10:56-07:00ActiveState Code RecipesTaxi Fare Splitter (Python) 2012-04-27T02:10:56-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/578115-taxi-fare-splitter/ <p style="color: grey"> Python recipe 578115 by <a href="/recipes/users/4167379/">James Coliins</a> (<a href="/recipes/tags/barganing/">barganing</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/optimisation/">optimisation</a>). </p> <p>A method for allocating costs 'fairly' amongst a group of friends who cooperate to their mutual advantage.</p> Integer square root function (Python) 2011-08-04T05:29:16-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577821-integer-square-root-function/ <p style="color: grey"> Python recipe 577821 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/isqrt/">isqrt</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>). </p> <p>The <em>integer square root</em> function, or isqrt, is equivalent to floor(sqrt(x)) for non-negative x. For small x, the most convenient way to calculate isqrt is by calling int(x**0.5) or int(math.sqrt(x)), but if x is a large enough integer, the sqrt calculation overflows.</p> <p>You can calculate the isqrt without any floating point maths, using just pure integer maths, allowing the function to operate with numbers far larger than possible with floats:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; n = 1234567*(10**1000) &gt;&gt;&gt; n2 = n*n &gt;&gt;&gt; math.sqrt(n2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; OverflowError: long int too large to convert to float &gt;&gt;&gt; isqrt(n2) == n True </code></pre> Approximately Equal (Python) 2010-03-17T17:05:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577124-approximately-equal/ <p style="color: grey"> Python recipe 577124 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/comparisons/">comparisons</a>, <a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/numeric/">numeric</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Generic "approximately equal" function for any object type, with customisable error tolerance.</p> <p>When called with float arguments, approx_equal(x, y[, tol[, rel]) compares x and y numerically, and returns True if y is within either absolute error tol or relative error rel of x, otherwise return False. The function defaults to sensible default values for tol and rel.</p> <p>For any other pair of objects, approx_equal() looks for a method __approx_equal__ and, if found, calls it with arbitrary optional arguments. This allows types to define their own concept of "close enough".</p> Floating point range (Python) 2010-02-24T07:12:55-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577068-floating-point-range/ <p style="color: grey"> Python recipe 577068 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>Generator that produces floats, equivalent to range for integers, minimising rounding errors by using only a single multiplication and addition for each number, and no divisions.</p> <p>This generator takes an optional argument controlling whether it produces numbers from the open, closed, or half-open interval.</p> Spline demonstrator (Python) 2009-08-08T15:21:06-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/576871-spline-demonstrator/ <p style="color: grey"> Python recipe 576871 by <a href="/recipes/users/4167379/">James Coliins</a> (<a href="/recipes/tags/c_a_d/">c_a_d</a>, <a href="/recipes/tags/maths/">maths</a>). </p> <p>An ordered set of points is created using the mouse. A smooth curve(spline) is created following the points with either an open or closed(cyclic) path.</p>