[Openmcl-devel] roundings...
Gary Byers
gb at clozure.com
Sat Sep 8 22:49:44 UTC 2012
The fact that some cases of ROUND in CCL depend on the rounding mode of the
FPU being set to its default value doesn't mean that it's a good idea to change
the rounding mode in order to get ROUND to produce incorrect results ...
I'd also misremembered that C defaults to a rounding-mode other than :nearest.
Not a good message at all, I'm afraid.
On Sat, 8 Sep 2012, Joakim Sandgren wrote:
> interesting!
> what I understand is that the :zero mode then equals to floor...?
> and that :negative mode does the same (?)
> and that the :positive mode equals to ceiling.?
>
> one could have a mode that rounds upwards from .5...
>
> joakim
>
> Le 8 sept. 2012 ?22:02, Gary Byers <gb at clozure.com> a ?rit :
>
>> I'd parsed the original question as asking how to change the rounding
>> mode used by the hardware, in which case section 4.11 of the CCL manual
>> would be suggested reading.
>>
>> CCL defaults to using "round to nearest [even number]" and IIRC C defaults
>> to "round towards 0", so things like (ROUND 1.5) will yield different results than the C equivalent.
>>
>> ? (get-fpu-mode :rounding-mode)
>> :NEAREST
>> ? (round 1.5)
>> 2
>> -0.5
>> ? (set-fpu-mode :rounding-mode :zero)
>> 14592
>> ? (round 1.5)
>> 1
>> 0.5
>> ?
>>
>>
>> On Fri, 7 Sep 2012, Bill St. Clair wrote:
>>
>>> 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
>>> _______________________________________________
>>> Openmcl-devel mailing list
>>> Openmcl-devel at clozure.com
>>> http://clozure.com/mailman/listinfo/openmcl-devel
>>>
>>>
>>
>
>
More information about the Openmcl-devel
mailing list