[racket] Decimal rounding problem

From: Stephen Bloch (bloch at adelphi.edu)
Date: Thu Nov 29 11:47:40 EST 2012

On Nov 29, 2012, at 11:01 AM, Neil Toronto wrote:

> This is because numbers in [4.0,8.0) have one fewer bit with which to represent the fractional part than numbers in [2.0,4.0). That bit is needed to represent the larger integer part.

I'm trying to replicate this in Java, using the DecimalFormat class, and I'm getting

0.225 -> "0.22"
1.225 -> "1.22"
2.225 -> "2.22"
3.225 -> "3.22"
4.225 -> "4.22"
9.225 -> "9.22"
17.225 -> "17.22"
35.225 -> "35.22"
68.225 -> "68.22"
130.225 -> "130.22"
250.225 -> "250.22"

What gives?  If Java and Racket are both using IEEE standard double-precision floating point, and both rounding-to-even (as the DecimalFormat class doc says it does by default), why am I NEVER getting round-up behavior in Java?

I tried the negations of the same numbers, and they still all end in ".22", as though it were in round-towards-zero mode, but I queried the rounding mode and it says it's HALF_EVEN.

The corresponding results in Racket, BTW, are
("0.23" "1.23" "2.23" "3.23" "4.22" "9.22" "17.23" "35.23" "68.22" "130.22" "250.22")



Stephen Bloch
sbloch at adelphi.edu



Posted on the users mailing list.