Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exact comparisons with binary values/directed conversions #79

Closed
simonbyrne opened this issue Aug 17, 2018 · 7 comments · Fixed by #121
Closed

exact comparisons with binary values/directed conversions #79

simonbyrne opened this issue Aug 17, 2018 · 7 comments · Fixed by #121

Comments

@simonbyrne
Copy link
Member

simonbyrne commented Aug 17, 2018

Currently

julia> using DecFP

julia> d64"1e100" == 1e100
true

which might seem reasonable, except that

julia> big(1e100)
1.000000000000000015902891109759918046836080856394528138978132755774783877217038e+100

Since the library doesn't appear to offer comparisons, the one option would be to do it via directed conversions, e.g.

<(x::Float64, y::Dec64) = x < Float64(y, RoundUp)
<=(x::Float64, y::Dec64) = x <= Float64(y, RoundDown)
==(x::Float64, y::Dec64) = x == Float64(y, RoundUp) == Float64(y, RoundDown)

Is it possible to do this without touching the global rounding mode?

@ScottPJones
Copy link
Contributor

Those comparisons won't be exact though. Although it might be slower, if you want accurate results you might do better making a grisu like conversion of the binary floating point value, and then do the comparison using those values. That technique would work for any binary floating point numbers, including BigFloat and the ones from DoubleFloat.jl, Quadmath.jl. Since they would be exact comparisions, no rounding would be needed.

@simonbyrne
Copy link
Member Author

why wouldn't they be exact?

@simonbyrne
Copy link
Member Author

simonbyrne commented Feb 28, 2019

e.g.

x = d64"1e100"
using DecFP
setrounding(Dec64, RoundDown)
x < 1e100

correctly returns false

@ScottPJones
Copy link
Contributor

If you have to do rounding, how can they be exact?

@simonbyrne
Copy link
Member Author

Because we can control the direction of rounding. It is similar to the way that we compare floats to Irrationals (https://simonbyrne.github.io/talks/juliacon2018#/11), though requires a few extra checks since they can be equal.

@jmkuhn
Copy link
Contributor

jmkuhn commented Feb 26, 2020

For the comparison

d64"1e100" == 1e100

the Float64 1e100 is getting promoted to a Dec64. The comparison is really

d64"1e100" == convert(Dec64, 1e100)

Note

julia> convert(Dec64, 1e100)
1.0e100

That is exactly 1e100, not just approximately 1e100 rounded for printing.

Edit:

My original explanation of how the conversion is done was incorrect, but what remains above is true.

@jmkuhn
Copy link
Contributor

jmkuhn commented Feb 27, 2020

julia> convert(Dec64, 1e100)
1.0e100

julia> convert(Dec128, 1e100)
1.000000000000000015902891109759918e100

julia> d64"1e100" == 1e100
true

julia> d128"1e100" == 1e100
false

jmkuhn added a commit to jmkuhn/DecFP.jl that referenced this issue May 11, 2020
stevengj pushed a commit that referenced this issue May 13, 2020
* Comparison with Float (issue #79)

* Test 1e100

* Simplify <=, >=

* Simplify flt op dec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants