(bind-func histogram
(lambda (hist:HistBin* buf:float* len lower upper nbins)
(let ((current_bin:HistBin* null)
(range (- upper lower)))
(if (not (> range 0.))
(begin
(println "Error in histogram: range (- upper lower) must be greater than 0.0")
null)
(begin
;; setup the bins
(doloop (i nbins)
(tset! (pref-ptr hist i)
0
(+ lower (* (convert i) (/ range (convert nbins)))))
(tset! (pref-ptr hist i)
1
0.))
(doloop (i len)
(set! current_bin
;; use "mod nbins" to make sure it always goes
;; into a valid slot in memory
(pref-ptr hist (% (convert (* (convert nbins float) (/ (- (pref buf i) lower) range)) i64) nbins)))
(tset! current_bin 1 (+ (tref current_bin 1) 1.0)))
hist)))))