Skip to content

Commit

Permalink
Manual: Mention Base.Checked module in integer overflow handling (J…
Browse files Browse the repository at this point in the history
…uliaLang#52071)

The `Base.Checked` module is not mentioned in this section of the
manual. This helps clarify to the reader an alternative solution
provided by `Base` in addition to use of `big()`.
  • Loading branch information
sjkelly authored and pull[bot] committed Jan 24, 2024
1 parent e49ad01 commit 911cf6a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/src/base/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Base.minmax
Base.Math.clamp
Base.Math.clamp!
Base.abs
Base.Checked
Base.Checked.checked_abs
Base.Checked.checked_neg
Base.Checked.checked_add
Expand Down
9 changes: 4 additions & 5 deletions doc/src/manual/integers-and-floating-point-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,10 @@ julia> x + 1 == typemin(Int64)
true
```

Thus, arithmetic with Julia integers is actually a form of [modular arithmetic](https://en.wikipedia.org/wiki/Modular_arithmetic).
This reflects the characteristics of the underlying arithmetic of integers as implemented on modern
computers. In applications where overflow is possible, explicit checking for wraparound produced
by overflow is essential; otherwise, the [`BigInt`](@ref) type in [Arbitrary Precision Arithmetic](@ref)
is recommended instead.
Arithmetic operations with Julia's integer types inherently perform [modular arithmetic](https://en.wikipedia.org/wiki/Modular_arithmetic),
mirroring the characteristics of integer arithmetic on modern computer hardware. In scenarios where overflow is a possibility,
it is crucial to explicitly check for wraparound effects that can result from such overflows.
The [`Base.Checked`](@ref) module provides a suite of arithmetic operations equipped with overflow checks, which trigger errors if an overflow occurs. For use cases where overflow cannot be tolerated under any circumstances, utilizing the [`BigInt`](@ref) type, as detailed in [Arbitrary Precision Arithmetic](@ref), is advisable.

An example of overflow behavior and how to potentially resolve it is as follows:

Expand Down

0 comments on commit 911cf6a

Please sign in to comment.