trig

    Elementary functions as a value: MoonBit's @math, or a wasm host's Math, for code that must compute as a JavaScript library does

    math
    trigonometry
    wasm
    host
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 hours ago
    Downloads
    4

    No dependencies

    #trig

    The elementary functions a computation takes, as a value: Trig holds sin, cos, tan, asin, acos, atan, atan2, exp, ln, pow, cbrt and atanh. Trig::moonbit() gives MoonBit's @math; a wasm build can pass its host's Math instead.

    Two reasons to take the functions as an argument. A port of a JavaScript library only gives the library's results bit for bit if it computes with the same Math (the last digit of sin differs between implementations). And a wasm-gc module that imports the host's functions does not carry @math. t-ujiie-g/proj and t-ujiie-g/geodesic take a Trig for both.

    Part of mbt-map, a WebGIS library, published on its own.

    moon add t-ujiie-g/trig

    /// The great-circle distance in radians, with the functions the caller chose.
    fn arc(m : @trig.Trig, lat1 : Double, lat2 : Double, dlon : Double) -> Double {
    m.acos(
    m.sin(lat1) * m.sin(lat2) + m.cos(lat1) * m.cos(lat2) * m.cos(dlon),
    )
    }

    ///|
    /// MoonBit's functions, with one replaced.
    fn with_pow(pow : (Double, Double) -> Double) -> @trig.Trig {
    { ..@trig.Trig::moonbit(), pow, }
    }

    #What it gives

    • Trig: a struct of twelve function fields, public, so a host's functions go in with a record ({ sin: host_sin, … }) or replace some of MoonBit's ({ ..Trig::moonbit(), pow }).
    • Trig::moonbit(): the functions of moonbitlang/core/math.
    • A method for each field (m.sin(x), m.atan2(y, x), …), calling it.

    For a wasm host, declare the imports (fn host_sin(x : Double) -> Double = "math" "sin") and build the Trig once from them; the module then links no @math unless something else in it does.

    #License

    Apache-2.0 (see NOTICE). No third-party code.

    Trig

    pub(all) struct Trig {
    sin : (Double) -> Double
    cos : (Double) -> Double
    tan : (Double) -> Double
    asin : (Double) -> Double
    acos : (Double) -> Double
    atan : (Double) -> Double
    atan2 : (Double, Double) -> Double
    exp : (Double) -> Double
    ln : (Double) -> Double
    pow : (Double, Double) -> Double
    cbrt : (Double) -> Double
    atanh : (Double) -> Double
    }

    Elementary functions: MoonBit's (Trig::moonbit), or a wasm host's Math, which computes as the JavaScript libraries a port follows and keeps @math out of the wasm.

    Trig::acos

    fn Trig::acos(self : Trig, x : Double) -> Double

    The arccosine of x, in radians, by this Trig's implementation.

    Trig::asin

    fn Trig::asin(self : Trig, x : Double) -> Double

    The arcsine of x, in radians, by this Trig's implementation.

    Trig::atan

    fn Trig::atan(self : Trig, x : Double) -> Double

    The arctangent of x, in radians, by this Trig's implementation.

    Trig::atan2

    fn Trig::atan2(self : Trig, y : Double, x : Double) -> Double

    The angle of the point (x, y) in radians, by this Trig's implementation.

    Trig::atanh

    fn Trig::atanh(self : Trig, x : Double) -> Double

    The inverse hyperbolic tangent of x, by this Trig's implementation.

    Trig::cbrt

    fn Trig::cbrt(self : Trig, x : Double) -> Double

    The cube root of x, by this Trig's implementation.

    Trig::cos

    fn Trig::cos(self : Trig, x : Double) -> Double

    The cosine of x (radians), by this Trig's implementation.

    Trig::exp

    fn Trig::exp(self : Trig, x : Double) -> Double

    e to the x, by this Trig's implementation.

    Trig::ln

    fn Trig::ln(self : Trig, x : Double) -> Double

    The natural logarithm of x, by this Trig's implementation.

    Trig::moonbit

    fn Trig::moonbit() -> Trig

    The functions of @math.

    Trig::pow

    fn Trig::pow(self : Trig, x : Double, y : Double) -> Double

    x to the y, by this Trig's implementation.

    Trig::sin

    fn Trig::sin(self : Trig, x : Double) -> Double

    The sine of x (radians), by this Trig's implementation.

    Trig::tan

    fn Trig::tan(self : Trig, x : Double) -> Double

    The tangent of x (radians), by this Trig's implementation.

    Source Files