3 ms·
I tried this with Julia and got the answer 1. e(x) = x == 0 ? 1 : (exp(x) - 1) / x q(x) = abs(x - sqrt(x^2 + 1)) - 1 / (x + sqrt(x^2 + 1)) h(x) = e
by _nalply 7y ago
I tried this with Julia and got the answer 1.
e(x) = x == 0 ? 1 : (exp(x) - 1) / x
q(x) = abs(x - sqrt(x^2 + 1)) - 1 / (x + sqrt(x^2 + 1))
h(x) = e(q(x) ^ 2)
What's happening here?
Is Julia switching to arbitrary precision like bc -l or rewriting exp(x) - 1 to expm1(x) or doing something else? Seems a lot of magic to me.
- StefanKarpinski 7y agoNot sure what’s going on but Julia is definitely not switching to arbitrary precision without being explicitly asked to do so. That would be very unjulian. I’m not at a computer or I’d investigate. One hypothesis based on some other comments is that getting the wrong answer (especially when using expm1) may be due to unsafe C compiler flags, which Julia does not apply by default. Just a guess though. Update from reading more comments: it seems that Rust and C via godbolt without questionable C flags also don’t fail like this. So I wonder if John hadn’t compiled the code with fast-math on, which will allow me the compiler to give all kinds of wrong answers.
- _nalply 7y agoI am using homebrew cask julia version 1.1.0 on macOS 10.13.6.
- mkl 7y agoI get 0 with Julia 1.1.1 on Ubuntu: julia> e(x) = x == 0 ? 1 : (exp(x) - 1) / x e (generic function with 1 method) julia> q(x) = abs(x - sqrt(x^2 + 1)) - 1 / (x + sqrt(x^2 + 1)) q (generic function with 1 method) julia> h(x) = e(q(x) ^ 2) h (generic function with 1 method) julia> for x in (15., 16., 17., 9999.) println(h(x)) end 0.0 0.0 0.0 0.0 I get the same in Python. They're all using standard hardware 64 bit floating point numbers in the same way.