Commit e078833e authored by Joachim Schunk's avatar Joachim Schunk

return calculatio-error from phys-val-check

parent eccd5237
......@@ -12,7 +12,7 @@
[answer-scheme-version migrate-answer]]
[math-abs migrate-if-necessary]])]
[double-division math-abs migrate-if-necessary]])]
[clojure.string :as clj-str :refer [join split trim]]
......@@ -23,7 +23,7 @@
[answer-scheme-version migrate-answer]]
[math-abs migrate-if-necessary]])]))
[double-division math-abs migrate-if-necessary]])]))
(def prefix-mapping {"Y" 24
"Z" 21
......@@ -169,10 +169,14 @@
:factor (:factor based-units-map)
:oom (:oom based-units-map)}))
(defn tolerable-error? [calculation-error error-relative]
(when (every? #(and (number? %) (not (neg? %))) [calculation-error error-relative])
(<= (* calculation-error 100) error-relative)))
(defn convert-phys-val-to-ref-units [phys-val ref-units]
(let [phys-val-base-unit-map (units-to-baseunits (:units phys-val))
ref-base-unit-map (units-to-baseunits ref-units)
convert-factor (/ (:factor phys-val-base-unit-map) (:factor ref-base-unit-map))
convert-factor (double-division (:factor phys-val-base-unit-map) (:factor ref-base-unit-map))
convert-oom (- (:oom phys-val-base-unit-map) (:oom ref-base-unit-map))
significand (edn/read-string (get-in phys-val [:number :significand]))
oom (edn/read-string (get-in phys-val [:number :order-of-magnitude]))]
......@@ -294,14 +298,13 @@
ans-significand (get-combined-significand :ans)
calc-oom (get-combined-oom :calc)
ans-oom (get-combined-oom :ans)
oom-difference (- calc-oom ans-oom)
calc-multiplier (Math/pow 10 oom-difference)]
(when (<=
(math-abs (- ans-significand (* calc-multiplier calc-significand)))
(math-abs (* (/ 1 100) ans-significand)))
(defn check-answer [cur-calc cur-ans & {:keys [with-oom with-units]}]
oom-difference (- ans-oom calc-oom)
ans-multiplier (Math/pow 10 oom-difference)]
(assoc result :calculation-error (math-abs (double-division
(- calc-significand (* ans-significand ans-multiplier))
(defn check-phys-vals [cur-calc cur-ans & {:keys [with-oom with-units]}]
(some->> {}
(get-number-part cur-calc with-oom :calc)
(get-number-part cur-ans with-oom :ans)
......@@ -368,12 +371,13 @@
(defmethod check-step :constant [_ {:keys [ans-id ans-obj ex-obj assignment res-map]}]
(let [update-res-map (fn [points] (assoc res-map ans-id {:points points :points-max 4}))]
(if (and (get-in ans-obj [:number :significand])
{:number (:number ex-obj) :units (:units assignment)}
(select-keys ans-obj [:number :units])
:with-oom true :with-units true))
(update-res-map 4)
(if (get-in ans-obj [:number :significand])
(let [phys-val-check-res (check-phys-vals {:number (:number ex-obj) :units (:units assignment)}
(select-keys ans-obj [:number :units])
:with-oom true :with-units true)]
(if (tolerable-error? (:calculation-error phys-val-check-res) 1)
(update-res-map 4)
(update-res-map 2)))
(update-res-map 2))))
(defmethod check-step :formula [_ {:keys [ans-id ans-obj ex-obj assignment param-id-map ex-id-map
......@@ -389,11 +393,11 @@
(if-not (some (comp nil? val) match-var-map)
(let [calc-mapping (:calc-mapping assignment)
formula-vec (formula-to-vec (:formula assignment))
calc-number (mathjs-str-to-number-obj (calc-step formula-vec calc-mapping match-var-map))]
(if (check-answer
{:number calc-number :units (:units assignment)}
(select-keys ans-obj [:number :units])
:with-oom true :with-units true)
calc-number (mathjs-str-to-number-obj (calc-step formula-vec calc-mapping match-var-map))
phys-val-check-res (check-phys-vals {:number calc-number :units (:units assignment)}
(select-keys ans-obj [:number :units])
:with-oom true :with-units true)]
(if (tolerable-error? (:calculation-error phys-val-check-res) 1)
(update-res-map 6)
(update-res-map 4)))
(update-res-map 2))))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment