Back to Content

abs: Wasm text instruction

files/en-us/webassembly/reference/numeric/abs/index.md

latest878 B
Original Source

The abs instructions, short for absolute, are used to get the absolute value of a number. That is, it returns x if x is positive, and the negation of x if x is negative.

{{InteractiveExample("Wat Demo: abs", "tabbed-standard")}}

wat
(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)
)
js
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

Syntax

wat
;; load a number onto the stack
f32.const -2

;; absolute
f32.abs

;; the top item on the stack will now be 2
InstructionBinary opcode
f32.abs0x8b
f64.abs0x99