平方根

sqrt 指令,是 square root(平方根)的縮寫,用於獲取數字的平方根。

試一試

(module
  (import "console" "log" (func $log (param f32)))
  (func $main

    f32.const 2 ;; load a number onto the stack
    f32.sqrt ;; calculate the square root
    call $log ;; log the result

  )
  (start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

語法

wat
;; load a number onto the stack
f32.const 289

;; get the square root of 289
f32.sqrt

;; the top item on the stack will now be 17
指令 二進位制操作碼
f32.sqrt 0x91
f64.sqrt 0x9f