Last active
August 29, 2015 14:09
-
-
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 …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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