向下取整

floor 指令用于获取一个数字向下取整到最接近的整数的值。

当用于负数时,floortrunc 不同,floor 在这些情况下会向下取整,而 trunc 会向上取整。

试一试

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

    f32.const -2.7 ;; load a number onto the stack
    f32.floor ;; round down
    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.7

;; round down
f32.floor

;; the top item on the stack will now be -3
指令 二进制操作码
f32.floor 0x8e
f64.floor 0x9c