0 people like it.

Convert a Float to a Mixed Number

Function to Convert a float to a mixed number. My thanks to John Kennedy of Santa Monica College for his "Algorithm to Convert a Decimal to a Fraction" (see link).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let ToMixedNumber(x : float) =
    let wholePart = int x     // whole part of x
    let decimalPt = x % 1.0   // decimal part of x
    let rec cF(Z : float, i : int, Dm : float, Do : float) =
        match Z % 1.0 > 1e-6, i < 1 with
        //  First case terminates after 14 iterations
        | _    , true  -> (wholePart, (int64 (System.Math.Round(decimalPt * Do)), int64 Do))
        //  Second case executes next cF (continuing fraction)
        | true , false -> cF(1.0/(Z % 1.0), i - 1 , Do, Do * System.Math.Round(1.0/(Z % 1.0)-0.5) + Dm )
        //  Final case terminates if the remainder of Z > 10^-6
        | false, _  -> (wholePart, (int64 (System.Math.Round(decimalPt * Do)), int64 Do))
    decimalPt
    |> fun x -> cF(x, 14, 0.0, 1.0)    
//  Test using pi as input
let dummyrecA = ToMixedNumber System.Math.PI
//  Recompose pi as a decimal form. 
//  NOTE Actual Value of pi = 3.1415926535897932384626433833M
let myPiA = decimal (fst dummyrecA) + decimal ( fst (snd dummyrecA)) / decimal ( snd (snd dummyrecA))
val ToMixedNumber : x:float -> int * (int64 * int64)

Full name: Script.ToMixedNumber
val x : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val wholePart : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val decimalPt : float
val cF : (float * int * float * float -> int * (int64 * int64))
val Z : float
val i : int
val Dm : float
val Do : float
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int64

--------------------
type int64 = System.Int64

Full name: Microsoft.FSharp.Core.int64

--------------------
type int64<'Measure> = int64

Full name: Microsoft.FSharp.Core.int64<_>
namespace System
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
System.Math.Round(d: decimal) : decimal
System.Math.Round(a: float) : float
System.Math.Round(d: decimal, mode: System.MidpointRounding) : decimal
System.Math.Round(d: decimal, decimals: int) : decimal
System.Math.Round(value: float, mode: System.MidpointRounding) : float
System.Math.Round(value: float, digits: int) : float
System.Math.Round(d: decimal, decimals: int, mode: System.MidpointRounding) : decimal
System.Math.Round(value: float, digits: int, mode: System.MidpointRounding) : float
val dummyrecA : int * (int64 * int64)

Full name: Script.dummyrecA
field System.Math.PI = 3.14159265359
val myPiA : decimal

Full name: Script.myPiA
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.decimal

--------------------
type decimal = System.Decimal

Full name: Microsoft.FSharp.Core.decimal

--------------------
type decimal<'Measure> = decimal

Full name: Microsoft.FSharp.Core.decimal<_>
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd

More information

Link:http://fssnip.net/kV
Posted:5 years ago
Author:visProcessEngg
Tags: mixed number , fraction