Quirrel4.41.0

iostream.swapfloat

Defined in module "iostream"
from "iostream" import swapfloat
pure fastcall swapfloat(val: number): float

Byte-swaps the bits of a float

Parameters

valnumberthe value, read as a float, whose bytes get swapped

Return value

The float made of the 4 bytes of val in reverse order.

Notes

val is read as a float first, so an integer argument is converted to the float of that value before swapping, the same as in castf2i.

Reversing the bytes of an ordinary-looking float rarely gives another ordinary-looking float: the result is typically a subnormal value with a tiny magnitude, or on some builds a flush to zero. Do not expect swapfloat(x) to look like x; only the round trip is predictable, since swapping is its own inverse.

Example

examples/iostream/swapfloat.nut
from "iostream" import swapfloat

// swapping twice restores the value; that is the only predictable case,
// so the example checks the round trip instead of a single swap
println("swapfloat(swapfloat(1.5)) == 1.5 =", swapfloat(swapfloat(1.5)) == 1.5)

// an int argument is converted to a float first, like castf2i
println("swapfloat(2) == swapfloat(2.0) =", swapfloat(2) == swapfloat(2.0))
Output:
swapfloat(swapfloat(1.5)) == 1.5 = true
swapfloat(2) == swapfloat(2.0) = true

See also

swap4Byte-swaps a 32-bit value
castf2iReinterprets the bits of a float as an integer
iostreammodule index