Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-07 Thread Lee Spector
On Feb 7, 2014, at 11:45 AM, Andy Fingerhut wrote: > You may also use a let form wrapped around your entire defproject if you want > to avoid the duplication of code present in your example. Thanks -- I actually noticed that after I posted. I don't know why, but I never thought of project.clj a

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-07 Thread Andy Fingerhut
You may also use a let form wrapped around your entire defproject if you want to avoid the duplication of code present in your example. Andy On Fri, Feb 7, 2014 at 8:22 AM, Lee Spector wrote: > > On Feb 5, 2014, at 11:42 PM, Michał Marczyk wrote: > > > This returns > > > > (.getTotalPhysicalMe

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-07 Thread Lee Spector
On Feb 5, 2014, at 11:42 PM, Michał Marczyk wrote: > This returns > > (.getTotalPhysicalMemorySize > (java.lang.management.ManagementFactory/getOperatingSystemMXBean)) > > You could use this in your project.clj, perhaps by including > > ~(str "-Xms" (quot (.getTotalPhysicalMemorySize ...) appr

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-06 Thread Glen Fraser
Probably because you're using 0 through 3 as the arguments to fesetround(), rather than the proper #defined values: (e.g. on my Mac, from fenv.h) #define FE_TONEAREST0x #define FE_DOWNWARD 0x0400 #define FE_UPWARD 0x0800 #define FE_TOWARDZERO 0x0c00 You shoul

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-06 Thread Bruno Kim Medeiros Cesar
Also, I've made a test for this function for all float values in C: https://gist.github.com/brunokim/8843039 Unfortunetely it doesn't work in my system, as it does not have other rounding modes available besides the default. If anyone suceeds in running it, please report. On Thursday, February

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-06 Thread Bruno Kim Medeiros Cesar
Just to add a bit to the thread: the Java compiler treats java.lang.Math differently when more efficient alternatives are available. StrictMath is used only as a fallback. >From the java.lang.Math >javadoc : > By default many of the

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-06 Thread Bruno Kim Medeiros Cesar
Just to add a bit to the thread: the Java compiler treats java.lang.Math differently when more efficient alternatives are available. StrictMath is used only as a fallback. >From the java.lang.Math >javadoc : > By default many of the

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Michał Marczyk
This returns (.getTotalPhysicalMemorySize (java.lang.management.ManagementFactory/getOperatingSystemMXBean)) You could use this in your project.clj, perhaps by including ~(str "-Xms" (quot (.getTotalPhysicalMemorySize ...) appropriate-number)) in :jvm-opts. Also, you can absolutely use your ow

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Lee Spector
On Feb 5, 2014, at 8:50 PM, Bruce Adams wrote: > Modern JVM's pick default heap sizes based on the physical memory in > your machine. With more than 1GB of physical memory, initial heap is > 1/64 and maximum heap is 1/4 of physical memory.[1] > > For OpenJDK and Oracle, this command: >java -X

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Bruce Adams
Modern JVM's pick default heap sizes based on the physical memory in your machine. With more than 1GB of physical memory, initial heap is 1/64 and maximum heap is 1/4 of physical memory.[1] For OpenJDK and Oracle, this command: java -XX:+PrintFlagsFinal -version | grep HeapSize will show the i

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Lee Spector
On Feb 5, 2014, at 6:05 PM, Alex Miller wrote: > To override the default tiered compilation, add this to your project.clj: > :jvm-opts ^:replace [] I was under the impression that one can get the same effect by running your program with: lein trampoline with-profile production run [etc] True?

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Daniel
He is running 7. -- 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, se

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Colin Yates
Did I see a thread a while ago where doing this caught some people out because it wiped out some other performance switches? I can't find the thread. Apologies if I am spreading FUD On Wednesday, 5 February 2014 23:05:18 UTC, Alex Miller wrote: > > To override the default tiered compilatio

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Alex Miller
To override the default tiered compilation, add this to your project.clj: :jvm-opts ^:replace [] I would also recommend using a newer JDK (preferably 7, but at least 6). On Wednesday, February 5, 2014 4:34:12 PM UTC-6, David Nolen wrote: > > You need to make sure that you are running with serv

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread David Nolen
You need to make sure that you are running with server settings. If you are using lein, it's likely that this is not the case unless you have overridden lein's defaults in your project.clj. On Wed, Feb 5, 2014 at 5:30 PM, Glen Fraser wrote: > Thanks, yes, the version starting with 0.0 in the lo

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Glen Fraser
Thanks, yes, the version starting with 0.0 in the loop (rather than 0) does run faster. In my case, about 13% faster (19.7 seconds -- for the code you pasted below, with *unchecked-math*, type hints and starting x of 0.0 -- vs 22.7 seconds for my original version). But if you start with x of 0

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread David Nolen
(set! *unchecked-math* true) (defn g ^double [^double x] (+ (Math/sin (* 2.3 x)) (Math/cos (* 3.7 x (time (loop [i 1 x 0.0] (if (pos? i) (recur (dec i) (g x)) x))) This is nearly 50% faster than the original version on my machine. Note that x is bound to 0.0 in the loop, which allows t

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Glen Fraser
Thanks to both of you for these suggestions, they're good to know. In my specific case, setting the *unchecked-math* flag true did indeed speed things up slightly (by about 6%). The other change, though, with the double type hints (I assume that's what those are), actually ran notably slower (

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread David Nolen
Also: (defn g ^double [^double x] (+ (Math/sin (* 2.3 x)) (Math/cos (* 3.7 x On Wed, Feb 5, 2014 at 2:07 PM, Alex Miller wrote: > Others have answered with many useful bits but I would mention that it > would possibly make a significant performance difference if you added this > to your co

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Alex Miller
Others have answered with many useful bits but I would mention that it would possibly make a significant performance difference if you added this to your code: (set! *unchecked-math* true) On Wednesday, February 5, 2014 7:17:13 AM UTC-6, Glen Fraser wrote: > > (sorry if you received an earlie

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread r
I'd agree here. This is actually a very nice example of a system that might be called "chaotic", though "chaos" is, even mathematically, a very vague term: 1) the iteration will never leave [-2, 2] 2) it won't converge because all 3 fixed points are unstable ( |f'(x_s)|>1 ) So, your example is

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Mark Engelberg
Ah, I see now that you are doing (g x) in your loop, not (g i), so scratch what I said about the loop running the wrong direction. On Wed, Feb 5, 2014 at 9:23 AM, Mark Engelberg wrote: > Looks to me like your Clojure loop runs in the opposite direction > (counting downwards) versus the other lan

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Glen Fraser
Thanks, this is a satisfying answer. You're probably right that the other languages are all using the C standard math library (I naïvely assumed Java would too, but I see that's not the case). And yes, as I said, it is a rather contrived (and chaotic) example. Glen. On Feb 5, 2014, at 6:22 P

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Mark Engelberg
Looks to me like your Clojure loop runs in the opposite direction (counting downwards) versus the other languages. Since your code only returns the result of the last iteration of the loop, it's not too surprising that they return completely different results -- the last iteration of the Clojure c

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Konrad Hinsen
--On 5 Feb 2014 05:17:13 -0800 Glen Fraser wrote: My benchmark iteratively runs a function 100M times: g(x) <-- sin(2.3x) + cos(3.7x), starting with x of 0. A quick look at the series you are computing suggests that it has chaotic behavior. Another quick looks shows that neither of the two va

Re: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Glen Fraser
Thanks for the tip. After reading your comment, I looked and discovered the Java library called StrictMath, and tried it (replacing Math/cos and Math/sin by the StrictMath versions). I did indeed get different results than with the regular library, but unfortunately still not the same answer

RE: Confused by Clojure floating-point differences (compared to other languages)

2014-02-05 Thread Jon Harrop
IIRC, Java provides unusual trigonometric functions which, I’m guessing, Clojure is using. I think the Java ones are actually more accurate (and slower) so you may well find the answer obtained on the JVM is more precise than the others. Cheers, Jon. From: clojure@googlegroups.com [m