views:

788

answers:

3
+1  Q: 

f# int64 to int

How can I convert an int64 to an int32 type in F# without Microsoft.FSharp.Compatibility.Int32.of_int64

I'm doing this because interactive doesn't seem to work when I try

open Microsoft.FSharp.Compatibility

With FSharp.PowerPack added as a reference it says:error FS0039: The namespace 'Compatibility' is not defined.

Edit: does anyone have an answer to the question? The suggestions about the int types are useful and informative, but I'm having the same issue opening the powerpack namespace in F# interactive.

+6  A: 

F# 1.9.6 has a type conversion function so you can do this:

let num = 1000
let num64 = int64(num)
Thedric Walker
+4  A: 

Notice that in this type of conversion, when you reduce the size of a value, the most significant bytes are thrown away, so your data might be truncated:

> let bignum =  4294967297L;;
val bignum : int64

> let myint = int32(bignum);;
val myint : int32

> myint;;
val it : int32 = 1
CMS
A: 

Note that the functions for converting to each integer type have the same names as the types themselves, and are defined in the library spec (see below). (With the release of the CTP (1.9.6.2), a lot of the library and the namespaces changed a bit compared to previous releases, but it will probably be more 'stable' moving forward.)

http://research.microsoft.com/fsharp/manual/FSharp.Core/Microsoft.FSharp.Core.Operators.html

Brian