Skip to content

Commit

Permalink
Add arithmetic simulations to verify precision and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sernamar committed Sep 24, 2024
1 parent dd821f8 commit f336596
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions dev/arithmetic_operations_simulation.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns dev.arithmetic-operations-simulation
(:require [dinero.core :as core]))

(defn money-simulation
[money]
(-> money
(core/add (core/money-of 1234567.3444 :eur))
(core/subtract (core/money-of 232323 :eur))
(core/multiply 3.4)
(core/divide 5.456)))

(time
(reduce (fn [acc _] (money-simulation acc)) (core/money-of 0 :eur) (range 1000000)))
;; => "Elapsed time: 3795.038377 msecs"
;; => {:amount 1657407.9625291828793774319066147859922178988326848249027237354085603112840466926070038910505836575875486381322957198443579766536964980544747081712062256809338521400778210116731517509727626459143968871595330739299610894941634241245136186770428015564202334630350194M, :currency :eur}

(defn rounded-money-simulation
[money]
(-> money
(core/add (core/rounded-money-of 1234567.3444 :eur 2 :half-up))
(core/subtract (core/rounded-money-of 232323 :eur 2 :half-up))
(core/multiply 3.4)
(core/divide 5.456)))

(time
(reduce (fn [acc _] (rounded-money-simulation acc)) (core/rounded-money-of 0 :eur 2 :half-up) (range 1000000)))
;; => "Elapsed time: 7031.994195 msecs"
;; => {:amount 1657407.95M, :currency :eur, :scale 2, :rounding-mode :half-up}

0 comments on commit f336596

Please sign in to comment.