Estimate Pi

Example 1


(module
  (import "js" "rand" (func $rand (result f32)))
  (func $calcPi (export "calcPi") (param $count f32) (result f32)
    (local $x f32)
    (local $y f32)
    (local $inside f32)
    (local $i f32)
    (set_local $inside (f32.const 0))
    (set_local $i (f32.const 0))

    (block $b0
      (loop $l0
        
        (set_local $x (f32.sub (f32.mul (call $rand) (f32.const 2)) (f32.const 1)))
        (set_local $y (f32.sub (f32.mul (call $rand) (f32.const 2)) (f32.const 1)))

        (f32.add (f32.mul (get_local $x) (get_local $x)) (f32.mul (get_local $y) (get_local $y)))
        (f32.const 1)
        f32.lt
        if $i0
          (set_local $inside (f32.add (f32.const 1) (get_local $inside)))
        end

        (set_local $i  (f32.add (f32.const 1) (get_local $i)))
        
        (get_local $count)
        (get_local $i)
        f32.eq
        br_if 1

        br 0
      )
    )

    (f32.mul (f32.const 4) (f32.div (get_local $inside) (get_local $i)))
  )
)

Example 2

(module
  (import "js" "pow" (func $pow (param f32) (param f32)(result f32)))
  
  (func $fact (export "fact") (param $in f32) (result f32)
    (local $out f32)
    f32.const 1
    set_local $out

    get_local $in
    (f32.const 0)
    f32.eq
    if $i0
      f32.const 1
      return
    end
    
    get_local $in
    (f32.const 1)
    f32.eq
    if $i0
      f32.const 1
      return
    end

    get_local $in
    (f32.const 2)
    f32.eq
    if $i0
      f32.const 2
      return
    end

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

        (f32.sub 
          (get_local $in)
          (f32.const 1)
        )
        tee_local $in
        (f32.const 2)
        f32.eq
        br_if 1

        br 0
      )
    )
    get_local $out
  )
  (func $calcPi (export "calcPi") (param $n f32) (result f32)
    (local $out f32)
    (local $k f32)
    (local $x f32)
    (set_local $out (f32.const 0))
    (set_local $k (f32.const 0))
    (set_local $x (f32.const 0))
    (block $b0
      (loop $l0
      
        (set_local $x 
          (f32.mul 
            (f32.mul 
                  (call $pow (f32.const -1) (get_local $k))
                  (call $fact (f32.mul (f32.const 6) (get_local $k)))
            )
            (f32.add 
              (f32.mul

                (f32.const 545140134)
                (get_local $k)
              )
              (f32.const 13591409)
            )
          )
        )

        (set_local $x (f32.div (get_local $x) 
          (f32.mul
            (f32.mul
              (call $fact (f32.mul (f32.const 3) (get_local $k)))
              (call $pow (call $fact (get_local $k)) (f32.const 3))
            )
            (call $pow (f32.const 640320) 
              (f32.add
                (f32.mul (f32.const 3) (get_local $k))
                (f32.div (f32.const 3) (f32.const 2))
              )
            )
          )
        ))
        
        (set_local $out (f32.add (get_local $out) (get_local $x)))

        (set_local $k  (f32.add (f32.const 1) (get_local $k)))
        get_local $k
        get_local $n
        f32.gt
        br_if 1

        br 0
      )
    )

    (set_local $out  (f32.mul (f32.const 12) (get_local $out)))
    (set_local $out  (f32.div (f32.const 1) (get_local $out)))
    (get_local $out)
  )
)

Last updated