Decimal F # Script Conversion and Compiled F #

The following code snippet works in F # Interactive:

> printfn "%A" (decimal 1I) 1M 

However, an error message appears in the compiled F # program:

 The type 'Numerics.BigInteger' does not support a conversion to the type 'decimal' 

What happened there? Is this because a different set of links (and link versions) are used between F # versions? or internal decimal representations are different in compiled and interpreted modes.

+8
decimal type-conversion f #
source share
3 answers

This is probably because your compiled F # program is for the .NET Framework 2.0 / F # 2.0. F # interactive uses the .NET Framework 4.0 / F # 4.0.

2.0 Framework uses BigInteger in FSharp.Core. The 4.0 Framework uses System.Numerics.BigInteger . FSharp.Core does not convert to decimal.

Change your project to target .NET 4.0 and add a link to System.Numerics and everything should match.

+9
source share

You are right that there is some inconsistency in whether it is possible to convert BigInteger using the decimal function or not. It seems to depend on the version of .NET you are compiling for. If you use the F # (or F # interactive) compiler from Visual Studio 2010, then .NET 4.0 is used by default. For this purpose compilation works fine:

 C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs Microsoft (R) F# 3.0 Compiler build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. 

You can change the target structure by explicitly specifying .NET 2.0 version of mscorlib.dll and FSharp.Core.dll . The compiler then reports the error you described:

 C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs --noframework -r:C:\Program Files (x86)\FSharp-2.0.0.0\bin\FSharp.Core.dll -r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll Microsoft (R) F# 3.0 Compiler build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. test.fs(1,23): error FS0001: The type 'System.Numerics.BigInteger' does not support a conversion to the type 'decimal' 

If you get an error compiling the project, your project is probably configured to compile for .NET 2.0.

+2
source share

Same result

 Microsoft(R) F# 2.0 Interactive ビルド 4.0.40219.1 Copyright (c) Microsoft Corporation. All Rights Reserved. > printfn "%A" (decimal 1I);; 1M val it : unit = () >fsc test.fs Microsoft(R) F# 2.0 Compiler ビルド 4.0.40219.1 Copyright (c) Microsoft Corporation. All Rights Reserved. >test 1M 
0
source share

All Articles