For the following code (Java):
double d = (double) m / n; //m and n are integers, n>0
int i = (int) (d * n);
i == m
Is the last expression always true? If it's not is this always true?:
i = (int) Math.round(d * n);
i == m
int i = (int) (d * n); i == m;
This is false for m=1, n=49.
i = (int) Math.round(d * n); i == m;
My intuition tells me it should be true, but it may be hard to prove rigorously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With