I very much like your idea of setting *math-context* (I was looking for something like that, but could not find it in the documentation), but how do I do it globally?
user=> (set! *math-context* java.math.MathContext/DECIMAL128) #<CompilerException java.lang.IllegalStateException: Can't change/ establish root binding of: *math-context* with set (NO_SOURCE_FILE:0)> whereas (set! *warn-on-reflection* true) works fine? What is the difference? How is it done right? On Jun 29, 2:24 pm, Christophe Grand <christo...@cgrand.net> wrote: > 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 -~----------~----~----~----~------~----~------~--~---