mirror of
https://github.com/Gnucash/gnucash.git
synced 2024-11-30 20:54:08 -06:00
[report-utilities] fix (gnc:make-stats-collector)
allows it to compare numbers with +/-inf.0 -- 10E9 is an arbitrary number to compare numbers. (< N +inf.0) is guaranteed to be #t for all numbers N whereby N is not also +inf.0.
This commit is contained in:
parent
60558f6ad1
commit
e3160af417
@ -177,40 +177,35 @@ construct gnc:make-gnc-monetary and use gnc:monetary->string instead.")
|
||||
;; yes. I think that would still be faster.
|
||||
|
||||
(define (gnc:make-stats-collector)
|
||||
(let ;;; values
|
||||
((value 0)
|
||||
(totalitems 0)
|
||||
(max -10E9)
|
||||
(min 10E9))
|
||||
(let ;;; Functions to manipulate values
|
||||
((adder (lambda (amount)
|
||||
(if (number? amount)
|
||||
(begin
|
||||
(set! value (+ amount value))
|
||||
(if (> amount max)
|
||||
(set! max amount))
|
||||
(if (< amount min)
|
||||
(set! min amount))
|
||||
(set! totalitems (+ 1 totalitems))))))
|
||||
(getnumitems (lambda () totalitems))
|
||||
(gettotal (lambda () value))
|
||||
(getaverage (lambda () (/ value totalitems)))
|
||||
(getmax (lambda () max))
|
||||
(getmin (lambda () min))
|
||||
(reset-all (lambda ()
|
||||
(set! value 0)
|
||||
(set! max -10E9)
|
||||
(set! min 10E9)
|
||||
(set! totalitems 0))))
|
||||
(lambda (action value) ;;; Dispatch function
|
||||
(case action
|
||||
((add) (adder value))
|
||||
((total) (gettotal))
|
||||
((average) (getaverage))
|
||||
((numitems) (getnumitems))
|
||||
((getmax) (getmax))
|
||||
((getmin) (getmin))
|
||||
((reset) (reset-all))
|
||||
(let ((value 0)
|
||||
(totalitems 0)
|
||||
(maximum -inf.0)
|
||||
(minimum +inf.0))
|
||||
(let ((adder (lambda (amount)
|
||||
(when (number? amount)
|
||||
(set! value (+ amount value))
|
||||
(if (> amount maximum) (set! maximum amount))
|
||||
(if (< amount minimum) (set! minimum amount))
|
||||
(set! totalitems (1+ totalitems)))))
|
||||
(getnumitems (lambda () totalitems))
|
||||
(gettotal (lambda () value))
|
||||
(getaverage (lambda () (/ value totalitems)))
|
||||
(getmax (lambda () maximum))
|
||||
(getmin (lambda () minimum))
|
||||
(reset-all (lambda ()
|
||||
(set! value 0)
|
||||
(set! maximum -inf.0)
|
||||
(set! minimum +inf.0)
|
||||
(set! totalitems 0))))
|
||||
(lambda (action value)
|
||||
(case action
|
||||
((add) (adder value))
|
||||
((total) (gettotal))
|
||||
((average) (getaverage))
|
||||
((numitems) (getnumitems))
|
||||
((getmax) (getmax))
|
||||
((getmin) (getmin))
|
||||
((reset) (reset-all))
|
||||
(else (gnc:warn "bad stats-collector action: " action)))))))
|
||||
|
||||
(define (gnc:make-drcr-collector)
|
||||
|
@ -22,6 +22,7 @@
|
||||
(test-commodity-collector)
|
||||
(test-get-account-balances)
|
||||
(test-monetary-adders)
|
||||
(test-make-stats-collector)
|
||||
(test-end "report-utilities"))
|
||||
|
||||
(define (NDayDelta t64 n)
|
||||
@ -503,3 +504,50 @@
|
||||
"gnc:monetary+ with >1 currency fails"
|
||||
#t
|
||||
(gnc:monetary+ usd10 usd10 eur8))))
|
||||
|
||||
(define (test-make-stats-collector)
|
||||
(test-begin "gnc:make-stats-collector")
|
||||
(let ((s (gnc:make-stats-collector)))
|
||||
(test-equal "initial s is 0"
|
||||
0
|
||||
(s 'total #f))
|
||||
|
||||
(s 'add 5)
|
||||
(test-equal "s+=5 is 5"
|
||||
5
|
||||
(s 'total #f))
|
||||
|
||||
(s 'add 9)
|
||||
(test-equal "s+=9 is 14"
|
||||
14
|
||||
(s 'total #f))
|
||||
|
||||
(test-equal "avg(s) is 7"
|
||||
7
|
||||
(s 'average #f))
|
||||
|
||||
(s 'add 1E12)
|
||||
(s 'add -1E13)
|
||||
|
||||
(test-equal "max(s) is now 1E12"
|
||||
1E12
|
||||
(s 'getmax #f))
|
||||
|
||||
(test-equal "min(s) is now -1E13"
|
||||
-1E13
|
||||
(s 'getmin #f))
|
||||
|
||||
(s 'add 9E12)
|
||||
(test-equal "newavg(s) is 2.8"
|
||||
2.8
|
||||
(s 'average #f))
|
||||
|
||||
(test-equal "num(s) is 5"
|
||||
5
|
||||
(s 'numitems #f))
|
||||
|
||||
(s 'reset #f)
|
||||
(test-equal "after reset num(s) is 0"
|
||||
0
|
||||
(s 'numitems #f)))
|
||||
(test-end "gnc:make-stats-collector"))
|
||||
|
Loading…
Reference in New Issue
Block a user