Why is there an extra value after dividing floating points in rust

Viewed 59

In my rust code when I make divisions an extra number is added in the decimal but in some cases it does not.

For example, in the code below if 500 is divided by 10^24 it yields: 0.000000000000000000000500000000000000005 which when converted back(500 * (10^24)) I get 500.00000000000006. but for other numbers like if amount is 700 I get the correct answer.

let amount = 500;
let base = 10;
let result = amount as f64 / base.pow(24) as f64;

Is there something I'm doing wrong here or is there a better way of doing this?

0 Answers
Related