Skip to content

Instantly share code, notes, and snippets.

@natevw
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natevw/c30cd4f0c27323e3c275 to your computer and use it in GitHub Desktop.
Save natevw/c30cd4f0c27323e3c275 to your computer and use it in GitHub Desktop.
Simple fma (usually "fused multiply-add", but in this case it's not really fused so we'll say "fixed", or "functional", or "fun", or the recursive backronym "fma is not unix") template helper for Django — multiply the first two numbers provided and then add the last to that. Optional p= kwarg for rounding results to a fixed precision. When used …
from django import template
#import helper_from_linked_gist as template
register = template.Library()
@register.simple_tag # {% fma m x b %}
#@register.assignment_tag # {% fma m x b as y %}
#@register.simple_assignment_tag # optional "as y", requires using helper_from_linked_gist above
def fma(x, y, z, p=None):
v = float(x) * float(y) + float(z)
return v if p is None else ("%%.%uf" % p) % v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment