Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a substitute for Pow in BigInteger in F#?

Tags:

pow

f#

biginteger

I was using the Pow function of the BigInteger class in F# when my compiler told me :

This construct is deprecated. This member has been removed to ensure that this type is binary compatible with the .NET 4.0 type System.Numerics.BigInteger

Fair enough I guess, but I didn't found a replacement immediately.

Is there one? Should we only use our own Pow functions? And (how) will it be replaced in NET4.0?

like image 497
Peter Avatar asked Dec 02 '09 14:12

Peter


2 Answers

You can use the pown function

let result = pown 42I 42

pown works on any type that 'understands' multiplication and 'one'.

like image 102
cfern Avatar answered Nov 15 '22 11:11

cfern


If you look at F# from the perspective of being based on OCaml, then the OCaml Num module has power_num. Since OCaml type num are arbitrary-precision rational numbers they can handle any size number, e.g. they are not limited by the CPU register because they can do the math symbolically. Also since num is defined as

type num =
| Int of int
| Big_int of Big_int.big_int
| Ratio of Ratio.ratio

they can handle very small numbers with out loss of precision because of the Ratio type.

Since F# does not have the num type, Jack created the FSharp.Compatibility.OCaml module which has num.fs and is available via NuGet.

So you can get all the precision you want using this, and the num functions can handle negative exponents.

like image 35
Guy Coder Avatar answered Nov 15 '22 11:11

Guy Coder