Quirrel4.41.0

iostream.castf2i

Defined in module "iostream"
from "iostream" import castf2i
pure fastcall castf2i(f: number): int

Reinterprets the bits of a float as an integer

Parameters

fnumberthe value whose bits become the result

Return value

The 32-bit pattern of f, as a non-negative int: the float's sign bit becomes bit 31 of the result rather than making it negative.

Notes

f is read as a float first, so an integer argument is converted to the float of that value before its bits are taken; castf2i(1) and castf2i(1.0) give the same result, which is the bit pattern of 1.0, not of the integer 1.

Only the low 32 bits of the result are meaningful; the upper bits are always 0.

Example

examples/iostream/castf2i.nut
from "iostream" import castf2i, casti2f

println("castf2i(1.5) =", castf2i(1.5))
println("casti2f(castf2i(1.5)) =", casti2f(castf2i(1.5)))  // round trip returns the original float

// an int argument is converted to a float first, so this is castf2i(1.0),
// not the bit pattern of the integer 1
println("castf2i(1) == castf2i(1.0) =", castf2i(1) == castf2i(1.0))

// a negative float still gives a non-negative int: the sign bit just
// becomes bit 31 of the result
println("castf2i(-1.0) > 0 =", castf2i(-1.0) > 0)
Output:
castf2i(1.5) = 1069547520
casti2f(castf2i(1.5)) = 1.5
castf2i(1) == castf2i(1.0) = true
castf2i(-1.0) > 0 = true

See also

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