絕對值

abs 指令,是絕對值的縮寫,用於獲取數字的絕對值。也就是說,如果 x 是正數,則返回 x;如果 x 是負數,則返回 x 的負數(即正數)。

試一試

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

    f32.const -10 ;; load a number onto the stack
    f32.abs ;; calculate the absolute value
    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 -2

;; absolute
f32.abs

;; the top item on the stack will now be 2
指令 二進位制操作碼
f32.abs 0x8b
f64.abs 0x99