Don't know what you mean by "guaranteeing a floating point comparison".

In any event, you should never check equality when comparing floating
point numbers (except perhaps in special cases).  Instead, use a
tolerance:

tol = 1e-12;
if (abs(a-b) < tol)
  # Close enough
else
  # not equal
end

The problems you see in other languages are due to the fact they round
the values they display (but the underlying values are not rounded).
The return you got from Juila was not rounded for display.

Stuart


On Wed, 5 Nov 2014, K Leo wrote:

julia> 2*10.97 + 23.9985
45.938500000000005

julia> 2*10.97 + 23.9985 == 45.938500000000005
true

Amazing. I never expected this. Is floating point comparison going to be guaranteed?

On 2014?11?05? 08:48, Stefan Karpinski wrote:
Some systems round their answers as John said but it's easy to check that it's a lie:

    R version 3.1.0 (2014-04-10) -- "Spring Dance"
    > 2*10.97 + 23.9985
    [1] 45.9385
    > 2*10.97 + 23.9985 == 45.9385
    [1] FALSE

    This is perl 5, version 16, subversion 2 (v5.16.2)
      DB<1> x 2*10.97 + 23.9985
    0  45.9385
      DB<2> x 2*10.97 + 23.9985 == 45.9385
    0  ''


I don't have a working copy of Matlab right now, but I think it does this too.

On Tue, Nov 4, 2014 at 8:31 PM, Neil Devadasan <[email protected] <mailto:[email protected]>> wrote:

    Thanks

    On Tuesday, November 4, 2014 2:13:37 PM UTC-5, John Myles White
    wrote:

        Hi Neil,

        Julie does math the same way that all computers do math.
        You're probably coming from another language where a lot of
        effort is invested into pretending that computers offer a
        closer approximation to abstract mathematics than they
        actually do. Those systems have been lying to you.

        Put another way: you just took the red pill by using Julia.

         -- John

        On Nov 4, 2014, at 11:06 AM, Neil Devadasan
        <[email protected]> wrote:

        > julia> f(x::Float64, y::Float64) = 2x + y;
        >
        > julia> f(10.97,23.9985)
        > 45.938500000000005
        >
        > The above method execution of function f returns an answer
        that I cannot understand.  Can someone clarify?
        >
        > Thank you.




Reply via email to