You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
# OverflowContexts.jl
2
2
3
3
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.
6
6
7
7
Together, these provide checked and unchecked contexts, as in other languages like C#:
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
11
11
checked or permissive operation, and thus are not affected by switching the default. Symbols for the functions will also be replaced, to support
12
12
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
13
13
context and vice versa.
14
14
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
16
16
or overflow-permissive operations, respectively, within the module it was executed in. All arguments that don't support overflow checking are passed
17
17
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.
@checkeda() # doesn't cross function boundary; no OverflowError
43
43
44
-
@default_checked
44
+
@default_checked# previous uses of operator in this module recompiled with new default
45
45
a() # ERROR: OverflowError: 9223372036854775807 + 1 overflowed for type Int64
46
46
47
47
@uncheckeda() # doesn't cross function boundary; still throws OverflowError
@@ -53,8 +53,8 @@ a() # -9223372036854775808
53
53
@checkedfoldl(+, (typemax(Int), 1))
54
54
55
55
# assignment operators
56
-
a=typemax(Int)
57
-
@checkeda+=1# OverflowError: 9223372036854775807 + 1 overflowed for type Int64
56
+
aa=typemax(Int)
57
+
@checkedaa+=1# OverflowError: 9223372036854775807 + 1 overflowed for type Int64
58
58
```
59
59
60
60
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