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: docs/fsharp/language-reference/symbol-and-operator-reference/arithmetic-operators.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,11 @@ This topic describes arithmetic operators that are available in F#.
9
9
10
10
## Summary of Binary Arithmetic Operators
11
11
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.
13
17
14
18
|Binary operator|Notes|
15
19
|---------------|-----|
@@ -20,6 +24,30 @@ The following table summarizes the binary arithmetic operators that are availabl
20
24
|`%` (remainder, rem)|Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand.|
21
25
|`**` (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.|
22
26
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.
|`+` (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.
### 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
+
23
51
## Summary of Unary Arithmetic Operators
24
52
25
53
The following table summarizes the unary arithmetic operators that are available for integral and floating-point types.
0 commit comments