What is going on with those multiplications


#1

Can someone explain, what is going on

> let y = 65536 * 65536 ;;
val y : int = 0

> let y = 655361 * 655361 ;;
val y : int = 1310721

> let y = 65536 * 65536 ;;  
val y : int = 0

> let y = 655361 * 65536 ;;
val y : int = 65536

> let y = 655362 * 65536 ;;
val y : int = 131072

> let y = 2 * 65536 ;;     
val y : int = 131072

I checked the docs and an int should support values from -2,147,483,648 to 2,147,483,647.

I am running version 4.1 on linux

more weirdness

> 65537 * 65536;;
val it : int = 65536

> 65539 * 65536;;
val it : int = 196608

#2

* is unchecked operator, so you get incorrect results if the value exceeds the maximum supported by the type

Summary of Binary Arithmetic Operators

65 536 * 65 536 = 4 294 967 296 > 2 147 483 647

But you can use Microsoft.FSharp.Core.Operators.Checked to avoid this behaviour


#3

Your results are larger than 2 147 483 647 and causes an overflow and your results are the least significant 32 bits of the multiplication.

Start your code with open Checked to get an exception each time you overflow.

( FoggyFinder won that one )