Skip to content

Commit

Permalink
Clarify div docstring for floating-point input (#55918)
Browse files Browse the repository at this point in the history
Closes #55837

This is a variant of the warning found in the `fld` docstring clarifying
floating-point behaviour.
  • Loading branch information
Sagnac authored Oct 6, 2024
1 parent 2ae0b7e commit 7cc195c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions base/div.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ julia> div(4, 3, RoundFromZero)
julia> div(-4, 3, RoundFromZero)
-2
```
Because `div(x, y)` implements strictly correct truncated rounding based on the true
value of floating-point numbers, unintuitive situations can arise. For example:
```jldoctest
julia> div(6.0, 0.1)
59.0
julia> 6.0 / 0.1
60.0
julia> 6.0 / big(0.1)
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
```
What is happening here is that the true value of the floating-point number written
as `0.1` is slightly larger than the numerical value 1/10 while `6.0` represents
the number 6 precisely. Therefore the true value of `6.0 / 0.1` is slightly less
than 60. When doing division, this is rounded to precisely `60.0`, but
`div(6.0, 0.1, RoundToZero)` always truncates the true value, so the result is `59.0`.
"""
div(x, y, r::RoundingMode)

Expand Down

0 comments on commit 7cc195c

Please sign in to comment.