Casting f64 to i128


#1

Given that f64 can hold integers that are outside the range [-2^127, 2^127-1]:

fn saturated(f: f64) -> i128 {
    f as i128
}

Is it guaranteed that saturated() will always,

  • return -2^127, if f is <= -2^127.
  • return 2^127-1, if f is >= 2^127-1.
  • return -2^127, if f is f64::NEG_INFINITY.
  • return 2^127-1, if f is f64::INFINITY.
  • return 0, if f is f64::NAN.

#2

According to issue #10148, I think these might be undefined behavior? There’s a bunch of discussion there on the topic. It’s all about f64 to smaller integers, though. I don’t know about i128.


#3

It is a useful link. Found more links from there that might be useful - issue 46298, rust-internals discussion