0

I was on a pipeline of functions and realised that, when converting integers into floats (not always, but in specific cases!), I lose some units on the way... Let me share an example (you can run it online here):

fn main() {
    let int_var:i64 = 12091941575702789;

    let converted:f64 = int_var as f64;

    println!("NUM: {:.1}", converted);
}

// Prints
NUM: 12091941575702788.0

Is there maybe another way to do this cast? I tried using f64::from(v) but "the trait From is not implemented for f64".

Thanks in advance!

Dexygen
  • 12,287
  • 13
  • 80
  • 147
Xavi
  • 1
  • 12091941575702789 requires 54 bits to represent, which is beyond what a f64's 52 fractional component (mantissa) can accurately represent. So it's impossible for an f64. – Colonel Thirty Two Oct 15 '22 at 21:14

0 Answers0