Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I explicitly use unchecked arithmetic operators in F#

Tags:

f#

If I use the --checked+ option when compiling F# code, how do I use unchecked arithmetic for a specific operation.

Going the other way is easy, just use the FSharp.Core.Operators.Checked module; but I can't find the appropriate module to get the Unchecked versions of the operators.

The FSharp.Core.Operators.Unchecked module exists, but does not contain any of the basic arithmetic operations such as +, *, etc.

For example:

let a = FSharp.Core.uint32.MaxValue
let b = a+1u //Alter this to get it to work?
//b should be 0,
//rather than OverflowException being thrown in the previous line
b
like image 896
Mankarse Avatar asked Dec 16 '25 20:12

Mankarse


1 Answers

The default unchecked operators are defined in Microsoft.FSharp.Core.Operators. If you need this in just a few places, you can refer to the operator explicitly via the full module name:

let a = FSharp.Core.uint32.MaxValue
let b = Microsoft.FSharp.Core.Operators.(+) a 1u
like image 87
Tomas Petricek Avatar answered Dec 19 '25 14:12

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!