Created
February 19, 2014 19:26
-
-
Save jcromartie/9099643 to your computer and use it in GitHub Desktop.
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
;;; MrMusAddict's original function | |
(def charge-max 100.0) | |
(def charge-min 0.0) | |
(def bright-max 0.9) | |
(def bright-min 0.0) | |
;;; float brightFinal = (brightMin-brightMax)*Math.Pow((chargeCurrent-chargeMax)/(chargeMax-chargeMin), 4)+brightMax; | |
(defn mr-mus-addict | |
[charge] | |
(+ bright-max | |
(* (- bright-min bright-max) | |
(Math/pow (/ (- charge charge-max) | |
(- charge-max charge-min)) | |
4)))) | |
;; a slight optimization by just multiplying four times, possibly | |
;; losing some accuracy due to floating point implementation | |
(defn mr-mus-addict' | |
[charge] | |
(let [n (/ (- charge charge-max) | |
(- charge-max charge-min))] | |
(+ bright-max | |
(* (- bright-min bright-max) | |
(* n n n n))))) | |
(def int-lookup | |
(mapv #(mr-mus-addict %) (range 101))) | |
(defn mr-mus-addict-lookup | |
[c] | |
(nth int-lookup c)) | |
;; mrhorrible's version (-0.00011*((chargeCurrent-90)^2))+0.9 | |
(defn mr-horrible | |
[^long charge] | |
(+ (* -0.00011 (Math/pow charge 2)) 0.9)) | |
;; my simplified version, using a charge range from [0,1] | |
(defn djork | |
[^double c] | |
(let [x (- 1 c)] | |
(- 1 (* x x x x)))) | |
;; now a lookup table version, which is actually slower | |
(let [lookup (mapv #(djork (/ % 100)) (range 101))] | |
(defn djork-lookup | |
[^double c] | |
(nth lookup (* c 100)))) | |
;; now a macro version... this is where it gets interesting folks | |
(defn compile-unrolled | |
[f inputs] | |
(let [clauses (mapcat #(list % (f %)) inputs)] | |
`(fn [x#] | |
(case x# | |
~@clauses | |
(~f x#))))) | |
(defn unrolled | |
[f inputs] | |
(eval (compile-unrolled f inputs))) | |
(def mr-mus-addict-unrolled | |
(unrolled mr-mus-addict (range 101))) | |
(def mr-horrible-unrolled | |
(unrolled mr-horrible (range 101))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment