Why is the default type of `ceiling` and` floor` numeric?

Why is everyone "numeric" ?

 class(ceiling(3)) class(ceiling(3L)) class(ceiling(3.1)) class(floor(2)) class(floor(2L)) class(floor(2.1)) 

This is similar to one arithmetic operation, where the result is uniquely an integer (as opposed to, say, exponential ), regardless of the input (this is an error for transmitting a complex number).

I tried to talk for the answer related to the base C code, but nothing really worked out.

I also found out that although "%/%"(x,y) should always be an integer as well, the class results depends on the input types, for example. 5%/%2 , 6%/%2 and 6%/%2L are all numeric , but 5L%/%2L and 6L%/%2L both integer (something is mentioned in ?Arithmetic ); it doesn't make sense to me either, but at least it's documented.

Is there a simple reason to return numeric objects from ceiling and floor ? If we were talking about inefficiencies due to repeated casting (which seems to be the case for integer division), I would expect class(ceiling(3L)) be "integer" , so what happens?

+5
source share
1 answer

I don’t know why the ceiling was created to return numeric , but the following example shows the ceiling constraints if it really returned an integer:

 options(digits = 15) .Machine$integer.max + 1.4 #[1] 2147483648.4 ceiling(.Machine$integer.max + 1.4) #[1] 2147483649 as.integer(ceiling(.Machine$integer.max + 1.4)) #[1] NA #Warning message: #NAs introduced by coercion 

However, there is no good reason why I see why ceiling does not return an integer when specifying integer input.

+6
source

All Articles