向下取整
floor 指令用於獲取一個數字向下取整到最接近的整數的值。
當用於負數時,floor 與 trunc 不同,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 |