在R中实现nextafter功能

在R中实现nextafter功能

本文介绍了在R中实现nextafter功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! R中是否有任何功能的实现,从而可以从给定的浮点数中获得下一个可表示的浮点数。这与C标准库中的 nextafter 函数类似。方案如 number + .Machine $ double.eps 通常不起作用。 解决方案不,但是有两种方法可以做到这一点: 使用C 如果需要 nextafter()函数的确切功能,可以编写一个C函数作为函数的接口,满足以下两个约束: 该函数不会返回一个值。所有的工作都是作为副作用来完成的(改变参数的值)。 所有参数都是指针。即使是标量也是向量(长度为1)。 该函数应该被编译为一个共享库: R CMD SHLIB foo.c 适用于类UNIX操作系统。共享库可以使用 dyn.load(foo.so)来调用。然后你可以使用 .C()函数从R中调用函数 .C(foo,...) 更深入的调用来自R的C是这里。 使用R 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 factor = 65 } if(delta> .Machine $ double.eps){ return(x + factor * .Machine $ double.eps)} else if(delta return(x - factor * .Machine $ double.eps )} else { return(x)} } 现在,与C不同的是,如果你想检查整数,你可以在同一个函数中做,但是你需要根据类型改变增量。 UPDATE 以前的代码没有像大于2的数字那样执行。有一个因子需要乘以 .Machine $ double。 eps ,使它足够大,导致数字与众不同它与2加1的最近的功率有关。 n 因子< ;(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 } 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 number + .Machine$double.eps don't work in general. 解决方案 No, but there are two ways you can make it:Using CIf you want the exact functionality of the nextafter() function, you can write a C function that works as an interface to the function such that the following two constraints are met:The function does not return a value. All work is accomplished as a "side effect" (changing the values of arguments).All the arguments are pointers. Even scalars are vectors (of length one) in R.That function should then be compiled as a shared library:R CMD SHLIB foo.cfor UNIX-like OSs. The shared library can be called using dyn.load("foo.so"). You can then call the function from inside R using the .C() function.C("foo", ...)A more in depth treatment of calling C from R is here.Using Rnumber + .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) }}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.UPDATEThe previous code did not perform as expected for numbers larger than 2. There is a factor that needs to be multiplied by the .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 <- -100factor <- 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} 这篇关于在R中实现nextafter功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 03:39