Skip to content

Commit 7cc195c

Browse files
authored
Clarify div docstring for floating-point input (#55918)
Closes #55837 This is a variant of the warning found in the `fld` docstring clarifying floating-point behaviour.
1 parent 2ae0b7e commit 7cc195c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

base/div.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ julia> div(4, 3, RoundFromZero)
4343
julia> div(-4, 3, RoundFromZero)
4444
-2
4545
```
46+
Because `div(x, y)` implements strictly correct truncated rounding based on the true
47+
value of floating-point numbers, unintuitive situations can arise. For example:
48+
```jldoctest
49+
julia> div(6.0, 0.1)
50+
59.0
51+
julia> 6.0 / 0.1
52+
60.0
53+
julia> 6.0 / big(0.1)
54+
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
55+
```
56+
What is happening here is that the true value of the floating-point number written
57+
as `0.1` is slightly larger than the numerical value 1/10 while `6.0` represents
58+
the number 6 precisely. Therefore the true value of `6.0 / 0.1` is slightly less
59+
than 60. When doing division, this is rounded to precisely `60.0`, but
60+
`div(6.0, 0.1, RoundToZero)` always truncates the true value, so the result is `59.0`.
4661
"""
4762
div(x, y, r::RoundingMode)
4863

0 commit comments

Comments
 (0)