djangosnippets.org: Latest snippets tagged with 'math'http://djangosnippets.org/tags/math/2011-09-08T00:31:11.139417-05:00Math Captcha Field and Widget
2011-09-08T00:31:11.139417-05:00btaylordesignhttp://djangosnippets.org/snippets/2543/<p>This math captcha field and widget was inspired by Justin Quick's django-math-captcha, but I wanted to make it into one form field and not have anything in settings.py. I removed the division and modulo operators to avoid people having to input fractions, and just randomly select the operator.</p>
<p>It leverages …</p>
Freely redistributablemath tag
2011-05-02T13:07:36.982994-05:00itchyfingrshttp://djangosnippets.org/snippets/2424/<p>Syntax:
{% math <argument, ..> "expression" as var_name %}</p>
<p>Evaluates a math expression in the current context and saves the value into a variable with the given name.</p>
<p>"$<number>" is a placeholder in the math expression. It will be replaced by the value of the argument at index <number> - …</p>
Freely redistributablemath filter
2011-04-11T12:02:52.998518-05:00itchyfingrshttp://djangosnippets.org/snippets/2409/<p>A filter for general math operations.</p>
Freely redistributableFloatField with safe expression parsing
2010-03-15T19:21:35.761081-05:00joelegnerhttp://djangosnippets.org/snippets/1959/<p>This FloatField replacement allows users to enter math expressions, such as:</p>
<p>4/5 + sqrt(32)</p>
<p>And will evaluate them safely when the field's clean() function is called. In the example above, it will evaluate to a float value of about 6.457.</p>
<p>Reference:</p>
<p><a href="http://lybniz2.sourceforge.net/safeeval.html">http://lybniz2.sourceforge.net/safeeval.html</a></p>
<p>The available functions are listed herein. Note that …</p>
Freely redistributableTemplate filter for formatting negative numbers
2009-04-16T19:30:58.074362-05:00pkshiuhttp://djangosnippets.org/snippets/1447/<p>I have a need to conditionally format a negative number, a hedgefund's daily price change, Excel style. i.e. show a negative number as a parenthesized number instead of a negative sign. Here is a filter that will do that and more, solving a more general case. See the doctest for …</p>
Freely redistributableSignificant digits filter
2009-03-13T17:31:12.462661-05:00joelegnerhttp://djangosnippets.org/snippets/1372/<p>Formats float values with specified number of significant digits (defaults to 3).</p>
<p>Usage:</p>
<p><code>{{value|sigdig}} # with 3 significant digits by default</code>
<code>{{value|sigdig:digits}}</code></p>
<p>Examples:</p>
<p><code>{{0.001432143|sigdig}}</code> renders as <code>0.00143</code></p>
<p><code>{{874321.4327184|sigdig}}</code> renders as <code>874000</code></p>
<p><code>{{874321.4327184|sigdig:5}}</code> renders as <code>874320</code></p>
<p>Useful for scientific or engineering presentation.</p>
Freely redistributableRPN template math
2008-10-24T22:56:18.989272-05:00durkahttp://djangosnippets.org/snippets/1156/<p>Django's templates don't provide much in the way of arithmetic: there is an "add" filter and that is about it. Even if sub, mult and div filters are implemented, it is difficult to chain filters while preserving some complicated expression, such as ((x+3)4-(2-y)/12.75). However, this expression can be converted into …</p>
Freely redistributable