Wasm Reference
  • Welcome
  • Nodejs WAT Compiler
  • Types
    • Functions
    • Globals
    • Linear Memory
    • Tables
    • Numbers
    • Text
  • Structures
    • IF
    • LOOP
  • Algorithms
    • Hello World
    • Estimate Pi
  • Maths Helper Algorithms
    • Pseudorandom Number Generator
    • Factorial
    • Dist
    • Map
    • Fract
    • RemF or ModF
    • Round
    • Clamp
    • Mix
    • Pow
    • Approx Sin
    • Approx Cos
    • Approx Tan
    • ToRad and ToDeg
Powered by GitBook
On this page

Was this helpful?

  1. Maths Helper Algorithms

Approx Cos

Returns cos

(module
  (func $pow (export "pow") (param $x f32) (param $y f32) (result f32)
    (local $out f32)
    (local $index f32)
    f32.const 1
    set_local $out
    f32.const 1
    set_local $index

    (block $b0
      (loop $l0
        (f32.mul 
          (get_local $out)
          (get_local $x)
        )
        set_local $out

        (f32.add 
          (get_local $index)
          (f32.const 1)
        )
        tee_local $index
        get_local $y
        f32.gt
        br_if 1

        br 0
      )
    )

    get_local $out
  )
  (func $cos (export "cos") (param $x f32) (result f32)
    (f32.sub
      (f32.add
        (f32.sub
          (f32.add
            (f32.sub
              (f32.const 1)
              (f32.div 
                (call $pow (get_local $x) (f32.const 2))
                (f32.const 2)
              )
            )
            (f32.div 
              (call $pow (get_local $x) (f32.const 4))
              (f32.const 24)
            )
          )
          (f32.div 
              (call $pow (get_local $x) (f32.const 6))
            (f32.const 720)
          )
        )
        (f32.div 
          (call $pow (get_local $x) (f32.const 8))
          (f32.const 40320)
        )
      )
      (f32.div 
        (call $pow (get_local $x) (f32.const 10))
        (f32.const 3628800)
      )
    )
  )
)
PreviousApprox SinNextApprox Tan

Last updated 5 years ago

Was this helpful?