Skip to content

Commit 068b66b

Browse files
authored
Add information on saturating and improve example
1 parent 356e5d9 commit 068b66b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# OverflowContexts.jl
22

33
This package conceptually extends `CheckedArithmetic.jl` to provide the following overall features:
4-
1. Ability to set a Module-level default to overflow-checked or overflow-permissive operations.
5-
2. Ability to specify whether a block of code should use overflow-checked or overflow-permissive operations regardless of the default.
4+
1. Ability to set a Module-level default to overflow-checked, overflow-permissive (unchecked), or saturating operations.
5+
2. Ability to specify whether a block of code should use overflow-checked, overflow-permissive (unchecked), or saturating operations regardless of the default.
66

77
Together, these provide checked and unchecked contexts, as in other languages like C#:
88
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked
99

10-
The expression-level `@checked` and `@unchecked` rewrite instances of `+`, `-`, `*`, `^`, and `abs` functions, to functions specific to the
10+
The expression-level `@checked`, `@unchecked`, and `@saturating` rewrite instances of `+`, `-`, `*`, `^`, and `abs` functions, to functions specific to the
1111
checked or permissive operation, and thus are not affected by switching the default. Symbols for the functions will also be replaced, to support
1212
calls like `foldl(+, v)`. If these macros are nested, the lowest level takes precedence so that an unchecked context can be nested inside a checked
1313
context and vice versa.
1414

15-
`@default_checked` and `@default_unchecked` create shadow copies of the `+`, `-`, `*`, `^`, and `abs` functions that redirect to overflow-checked
15+
`@default_checked`, `@default_unchecked`, and `@default_saturating` create shadow copies of the `+`, `-`, `*`, `^`, and `abs` functions that redirect to overflow-checked
1616
or overflow-permissive operations, respectively, within the module it was executed in. All arguments that don't support overflow checking are passed
1717
through to their respective Base methods. **Important:** If you wish to use this feature, the first usage of this macro must occur earlier than the first usage of the affected Base functions. It should only be set once per module, though switching is allowed for interactive use. It is not necessary to set a default to use the expression-level macros.
1818

@@ -41,7 +41,7 @@ d() = x + 1; c() = d(); b() = c(); a() = b();
4141
a() #-9223372036854775808
4242
@checked a() # doesn't cross function boundary; no OverflowError
4343

44-
@default_checked
44+
@default_checked # previous uses of operator in this module recompiled with new default
4545
a() # ERROR: OverflowError: 9223372036854775807 + 1 overflowed for type Int64
4646

4747
@unchecked a() # doesn't cross function boundary; still throws OverflowError
@@ -53,8 +53,8 @@ a() # -9223372036854775808
5353
@checked foldl(+, (typemax(Int), 1))
5454

5555
# assignment operators
56-
a = typemax(Int)
57-
@checked a += 1 # OverflowError: 9223372036854775807 + 1 overflowed for type Int64
56+
aa = typemax(Int)
57+
@checked aa += 1 # OverflowError: 9223372036854775807 + 1 overflowed for type Int64
5858
```
5959

6060
If you are implementing your own numeric types, this package should just work for you so long as you extend the Base operators and the Base.Checked `checked_` methods.

0 commit comments

Comments
 (0)