在R中实现nextafter功能 [英] Implementation of nextafter functionality in R
问题描述
number + .Machine $ double.eps
通常不起作用。 不,但是有两种方法可以做到这一点:
使用C 如果需要 该函数应该被编译为一个共享库: 适用于类UNIX操作系统。共享库可以使用 更深入的调用来自R的C是这里。 使用R 现在,与C不同的是,如果你想检查整数,你可以在同一个函数中做,但是你需要根据类型改变增量。 UPDATE Is there any implementation of functionality in R, such that it is possible to get the next representable floating point number from a given floating point number. This would be similar to the nextafter function in the C standard library. Schemes such as No, but there are two ways you can make it: Using C If you want the exact functionality of the That function should then be compiled as a shared library: for UNIX-like OSs. The shared library can be called using A more in depth treatment of calling C from R is here. Using R Now, unlike C, if you want to check integers, you can do so in the same function but you need to change the increment based on the type. UPDATE
The previous code did not perform as expected for numbers larger than 2. There is a factor that needs to be multiplied by the
这篇关于在R中实现nextafter功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
nextafter()
函数的确切功能,可以编写一个C函数作为函数的接口,满足以下两个约束:
R CMD SHLIB foo.c
dyn.load(foo.so)
来调用。然后你可以使用 .C()
函数从R中调用函数
.C(foo,...)
number + .Machine $ double.eps
是要走的路,但你必须考虑边缘情况,例如,如果 x - y< .Machine $ double.eps
或if x == y
。我会写这样的函数:
$ $ p $ nextafter< - function(x,y){
#如果(x> 0){
factor = 2 ^ floor(log2(x))+ ifelse(x> = 4),则检查和边界检查在这里执行
delta = y - x
,1,0)
} else if(x <0){
factor = 65
}
if(delta> .Machine $ double.eps){
return(x + factor * .Machine $ double.eps)
} else if(delta <.Machine $ double.eps){
return(x - factor * .Machine $ double.eps )
} else {
return(x)
}
}
以前的代码没有像大于2的数字那样执行。有一个因子需要乘以 .Machine $ double。 eps
,使它足够大,导致数字与众不同它与2加1的最近的功率有关。
n < - -100
因子< ;(vector)('numeric',100)
for(i in 1:n){
j = 0
while(TRUE){
j = j + 1
if(i - j * .Machine $ double.eps!= i)break()
}
factor [i] = j
}
number + .Machine$double.eps
don't work in general.nextafter()
function, you can write a C function that works as an interface to the function such that the following two constraints are met:
R CMD SHLIB foo.c
dyn.load("foo.so")
. You can then call the function from inside R using the .C()
function.C("foo", ...)
number + .Machine$double.eps
is the way to go but you have to consider edge cases, such as if x - y < .Machine$double.eps
or if x == y
. I would write the function like this:nextafter <- function(x, y){
# Appropriate type checking and bounds checking goes here
delta = y - x
if(x > 0){
factor = 2^floor(log2(x)) + ifelse(x >= 4, 1, 0)
} else if (x < 0) {
factor = 65
}
if (delta > .Machine$double.eps){
return(x + factor * .Machine$double.eps)
} else if (delta < .Machine$double.eps){
return(x - factor * .Machine$double.eps)
} else {
return(x)
}
}
.Machine$double.eps
to make it large enough to cause the numbers to be different. It is related to the nearest power of 2 plus one. You can get an idea of how this works with the below code:n <- -100
factor <- vector('numeric', 100)
for(i in 1:n){
j = 0
while(TRUE){
j = j + 1
if(i - j * .Machine$double.eps != i) break()
}
factor[i] = j
}