iostream.swapfloat
Defined in module
"iostream"from "iostream" import swapfloatByte-swaps the bits of a float
Parameters
val | number | the 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
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) = trueSee also
swap4 | Byte-swaps a 32-bit value |
castf2i | Reinterprets the bits of a float as an integer |
| iostream | module index |