kudzu-forest / elm-wide-float / WideFloat

This module provides the definitions and functionalities of WideFloat type.

Types


type WideFloat

Opaque type that represents floating point number with 42 exponent bits and 52 significand bits.


type alias Content =
{ base2toThe1024exponent : Basics.Int
, significand : Basics.Float 
}

Record type that represents inner content of WideFloat.

Constants

zero : WideFloat

WideFloat representing 0.

zero == fromFloat 0 --> True

one : WideFloat

WideFloat representing 1.

one == fromFloat 1 --> True

Creation

create : Content -> WideFloat

Creates a WideFloat value from specified exponent and significand. The value create { base2toThe1024exponent = a, significand = b} corresponds to b * 2^(1024a).

create
    { base2toThe1024exponent = 0
    , significand = 2 ^ 513
    }
    == create
        { base2toThe1024exponent = 1
        , significand = 2 ^ -511
        } -->True

fromFloat : Basics.Float -> WideFloat

Creates a WideFloat value from specified float

fromFloat 1
    == one --> True

Checks

isZero : WideFloat -> Basics.Bool

Returns True if the parameter is recognized as zero in floating point precision.

fromFloat 1
    |> add
        ( create
            { base2toThe1024exponent = 100
            , significand = 1
            }
        )
    |> differenceFrom
        ( create
            { base2toThe1024exponent = 100
            , significand = 1
            }
        )
    |> isZero --> True


fromFloat 1
    |> add
        ( create
            { base2toThe1024exponent = 0
            , significand = 2^50
            }
        )
    |> differenceFrom
        ( create
            { base2toThe1024exponent = 0
            , significand = 2^50
            }
        )
    |> isZero --> False

isLargerThan : WideFloat -> WideFloat -> Basics.Bool

Returns True if the second parameter is larger than first one. Reads naturally when used with pipe operator(|>).

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 1000
        , significand = 1
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 999
        , significand = 1.999
        }

w1 |> isLargerThan w2 --> True

w2 |> isLargerThan w1 --> False

isSmallerThan : WideFloat -> WideFloat -> Basics.Bool

Returns True if the second parameter is smaller than first one. Reads naturally when used with pipe operator(|>).

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 1000
        , significand = 1
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 999
        , significand = 1.999
        }

w1 |> isSmallerThan w2 --> False

w2 |> isSmallerThan w1 --> True

isEqualTo : WideFloat -> WideFloat -> Basics.Bool

Returns True if the value of first parameter is the same as the second parameter.

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 1000
        , significand = 1
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 50
        , significand = 1
        }
        |> add
            (create
                { base2toThe1024exponent = 0
                , significand = 1
                }
            )

w1 |> isEqualTo w1 --> True

w1 |> isEqualTo w2 --> False

Calcuration

add : WideFloat -> WideFloat -> WideFloat

adds two WideFloats.

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 100
        , significand = 2^(-511)
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 99
        , significand = 2^511
        }

add w1 w2 --> create { base2toThe1024exponent = 100, significand = 1.25 * 2^(-511) }

differenceFrom : WideFloat -> WideFloat -> WideFloat

Subtracts WideFloat value given as the first parameter from the second. Reads naturally when used with pipe(|>).

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 1
        , significand = 2^(-511)
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 0
        , significand = 2^511
        }

w1 |> differenceFrom w2 --> create { base2toThe1024exponent = 1, significand = 1.5 * 2^(-512) }

multiply : WideFloat -> WideFloat -> WideFloat

Multiplies two WideFloats.

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 100
        , significand = 1.5
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 200
        , significand = 1.75
        }

multiply w1 w2 --> create { base2toThe1024exponent = 300, significand = 2.625 }

multiplyFloat : Basics.Float -> WideFloat -> WideFloat

multiplies a Float value on a WideFloat value.

w : WideFloat
w =
    create
        { base2toThe1024exponent = 0
        , significand = 2^(500)
        }

multiplyFloat (2^20) w --> create { base2toThe1024exponent = 1, significand = 256 * 2 ^(-512) }

dividedBy : WideFloat -> WideFloat -> WideFloat

Returns ratio between to WideFloats. Reads naturally when used with pipe(|>).

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 100
        , significand = 1
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 99
        , significand = 1.6
        }

w1 |> dividedBy w2
    |> differenceFrom ( create { base2toThe1024exponent = 0, significand = 1.25})
    |> isSmallerThan ( create { base2toThe1024exponent = -30, significand = 1})--> True

Output

proportionOf : WideFloat -> WideFloat -> Basics.Float

Returns proportion of the first parameter in the sum of first and second parameters.

w1 : WideFloat
w1 =
    create
        { base2toThe1024exponent = 100
        , significand = 2 ^ (-511)
        }

w2 : WideFloat
w2 =
    create
        { base2toThe1024exponent = 99
        , significand = 2^511
        }

p : Float
p =
    proportionOf w1 w2


p <= 0.8001 --> True

p >= 0.7999 --> True

getInternal : WideFloat -> Content

Returns a record representing the inner content of WideFloat. Inverse function of create (when significand is specified properly).

content : Content
content =
    { base2toThe1024exponent = 1000
    , significand = 1.234
    }


getInternal (create content)
   == content --> True

toString : Basics.Int -> WideFloat -> String

Returns a String value representing the base 10 expression of a WideFloat. You can specify how many digits is shown after decimal point as the first parameter. The result may be a little less precise than other calcurations implemented in this module.

create
    { base2toThe1024exponent = 1
    , significand = 1
    }
    |> toString 3 --> "1.798e308"

If you want some other formats, you can make your original toString function using getInternal.