Skip to content
New issue

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

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

Already on GitHub? # to your account

Inexact construction of Decimal from float #85

Closed
barucden opened this issue Nov 4, 2024 · 0 comments · Fixed by #86
Closed

Inexact construction of Decimal from float #85

barucden opened this issue Nov 4, 2024 · 0 comments · Fixed by #86

Comments

@barucden
Copy link
Collaborator

barucden commented Nov 4, 2024

julia> Decimal(0.1)
0.1

julia> Decimal(big(0.1))
0.1000000000000000055511151231257827021181583404541015625

This is problematic because then

julia> x = 0.1
0.1

julia> y = Decimal(x)
0.1

julia> x == y
true

julia> hash(x) == hash(y)
false

It can be improved:

julia> z = Decimal(big(0.1))
0.1000000000000000055511151231257827021181583404541015625

julia> hash(x) == hash(z)
true
barucden added a commit to barucden/Decimals.jl that referenced this issue Nov 6, 2024
Numbers such as `0.3` or `1.1` cannot be represented exactly using
`Float32` or `Float64`. We can all digits by converting them to
`BigFloat`:
```julia
julia> big(0.3)
0.299999999999999988897769753748434595763683319091796875

julia> big(1.1)
1.100000000000000088817841970012523233890533447265625
```
This PR updates the `Decimal` constructors so that floating point
numbers are converted to `Decimal`s exactly in the sense that
```julia
julia> hash(1.1) == hash(Decimal(1.1))
true
```

Fixes JuliaMath#85
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant