[Openmcl-devel] roundings...
Bill St. Clair
wws at clozure.com
Fri Sep 7 03:11:26 PDT 2012
Joakim,
Please send an example of a .5 case that doesn't round to the even number.
Your example is running into the limit of resolution of single floats.
5.4999999 IS 5.5, so it rounds to 6:
CL-USER> 5.4999999
5.5
CL-USER> (round *)
6
-0.5
CL-USER> (= 5.4999999 (+ 5 1/2))
T
CL-USER> 5.4999999d0
5.4999999D0
CL-USER> (round *)
5
0.4999998999999997D0
Note the rounding error in the final result. Almost, but not quite
0.4999999 . So it is with floats. If you need precise math, use
RATIONALs (INTEGERs and RATIOs).
If your C code works differently, are you sure you're using single
floats there? May I see it?
-Bill
On Fri, Sep 7, 2012 at 4:30 AM, Joakim Sandgren
<joakim at joakimsandgren.com> wrote:
> Hi,
>
> how can I do a more 'c-like' rounding in lisp ?
> that is in the .5 cases not always go to the even number.
> i would like to have it to round up
> and perhaps have better float resolution ? in lisp: (round 5.499999) => 5 (round 5.4999999) => 6
>
>
>
> are there some way around this?
>
> sincerely
> joakim
>
>
>
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
More information about the Openmcl-devel
mailing list