Back to Content

sqrt: Wasm text instruction

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

latest817 B
Original Source

The sqrt instructions, short for square root, are used to get the square root of a number.

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

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

Syntax

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
InstructionBinary opcode
f32.sqrt0x91
f64.sqrt0x9f