[R] converting numeric to integer
Linlin Yan
yanlinlin82 at gmail.com
Sun May 17 11:36:45 CEST 2009
On Sun, May 17, 2009 at 5:00 PM, Thomas Mang <Thomas.Mang at fiwi.at> wrote:
> Hi,
>
> Well, also not quite.
> Suppose x = 5.00000000001 (in finite binary represenation). Then I want x ==
> 5, but that is of course an integer which is now less than the original
> numeral.
Sorry for that I haven't got what your mean exactly about the finite
binary representation, because my English is not very good. How much
is the difference of x and 5 when result must be 5?
> Your code would turn it into 6; moreover, your code would still work on
> numeric-values, so once it gets to the interger-conversion we are back at
> where the problem started, namely at the issue of finite floating-point
> representations, which might, or might not, yield the desired integer after
> the decimal digits have been truncated.
>
> BTW, do you know why sometimes replys do not appear in the newsgroups ->
> mine to yours is not there, equally not your last response (althought the
> later might of course be the resuld because I made a mistak. in the first
> step....)
The reason our last replys did not appear in the newsgroup is that you
replied to me only rather than to the maillist addres
r-help at stat.math.ethz.ch.
>
> cheers and thanks,
> Thomas
>
>
> Linlin Yan wrote:
>>
>> I see. What you want is the integer with same sign as the original
>> numeral, and whose absolute value is the least integer which is not
>> less than absolute value of the original numeral. Am I right? I am
>> afraid that there may not be any single function could work it out.
>> But I could give the following expression:
>> sign(x) * ceiling(abs(x)),
>> which may be a little clearer.
>>
>> On Sun, May 17, 2009 at 2:02 PM, Thomas Mang <Thomas.Mang at fiwi.at> wrote:
>>
>>>
>>> Hi,
>>>
>>> ceiling would do the wrong thing for negative values. If x = -4.9999999,
>>> the
>>> wanted result would be -5, but ceiling makes a -4 out of it.
>>>
>>> bye,
>>> Thomas
>>>
>>> Linlin Yan wrote:
>>>
>>>>
>>>> How about ceiling(x), which return the smallest integer not less than x?
>>>>
>>>> On Sun, May 17, 2009 at 2:49 AM, Thomas Mang <thomas.mang at fiwi.at>
>>>> wrote:
>>>>
>>>>
>>>>>
>>>>> Hello,
>>>>>
>>>>> Suppose I have x, which is a variable of class numeric. The
>>>>> calculations
>>>>> performed to yield x imply that mathematically it should be an integer
>>>>> ,
>>>>> but
>>>>> due to round-off errors, it might not be (and so in either direction).
>>>>> The
>>>>> error is however small, so round(x) will yield the appropriate integer
>>>>> value. Moreover, this integer values is guaranteed to be representable
>>>>> by
>>>>> an
>>>>> 'integer' class, that is -2^31 < x < 2^31, and logically it is an
>>>>> integer
>>>>> anyway. So I want to convert x from class 'numeric' to 'integer'. What
>>>>> is
>>>>> the most elegant, but always correct way, to achieve this conversion ?
>>>>>
>>>>> What comes to mind is of course something along:
>>>>>
>>>>> x = as.integer(round(x))
>>>>>
>>>>> I am, however, not sure if this always works, because I do not know if
>>>>> the
>>>>> round-function is guaranteed to return a numeric value which, in finite
>>>>> binary representation, is always >= the underlying mathematical
>>>>> integer.
>>>>> If
>>>>> that is however guaranteed, that would of course be a simple + elegant
>>>>> one.
>>>>>
>>>>> An alternative I came up with is:
>>>>>
>>>>> x = as.integer(round(x) + ifelse(x >= 0, 0.5, -0.5))
>>>>> Where I explicitly add a bit to ensure the finite binary representation
>>>>> must
>>>>> be >= the underlying integer, and then truncate the decimal digits.
>>>>> IMO, this one is always guaranteed to work, at least within the
>>>>> numerical
>>>>> range of what integers are limited to anyway.
>>>>>
>>>>>
>>>>> What's your opinion on the issue ?
>>>>> Any other solution ?
>>>>>
>>>>> Thanks a lot in advance and cheers,
>>>>> Thomas
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
More information about the R-help
mailing list