Is there an arbitrary precision float in F#? Something like BigFloat? - f#

Is there a data type in F# that lets me calculate a float to an arbitrary/large number of decimal places? Something like the floating point equivalent of BigInt.
I would like do something like
myLargeFloat = 1.0/7.0
printfn "%12.500f" myLargeFloat // get the recurring cycle "0.142857142857142857...<500 digits long>"
I was using the BigInt to get the precision by multiplying the numerator with a bigint like so.
myLargeFloat = (bigint.Pow(10I,500)/7I)
Is there a better way of doing this?

BigRational in the F# Powerpack is defined like this:
[<CustomEquality; CustomComparison>]
[<StructuredFormatDisplay("{StructuredDisplayString}N")>]
type BigRational =
| Z of BigInteger
| Q of BigRationalLarge
Where a BigRationalLarge is defined as:
[<CustomEquality; CustomComparison>]
type BigRationalLarge =
| Q of BigInteger * BigInteger
To Print a BigInt with 1000 precision do something like this:
let factorial n = Seq.fold ( * ) 1I [1I .. n]
printf "Factorial of 1000 is %A" (factorial 1000I)
Taken from here.
Looking at the BigRationalLarge type here:
There are a number of ways to convert it to a different type to print:
static member ToDouble(n:BigRational) =
match n with
| Z z -> ToDoubleI z
| Q q -> BigRationalLarge.ToDouble q
static member ToBigInt(n:BigRational) =
match n with
| Z z -> z
| Q q -> BigRationalLarge.integer q
static member ToInt32(n:BigRational) =
match n with
| Z z -> ToInt32I(z)
| Q q -> ToInt32I(BigRationalLarge.integer q )
The conversion to a double looks like this:
static member ToDouble (Q(p,q)) =
ToDoubleI p / ToDoubleI q
The default way of printing it as a numerator and denominator combination:
override n.ToString() =
let (Q(p,q)) = n
if q.IsOne then p.ToString()
else p.ToString() + "/" + q.ToString()
None of that really helps us get more precision. There is no way to print it while specifying the number of decimal places to print.
So to answer your question:
You could make a function that prints the values you want, using the two BigInt parts of a BigRational or you could write an entirely new type to do this for you, but there isn't anything like that right now.

There is a BigRational type in the F# PowerPack. See also http://tomasp.net/blog/powerpack-numeric.aspx

If you truly require arbitrary-precision floats, you'll have to use #mydogisbox's method. If you just need something with better precision than float (i.e., System.Double) you can try using the decimal type; it's an alias for System.Decimal, which is the .NET implementation of 128-bit binary-coded decimal (BCD). It's much more precise than float but much slower too (like 10-20x).

Related

Is there default parameter in F#?

I wrote a program to convert file size from bytes to a human readable format in F#:
let rec sizeFmt num i =
let suffix="B"
let unit = ["";"Ki";"Mi";"Gi";"Ti";"Pi";"Ei";"Zi"]
match abs num with
| x when x < 1024.0 -> printfn "%3.1f %s%s" num unit.[i] suffix
| _ -> sizeFmt (num / 1024.0) (i+1)
let humanReadable n =
sizeFmt (float n) 0
Run example:
> humanReadable 33;;
33.0 B
val it : unit = ()
> humanReadable 323379443;;
308.4 MiB
val it : unit = ()
>
Question:
It would be nice if I can set i=0 as the default value in the
sizeFmt funciton. I checked the F# documentation, only found that
there's no default parameter. So I have to write a wrapper function
humanReadable. Is there a better way?
In order to handle both int and float type input like humanReadable 123;; and humanReadable 123433.33;;, I have to add a float n in the wrapper function. The obvious problem is: it is very easy to exceed the max int size which is 2,147,483,647. I guess there might be a better way, are there?
If sizeFmt is only used by humanReadable, it makes sense to make it an inner function. That avoids the 'parameter default' issue.
Also, marking the outer function inline causes it to accept any type of n that supports explicit conversion to float.
let inline humanReadable n =
let rec sizeFmt num i =
let suffix="B"
let unit = ["";"Ki";"Mi";"Gi";"Ti";"Pi";"Ei";"Zi"]
match abs num with
| x when x < 1024.0 -> printfn "%3.1f %s%s" num unit.[i] suffix
| _ -> sizeFmt (num / 1024.0) (i+1)
sizeFmt (float n) 0
humanReadable 123 //works
humanReadable 123433.33 //also works
One F# convention that may help is to put primary parameters at the end of the parameter list and secondary parameters first - the opposite of the convention in OO languages. This lets you pipe your primary argument to your function, e.g.
let rec sizeFmt i num =
...
123.0 |> sizeFmt 0
It also lets you easily create partial functions with optional parameters filled in:
let humanReadable = sizeFmt 0
In answer to 2, no there is no better way, unless you make sizeFmt generic and pass in the typed value of 1024.0 but this probably won't make it any simpler.
The only way to have an optional parameter in F# is to use a method instead of a function. To specify that a parameter is optional, put a ? before it. From the documentation here:
type DuplexType =
| Full
| Half
type Connection(?rate0 : int, ?duplex0 : DuplexType, ?parity0 : bool) =
let duplex = defaultArg duplex0 Full
let parity = defaultArg parity0 false
let mutable rate = match rate0 with
| Some rate1 -> rate1
| None -> match duplex with
| Full -> 9600
| Half -> 4800
do printfn "Baud Rate: %d Duplex: %A Parity: %b" rate duplex parity
let conn1 = Connection(duplex0 = Full)
let conn2 = Connection(duplex0 = Half)
let conn3 = Connection(300, Half, true)
Although I know that's not what is being asked about, did you know about F#'s Units of Measure feature?
[<Measure>] type B
[<Measure>] type kB
let bPerKB = 1024.M<B/kB>
let bytesToKiloBytes (bytes : decimal<B>) = bytes / bPerKB
let kiloBytesToBytes (kiloBytes : decimal<kB>) = kiloBytes * bPerKB
This gives you a type-safe way to distinguish bytes from kilobytes, and prevents you from accidentally assigning a kilobyte value to a function that expects bytes.
Here are some example conversions:
> 1024.M<B> |> bytesToKiloBytes;;
val it : decimal<kB> = 1M
> 1145.M<B> |> bytesToKiloBytes;;
val it : decimal<kB> = 1.1181640625M
> 1.M<kB> |> kiloBytesToBytes;;
val it : decimal<B> = 1024M
If you just need the function like above as a quick way to make a large byte value human-readable, this is surely overkill, but if you need to manage byte values on many scales, this may be appropriate.
The existing answers already explain that keeping a wrapper function is a good idea, since this lets the code be as much modular as possible. This would not be very obvious in a simple example, but in real-life projects it would be a great advantage to be able to extend sizeFmt at some moment by exposing more parameters ā€” consider that you may occasionally need "Hertz" instead of "Bytes" (and division by 1000 instead of 1024), or a sting format pattern (five decimal digits), or a localizable list of multipliers, and so on.
As for the second question, converting to float, the solution is very simple: make value a statically-resolved type:
let inline humanReadable (value:^T) =
sizeFmt (float value) 0
This will make humanReadable to have the following type constraint:
val inline humanReadable :
value: ^T -> unit when ^T : (static member op_Explicit : ^T -> float)
Usage:
humanReadable 42424242.42 // float
humanReadable 4242 // int
humanReadable 42424242424242424242I // Numerics.BigInteger
humanReadable (424242424242424242422424N / 5N) // BigRational
Using float in the inner function seems to be fine: any round-off errors will be eliminated by a series of divisions.

Is it possible to create a discriminated union via a unit of measure tag in F#?

Is it possible to create a discriminated union type via a unit of measurement tag in F#?
I want to write sth. like the following:
type DomainObject =
| Pixel of int
| ScaledPixel of int
| Centimeter of float
| Unset
let var1 = 10<px> // should equal: let var1 = Pixel(10)
let var2 = 0<us> // should equal: let var2 = Unset
let process sth =
match sth with
| Pixel(p) -> ...
| Centimeter(c) -> ...
// etc.
With NumericLiterals such things are possible. But then one can only use a small amount of Literals like Neil P. showed.
As I said in the comment, the simple answer is no.
In a way, you are trying to misuse one F# feature (units of measure) to emulate a feature that might exist in other languages (suffix operators), which is probably a bad thing to do in the first place, because (even if it was possible), the resulting code would be quite confusing.
If you simply want to reverse the order of the arguments so that the number comes before the unit name, you can use the piping operator and write:
let var1 = 10 |> Pixel
let var2 = Unset
This essentially gives you a way to write "suffix operators", but using standard F# idioms.
I don't think that this special combination is possible but you can go with smart constructors if you like:
module Domain =
[<Measure>] type px
[<Measure>] type spx
[<Measure>] type cm
// ...
type DomainObject =
| Pixel of float<px>
| ScaledPixel of float<spx>
| Centimeter of float<cm>
| Unset
let inline pixel f = Pixel <| float f * 1.0<px>
let inline scaledPixel f = ScaledPixel <| float f * 1.0<spx>
let unset = Unset
// ...
let var1 = pixel 10
let var2 = unset
let process sth =
match sth with
| Pixel(p) -> ...
| Centimeter(c) -> ...
// etc.
I think this is reasonable close - if you want you can make the constructors private and add active-patterns (to reenable pattern-matching) or accessors to fully encapsulate the implementation-details.
If you get fancy you can even add (+), (-), ...
PS: the inline is to get the functions working with all kinds of numeric values ;)
PPS: I played a bit and the problem is indeed (as mentioned in the link you gave - that you can only have a very limited set of "suffixes" - namely Q, R, Z, I, N, and G) - for example this kindof works:
module NumericLiteralQ =
open Domain
let inline FromZero() = Pixel 0.0<px>
let inline FromOne() = Pixel 1.0<px>
let inline FromString (s:string) =
System.Double.Parse s * 1.0<px> |> Pixel
let inline FromInt32 (n:int) =
1.0<px> * float n |> Pixel
let inline FromInt64 (n:int64) =
1.0<px> * float n |> Pixel
but I think it's very uggly to write
let p = 5Q
instead of
let p = pixel 5
or
let p = 5 |> pixel

Built-in compare on discriminated unions in f#

In answering this question, I discovered the following behaviour of compare on discriminated unions.
type T = A | B | C | D
compare A B (* val it : int = -1 *)
compare A C (* val it : int = -2 *)
compare A D (* val it : int = -3 *)
I was surprised by this.
Can I rely on compare measuring the "distance" between constructors like this?
The spec says (p. 154) about the generated compareTo:
If T is a union type, invoke Microsoft.FSharp.Core.Operators.compare first on the index of the union cases for the two values, and then on each corresponding field pair of x and y for the data carried by the union case. Return the first non-zero result.
From that, I'd expect compare on type T to always give one of -1,0,1 since that's how compare behaves on numeric types. (Right?)
The quote from the specification says that the generated comparison will first compare the tags (that is essentially the index of the constructors), but I'm not sure if this gives you any useful information - because if the union carries some value, you will not know whether the number is distance between the constructors, or the result of the comparison of the contained values. For example:
type Tricky() =
interface System.IComparable with
override x.CompareTo(b) = -2
type DU =
| A of Tricky
| B
| C
// Returns -2 because of the distance between constructors
compare (A (Tricky())) C
// Returns -2 because of the comparison on `Tricky` objects
compare (A (Tricky())) (A(Tricky()))
If you wanted to rely on the ability to get the distance between constructors, it might be safer to use enumerations:
type DU =
| A = 1
| B = 2
| C = 3
Then you can get the distance by converting the values to integers using (int DU.A) - (int DU.C).

F# Power issues which accepts both arguments to be bigints

I am currently experimenting with F#. The articles found on the internet are helpful, but as a C# programmer, I sometimes run into situations where I thought my solution would help, but it did not or just partially helped.
So my lack of knowledge of F# (and most likely, how the compiler works) is probably the reason why I am totally flabbergasted sometimes.
For example, I wrote a C# program to determine perfect numbers. It uses the known form of Euclids proof, that a perfect number can be formed from a Mersenne Prime 2pāˆ’1(2pāˆ’1) (where 2p-1 is a prime, and p is denoted as the power of).
Since the help of F# states that '**' can be used to calculate a power, but uses floating points, I tried to create a simple function with a bitshift operator (<<<) (note that I've edit this code for pointing out the need):
let PowBitShift (y:int32) = 1 <<< y;;
However, when running a test, and looking for performance improvements, I also tried a form which I remember from using Miranda (a functional programming language also), which uses recursion and a pattern matcher to calculate the power. The main benefit is that I can use the variable y as a 64-bit Integer, which is not possible with the standard bitshift operator.
let rec Pow (x : int64) (y : int64) =
match y with
| 0L -> 1L
| y -> x * Pow x (y - 1L);;
It turns out that this function is actually faster, but I cannot (yet) understand the reason why. Perhaps it is a less intellectual question, but I am still curious.
The seconds question then would be, that when calculating perfect numbers, you run into the fact that the int64 cannot display the big numbers crossing after finding the 9th perfectnumber (which is formed from the power of 31). I am trying to find out if you can use the BigInteger object (or bigint type) then, but here my knowledge of F# is blocking me a bit. Is it possible to create a powerfunction which accepts both arguments to be bigints?
I currently have this:
let rec PowBigInt (x : bigint) (y : bigint) =
match y with
| bigint.Zero -> 1I
| y -> x * Pow x (y - 1I);;
But it throws an error that bigint.Zero is not defined. So I am doing something wrong there as well. 0I is not accepted as a replacement, since it gives this error:
Non-primitive numeric literal constants cannot be used in pattern matches because they
can be mapped to multiple different types through the use of a NumericLiteral module.
Consider using replacing with a variable, and use 'when <variable> = <constant>' at the
end of the match clause.
But a pattern matcher cannot use a 'when' statement. Is there another solution to do this?
Thanks in advance, and please forgive my long post. I am only trying to express my 'challenges' as clear as I can.
I failed to understand why you need y to be an int64 or a bigint. According to this link, the biggest known Mersenne number is the one with p = 43112609, where p is indeed inside the range of int.
Having y as an integer, you can use the standard operator pown : ^T -> int -> ^T instead because:
let Pow (x : int64) y = pown x y
let PowBigInt (x: bigint) y = pown x y
Regarding your question of pattern matching bigint, the error message indicates quite clearly that you can use pattern matching via when guards:
let rec PowBigInt x y =
match y with
| _ when y = 0I -> 1I
| _ -> x * PowBigInt x (y - 1I)
I think the easiest way to define PowBigInt is to use if instead of pattern matching:
let rec PowBigInt (x : bigint) (y : bigint) =
if y = 0I then 1I
else x * PowBigInt x (y - 1I)
The problem is that bigint.Zero is a static property that returns the value, but patterns can only contain (constant) literals or F# active patterns. They can't directly contain property (or other) calls. However, you can write additional constraints in where clause if you still prefer match:
let rec PowBigInt (x : bigint) (y : bigint) =
match y with
| y when y = bigint.Zero -> 1I
| y -> x * PowBigInt x (y - 1I)
As a side-note, you can probably make the function more efficent using tail-recursion (the idea is that if a function makes recursive call as the last thing, then it can be compiled more efficiently):
let PowBigInt (x : bigint) (y : bigint) =
// Recursive helper function that stores the result calculated so far
// in 'acc' and recursively loops until 'y = 0I'
let rec PowBigIntHelper (y : bigint) (acc : bigint) =
if y = 0I then acc
else PowBigIntHelper (y - 1I) (x * acc)
// Start with the given value of 'y' and '1I' as the result so far
PowBigIntHelper y 1I
Regarding the PowBitShift function - I'm not sure why it is slower, but it definitely doesn't do what you need. Using bit shifting to implement power only works when the base is 2.
You don't need to create the Pow function.
The (**) operator has an overload for bigint -> int -> bigint.
Only the second parameter should be an integer, but I don't think that's a problem for your case.
Just try
bigint 10 ** 32 ;;
val it : System.Numerics.BigInteger =
100000000000000000000000000000000 {IsEven = true;
IsOne = false;
IsPowerOfTwo = false;
IsZero = false;
Sign = 1;}
Another option is to inline your function so it works with all numeric types (that support the required operators: (*), (-), get_One, and get_Zero).
let rec inline PowBigInt (x:^a) (y:^a) : ^a =
let zero = LanguagePrimitives.GenericZero
let one = LanguagePrimitives.GenericOne
if y = zero then one
else x * PowBigInt x (y - one)
let x = PowBigInt 10 32 //int
let y = PowBigInt 10I 32I //bigint
let z = PowBigInt 10.0 32.0 //float
I'd probably recommend making it tail-recursive, as Tomas suggested.

Reverse currying?

I'd like to compose functions in a certain way. Please consider these 2 functions in pseudocode (not F#)
F1 = x + y
F2 = F1 * 10 // note I did not specify arguments for F1, 'reverse curry' for lack of a better word
What I would like for F# to do is figure out that since
let F1 x y = x + y
//val F1 : int -> int -> int
the code let F2 = F1 * 10 would give me the same signature as F1: val F2 : int -> int -> int, and calling F2 2 3 would result in 50: (2 + 3) * 10. That would be rather clever...
What happens is quite different tho. The first line goes as expected:
let F1 x y = x + y
//val F1 : int -> int -> int
but when I add a second line let F2 = F1 * 10 it throws off F#. It complains that the type int does not match the type 'a -> 'b -> 'c and that F1 now requires member ( + ).
I could of course spell it out like this:
let F1(x, y) = x + y
let F2(x, y) = F1(x, y) * 10
But now I might as well have used C#, we're not that far away anymore. The tupled arguments break a lot of the elegance of F#. Also my real functions F1 and F2 have a lot more arguments than just 2, so this makes me go cross eyed, exactly what I wanted to dodge by using F#. Saying it like this would be much more natural:
let F1 x y = x + y
let F2 = F1 * 10
Is there any way I can (almost) do that?
For extra credits: what exactly goes on with these error messages? Why does the second line let F2 = F1 * 10 change the typing on the first?
Thanks in advance for your thoughts,
Gert-Jan
update
Two apporaches that (almost) do what's described.
One using a tuple. Second line looks a little quirky a first, works fine. Small drawback is I can't use currying now or I'll have to add even more quirky code.
let F1 (a, b) = a + b
let F2 = F1 >> (*) 10
F2(2, 3) // returns 50
Another approach is using a record. That is a little more straight forward and easier to get at first glance, but requieres more code and ceremony. Does remove some of the elegance of F#, looks more like C#.
type Arg (a, b) =
member this.A = a
member this.B = b
let F1 (a:Arg) = a.A + a.B
let F2 (a:Arg) = F1(a) * 10
F2 (Arg(2, 3)) // returns 50
There is no pattern for this in general. Using combinators (like curry and uncurry) as suggested by larsmans is one option, but I think the result is less readable and longer than the explicit version.
If you use this particular pattern often, you could define an operator for multiplying a function (with two parameters) by a scalar:
let ( ** ) f x = fun a b -> (f a b) * x
let F1 x y = x + y
let F2 = F1 ** 10
Unfortunately, you cannot add implementation of standard numeric operators (*, etc.) to existing types (such as 'a -> 'b -> int). However, this is quite frequent request (and it would be useful for other things). Alternatively, you could wrap the function into some object that provides overloaded numeric operators (and contains some Invoke method for running the function).
I think an appropriate name for this would be lifting - you're lifting the * operator (working on integers) to a version that works on functions returning integers. It is similar to lifting that is done in the C# compiler when you use * to work with nullable types.
To explain the error message - It complains about the expression F1 * 10:
error FS0001: The type 'int' does not match the type ''a -> 'b -> 'c'
I think it means that the compiler is trying to find an instantiation for the * operator. From the right-hand side, it figures out that this should be int, so it thinks that the left-hand side should also be int - but it is actually a function of two arguments - something like 'a -> 'b -> c'.
That would be rather clever...
So clever that it would beat the hell out of the type system. What you want is array programming as in APL.
Is there any way I can (almost) do that?
I don't speak F#, but in Haskell, you'd uncurry F1, then compose with *10, then curry:
f2 = curry ((*10) . uncurry f1)
Which in an ML dialect such as F# becomes something like:
let curry f x y = f (x,y)
let uncurry f (x,y) = f x y
let mult x y = x * y
let F1 x y = x + y
let F2 = curry (uncurry F1 >> mult 10)
(I wasn't sure if curry and uncurry are in the F# standard library, so I defined them. There may also be a prettier way of doing partial application of * without defining mult.)
BTW, using point-free (or rather pointless in this case) approach one could define these functions in the following way:
let F1 = (+)
let F2 = (<<)((*)10) << F1

Resources