On Mon, Jun 29, 2009 at 1:23 PM, arasoft <t...@arasoft.de> wrote:

>
> At least I believe so: it would allow client code to set the desired
> precision once and then be able to invoke functions that take an
> optional precision parameter (or none at all) without having to
> specify precision every time.



If your code allow precision to be tuned in specific area, I think you
should expose your own specific *default-precision* and bind or set! it once
for all (and not at each call).
If you really want to set! application-wide precision, you should directly
set (or redef) *math-context* or wrap the whole computation in a single
with-precision:

(defn harmonic-number
   ([]
     ((fn more [sum n] (let [new-sum (+ sum (/ 1 (bigdec n)))]
       (lazy-seq (cons (bigdec new-sum) (more new-sum (inc n))))))
       0 1))
   ([n]
       (reduce + (map #(/
1 (bigdec %)) (range 1 (inc n))))))

(with-precision (inc (.getPrecision java.math.MathContext/DECIMAL128))
 :rounding HALF_EVEN
  (doall (take 10 (harmonic-number))))

This code doesn't yield numbers with the same precision as yours because, in
your code, + wasn't "precisioned".

hth,

Christophe


-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to