kudzu-forest/elm-wide-float/1.0.0

NOTE: the latest version is 2.0.2

WideFloat

The definitions and functionalities of WideFloat type (which is explained in README.md).

Types

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

type alias Content =
{ base2exponent : Int
, significand : Float
}

Record type that represents inner content of WideFloat.

Creation

create : Content -> WideFloat

Creates a WideFloat value from specified exponent and significand. If the absolute value of given significand is greater 2 or less than 1, n is added to the exponent and 2^(-n) is multiplied to the significand where n denote floor (logBase 2 (abs significand))

create { base2exponent = 0, significand = 10 }
    == create { base2exponent = 3, significand = 1.25 }
    --> True
fromFloat : Float -> WideFloat

Creates a WideFloat value from specified float

fromFloat (1024.0 * 1024.0 * 1024.0 * 1024.0)
    == create { base2exponent = 40, significand = 1 }
    --> True

Checks

isZero : WideFloat -> Bool

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

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




fromFloat 1
    |> add
        ( create
            { base2exponent = 50
            , significand = 1
            }
        )
    |> differenceFrom
        ( create
            { base2exponent = 50
            , significand = 1
            }
        )
    |> isZero --> False

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

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


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


w1 |> isLargerThan w2 --> True


w2 |> isLargerThan w1 --> False

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

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


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


w1 |> isSmallerThan w2 --> False


w2 |> isSmallerThan w1 --> True

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

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


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


w1 |> isEqualTo w1 --> True


w1 |> isEqualTo w2 --> False

Calcuration

adds two WideFloats.

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


w2 : WideFloat
w2 =
    create
        { base2exponent = 99
        , significand = 1
        }


add w1 w2 --> create { base2exponent = 100, significand = 1.5 }

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

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


w2 : WideFloat
w2 =
    create
        { base2exponent = 99
        , significand = 1.75
        }


w1 |> differenceFrom w2 --> create { base2exponent = 97, significand = 1.0 }

Multiplies two WideFloats.

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


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


multiply w1 w2 --> create { base2exponent = 301, significand = 1.3125 }

multiplies a Float value on a WideFloat value.

w : WideFloat
w =
    create
        { base2exponent = 100
        , significand = 1
        }


multiplyFloat 3 w --> create { base2exponent = 101, significand = 1.5 }

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

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


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


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

Output

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

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


w2 : WideFloat
w2 =
    create
        { base2exponent = 99
        , significand = 1
        }


proportionOf w1 w2 == 2.0 / 3.0 --> True
getInternal : WideFloat -> Content

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

content : { base2exponent : Int, significand : Float}
content =
    { base2exponent = 1000, significand = 1.234 }




getInternal (create content)
   == content --> True
toString : 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
    { base2exponent = 2
    , significand = 1
    }
    |> toString 3 --> "4.000e0"

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