5 people like it.

Square root for integral types

Square root for integral types

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
module NumericLiteralG =
    let inline FromZero () = LanguagePrimitives.GenericZero
    let inline FromOne () = LanguagePrimitives.GenericOne

let inline isqrt num =
    if num > 0G then
        let two = 1G + 1G
        let inline reduce n = (num / n + n) / two
        let rec impl n = function
            | n' when n' <= n -> n'
            | _               -> impl (reduce n) n
        let n = num / two + 1G
        impl (reduce n) n
    elif num = 0G then num
    else invalidArg "num" "negative numbers are not supported"
val FromZero : unit -> 'a (requires member get_Zero)

Full name: Script.NumericLiteralG.FromZero
module LanguagePrimitives

from Microsoft.FSharp.Core
val GenericZero<'T (requires member get_Zero)> : 'T (requires member get_Zero)

Full name: Microsoft.FSharp.Core.LanguagePrimitives.GenericZero
val FromOne : unit -> 'a (requires member get_One)

Full name: Script.NumericLiteralG.FromOne
val GenericOne<'T (requires member get_One)> : 'T (requires member get_One)

Full name: Microsoft.FSharp.Core.LanguagePrimitives.GenericOne
val isqrt : num:'a -> 'a (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)

Full name: Script.isqrt
val num : 'a (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)
val two : 'd (requires member ( / ) and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_Zero and member ( / ) and comparison and member ( + ) and member get_One)
val reduce : ('a -> 'a) (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)
val n : 'a (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)
val impl : ('a -> 'a -> 'a) (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)
val n' : 'a (requires member ( + ) and member get_Zero and member ( / ) and member ( / ) and comparison and member ( / ) and member get_One and member ( + ) and member get_One and member ( + ) and member get_One)
val invalidArg : argumentName:string -> message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidArg
Raw view Test code New version

More information

Link:http://fssnip.net/dR
Posted:12 years ago
Author:ildjarn
Tags: math , square root , sqrt