[R] That dreaded floating point trap

Kenn Konstabel lebatsnok at gmail.com
Thu Mar 31 15:15:10 CEST 2011


n Thu, Mar 31, 2011 at 3:56 PM, Alexander Engelhardt
<alex at chaotic-neutral.de> wrote:
> Am 31.03.2011 14:41, schrieb Sarah Goslee:
>>
>> On Thu, Mar 31, 2011 at 8:14 AM, Alexander Engelhardt
>>>
>>> this helps, thank you.
>>> But if this code is in a function, and some user supplies a vector, I
>>> will
>>> still have to round it in the function, I guess.
>>>
>>> It's weird how 0.1 is different from round(0.1, digits=1) , but I don't
>>> want
>>> to read that 90 page long floating point paper which was referenced
>>> somewhere :)
>>
>>
>> Or you could try the much shorter R FAQ 7.31. Turns out it
>> isn't weird at all, if you are a computer.
>
> You're a computer! :)
>
> But yes.. the FAQ entry was where I found all.equal and the referenced
> 90-page-paper. But I didn't find out how to do a subset with 'somevector >
> 0.4'.
>
> I think I'll have to round the numbers every time now.. or use some other
> not-so-pretty workaround like 'somevector > 0.4 - 0.05' for 0.1-binned data.

You could define your own %>% and %<% to do rounding and comparison in
one step.

Kenn



More information about the R-help mailing list