wasm_num_trunc (wasm v0.1.0)

View Source

Float to integer truncation, both families. Read this if a conversion traps where you expected a clamp, or the other way round.

The two families differ only in what they do when the value does not fit:

  • i32.trunc_f32_s and friends trap on NaN and on out-of-range values.
  • i32.trunc_sat_f32_s and friends saturate: NaN becomes 0, and out-of-range values clamp to the nearest representable bound.

The range test is the subtle part. It must be done against the exact real bound, not the rounded one: f32 cannot represent 2^31-1, so testing V =< 2147483647.0 would wrongly accept 2147483648.0, which rounds to the same float. The correct test is strict against the next power of two, which is exactly representable. Truncation happens first, so the comparison is on the already-truncated value.

Summary

Types

fspecial()

-type fspecial() :: infinity | neg_infinity | {nan, Sign :: 0 | 1, Payload :: non_neg_integer()}.

Functions

apply/2

-spec apply(atom(), float() | fspecial()) -> integer().