2 people like it.

Mixed number

A function to calculate 'mixed numbers' - eg. 1 1/8. (With fix to reduce the fractional part to its lowest terms)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
open System

/// Euclid's algorithm for hcf
let rec highestCommonFactor (x : int) (y : int) =
    let larger, smaller = Math.Max(x, y), Math.Min(x, y)
    let q, r = larger / smaller, larger % smaller
    if r = 0 then
        smaller
    else
        highestCommonFactor y r

/// Find the lowest terms of a fraction - eg. 2/4 --> 1/1
let lowestTerms n d = 
    if n <= 0 || d <= 0 then
        1, 1
    else
        let hcf = highestCommonFactor n d 
        n / hcf, d / hcf

/// Convert a floating point number to the nearest mixed number using the specified numerator and denominator - eg. 1,3,1.666 --> 1 2/3
let toMixedNumber numerator denominator x =
    if denominator <= 0 then
        raise (System.ArgumentException("Denominator must be greater than 0"))
    let a = x / (numerator|>float) * (denominator|>float)
    let b = a |> round |> int
    let c = b / denominator
    let d = b % denominator

    let d', denominator' = lowestTerms d denominator

    match c, d with
    | 0, 0 -> sprintf "0"
    | 0, d -> sprintf "%i/%i" d' denominator'
    | c, 0 -> sprintf "%i" c
    | c, d -> sprintf "%i %i/%i" c d' denominator'

//  ["0"; "1/3"; "2/3"; "1"; "1 1/3"; "1 2/3"; "2"; "2 1/3"; "2 2/3"; "3";
//   "3 1/3"; "3 2/3"; "4"; "4 1/3"; "4 2/3"; "5"; "5 1/3"; "5 2/3"; "6";
//   "6 1/3"; "6 2/3"; "7"; "7 1/3"; "7 2/3"; "8"; "8 1/3"; "8 2/3"; "9";
[0. .. (1./3.) .. 32.] |> List.map (fun x -> toMixedNumber 1 3 x)

//  ["0"; "1/6"; "1/3"; "1/2"; "2/3"; "5/6"; "1"; "1 1/6"; "1 1/3"; "1 1/2";
//   "1 2/3"; "1 5/6"; "2"; "2 1/6"; "2 1/3"; "2 1/2"; "2 2/3"; "2 5/6"; "3";
//   "3 1/6"; "3 1/3"; "3 1/2"; "3 2/3"; "3 5/6"; "4"; "4 1/6"; "4 1/3"; "4 1/2";
[0. .. (1./6.) .. 32.] |> List.map (fun x -> toMixedNumber 1 6 x)

//  ["0"; "1/10"; "1/5"; "3/10"; "2/5"; "1/2"; "3/5"; "7/10"; "4/5"; "9/10"; "1";
//   "1 1/10"; "1 1/5"; "1 3/10"; "1 2/5"; "1 1/2"; "1 3/5"; "1 7/10"; "1 4/5";
//   "1 9/10"; "2"; "2 1/10"; "2 1/5"; "2 3/10"; "2 2/5"; "2 1/2"; "2 3/5";
[0. .. (1./10.) .. 32.] |> List.map (fun x -> toMixedNumber 1 10 x)
namespace System
val highestCommonFactor : x:int -> y:int -> int

Full name: Script.highestCommonFactor


 Euclid's algorithm for hcf
val x : 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 y : int
val larger : int
val smaller : int
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
Math.Max(val1: decimal, val2: decimal) : decimal
   (+0 other overloads)
Math.Max(val1: float, val2: float) : float
   (+0 other overloads)
Math.Max(val1: float32, val2: float32) : float32
   (+0 other overloads)
Math.Max(val1: uint64, val2: uint64) : uint64
   (+0 other overloads)
Math.Max(val1: int64, val2: int64) : int64
   (+0 other overloads)
Math.Max(val1: uint32, val2: uint32) : uint32
   (+0 other overloads)
Math.Max(val1: int, val2: int) : int
   (+0 other overloads)
Math.Max(val1: uint16, val2: uint16) : uint16
   (+0 other overloads)
Math.Max(val1: int16, val2: int16) : int16
   (+0 other overloads)
Math.Max(val1: byte, val2: byte) : byte
   (+0 other overloads)
Math.Min(val1: decimal, val2: decimal) : decimal
   (+0 other overloads)
Math.Min(val1: float, val2: float) : float
   (+0 other overloads)
Math.Min(val1: float32, val2: float32) : float32
   (+0 other overloads)
Math.Min(val1: uint64, val2: uint64) : uint64
   (+0 other overloads)
Math.Min(val1: int64, val2: int64) : int64
   (+0 other overloads)
Math.Min(val1: uint32, val2: uint32) : uint32
   (+0 other overloads)
Math.Min(val1: int, val2: int) : int
   (+0 other overloads)
Math.Min(val1: uint16, val2: uint16) : uint16
   (+0 other overloads)
Math.Min(val1: int16, val2: int16) : int16
   (+0 other overloads)
Math.Min(val1: byte, val2: byte) : byte
   (+0 other overloads)
val q : int
val r : int
val lowestTerms : n:int -> d:int -> int * int

Full name: Script.lowestTerms


 Find the lowest terms of a fraction - eg. 2/4 --> 1/1
val n : int
val d : int
val hcf : int
val toMixedNumber : numerator:int -> denominator:int -> x:float -> string

Full name: Script.toMixedNumber


 Convert a floating point number to the nearest mixed number using the specified numerator and denominator - eg. 1,3,1.666 --> 1 2/3
val numerator : int
val denominator : int
val x : float
val raise : exn:Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type ArgumentException =
  inherit SystemException
  new : unit -> ArgumentException + 4 overloads
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Message : string
  member ParamName : string

Full name: System.ArgumentException

--------------------
ArgumentException() : unit
ArgumentException(message: string) : unit
ArgumentException(message: string, innerException: exn) : unit
ArgumentException(message: string, paramName: string) : unit
ArgumentException(message: string, paramName: string, innerException: exn) : unit
val a : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
val b : int
val round : value:'T -> 'T (requires member Round)

Full name: Microsoft.FSharp.Core.Operators.round
val c : int
val d' : int
val denominator' : int
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map

More information

Link:http://fssnip.net/9t
Posted:13 years ago
Author:Kit Eason
Tags: fractions , mixed numbers