[racket] Decimal rounding problem

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

On Nov 29, 2012, at 9:44 AM, Greg Graham wrote:

> I am trying to report GPA calculation results to 2 decimal places, so I thought real->decimal-string would do the trick. However, the following behavior surprised me:
>> (real->decimal-string 3.225 2)
> "3.23"
>> (real->decimal-string 4.225 2)
> "4.22"
> I would like the second answer to be "4.23", which is what a student would expect to see if they did the calculations themselves. The documentation for real->decimal-string says that it first converts the argument to an exact number. I suspect the problem has something to do with this:
>> (inexact->exact 4.225)
> 4 126663739519795/562949953421312
>> (/ 126663739519795.0 562949953421312.0)
> 0.22499999999999964
> Is there another rounding function that would just round the floating point without going through the conversion to exact?

Another rounding function won't help, because by the time the READER has finished with the character string "4.225", the internal form is already slightly less than 4225/1000.  And if you're getting the inexact 4.225 from somewhere else, again it's not really 4225/1000 so it's already too late to round it the way you want.

You could work in a student language, where the reader reads decimals as exact by default :-)

Seriously, I'm sure there's a way to tell the reader in other languages to read decimals as exact; I just don't know what it is.

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.