[The Accountant] Floating-Point Precision Troubles?

I’m attempting an implementation in Java, and have run into (what appears to be) multiple instances of precision-rounding issues. This is my first contest entry, so forgive me if I’m missing something obvious in the description, but I don’t see anything that says which formulas I should be using, and what level of precision (float vs double). The only formula mentioned is the “Euclidean distance formula.”

Basically, most of my final Integer calculations are plagued by off-by-one values. I’ve read and reread when and where I should be using Math.round vs Math.floor, anyone else experiencing this?

Different languages have a different interpretation of rounding 0.5 values. See thread&comments at https://pay.reddit.com/r/haskell/comments/5136qn/round_05_0/

Keep in mind that a narrowing conversion from a floating-point type to an integral type involves rounding towards zero (refer to JLS 5.1.3 for the specifics). That implies that 10 - ((int) 1.5) (which would be 10 - 1 == 9) is not equivalent to (int)(10 - 1.5) (which would be (int)8.5 == 8)

For me, that insight solved the off-by-one errors.

1 Like