Skip to content

Commit ab565f2

Browse files
shethaaditAdit Sheth
andauthored
Add documentation for checked arithmetic operators with examples in F# (#43789)
* Fixed bug 41859. * Comments fix. --------- Co-authored-by: Adit Sheth <[email protected]>
1 parent 8108d6d commit ab565f2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/fsharp/language-reference/symbol-and-operator-reference/arithmetic-operators.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ This topic describes arithmetic operators that are available in F#.
99

1010
## Summary of Binary Arithmetic Operators
1111

12-
The following table summarizes the binary arithmetic operators that are available for unboxed integral and floating-point types.
12+
Arithmetic operations in F# can be performed in two modes: **Unchecked** and **Checked**. By default, arithmetic operations use unchecked behavior, which prioritizes performance but allows overflow/underflow. Checked operators prioritize safety by throwing exceptions in such cases.
13+
14+
### Unchecked Arithmetic Operators
15+
16+
The following table summarizes the binary arithmetic operators that are available for **Unchecked Arithmetic** with unboxed integral and floating-point types.
1317

1418
|Binary operator|Notes|
1519
|---------------|-----|
@@ -20,6 +24,30 @@ The following table summarizes the binary arithmetic operators that are availabl
2024
|`%` (remainder, rem)|Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand.|
2125
|`**` (exponentiation, to the power of)|Possible overflow condition when the result exceeds the maximum absolute value for the type.<br /><br />The exponentiation operator works only with floating-point types.|
2226

27+
The unchecked behavior does not throw exceptions when overflow or underflow occurs, making it less safe for arithmetic on large or edge-case values.
28+
29+
### Checked Arithmetic Operators
30+
31+
The following table summarizes the binary arithmetic operators that are available for **Checked Arithmetic** with unboxed integral types. Checked operators ensure that calculations are verified for overflow or underflow, providing safer arithmetic for critical applications.
32+
33+
| Binary Operator | Notes |
34+
|------------------|----------------------------------------------------------------------------------------|
35+
| `+` (addition, plus) | Throws an <xref:System.OverflowException> if the result exceeds the maximum value or goes below the minimum value supported by the type. Both **Overflow** and **Underflow** are possible. |
36+
| `-` (subtraction, minus) | Throws an <xref:System.OverflowException> if the result exceeds the maximum value or goes below the minimum value supported by the type. Both **Overflow** and **Underflow** are possible. |
37+
| `*` (multiplication, times) | Throws an <xref:System.OverflowException> if the product exceeds the maximum value or goes below the minimum value supported by the type. Both **Overflow** and **Underflow** are possible. |
38+
39+
The checked operators are useful for ensuring that arithmetic overflows are caught and handled explicitly.
40+
41+
Here’s an example:
42+
43+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet3502.fs)]
44+
45+
### Choosing Between Checked and Unchecked Operators
46+
47+
**Checked Operators:** Ideal for scenarios where overflow errors must be detected and handled explicitly.
48+
49+
**Unchecked Operators:** By default, F# uses unchecked arithmetic for performance reasons. These operations may silently produce incorrect results when overflow or underflow occurs. Use with caution.
50+
2351
## Summary of Unary Arithmetic Operators
2452

2553
The following table summarizes the unary arithmetic operators that are available for integral and floating-point types.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
open Microsoft.FSharp.Core.Operators.Checked
2+
3+
let safeAddition () =
4+
try
5+
let result = 2147483647 + 1 // Attempt to add integers at their maximum boundary
6+
printfn "Result: %d" result
7+
with
8+
| :? System.OverflowException as ex ->
9+
printfn "Overflow occurred: %s" ex.Message
10+
11+
safeAddition()
12+
13+
// Output:
14+
// Overflow occurred: Arithmetic operation resulted in an overflow.

0 commit comments

Comments
 (0)