java和C中exp 指数和对数函数的快速实现
来源:互联网 发布:手机怎样设置网络快 编辑:程序博客网 时间:2024/05/17 17:56
1: java的实现 转载于:http://martin.ankerl.com/2007/02/11/optimized-exponential-functions-for-java/
Usually microoptimization is only done in C or C++, but it works quite well in Java too. For a project I needed very fast log() and exp() calculations, and Java’s Math.log() andMath.exp() just doesn’t cut it. After a bit of research I have found the following approximations that are good enough for me:
UPDATE This pow() approximation is obsolete. I have a much faster and more accurate approximation version here.
Fast Exponential Function in Java
The paper “A Fast, Compact Approximation of the Exponential Function” describes a C macro that does a good job at exploiting the IEEE 754 floating-point representation to calculate e^x. I have transformed the macro into Java code:
public static double exp(double val) { final long tmp = (long) (1512775* val + 1072632447); return Double.longBitsToDouble(tmp << 32);}
This code is 5.3 times faster than Math.exp() on my computer. Beware that it is only an approximation, for a detailed analysis read the paper.
Fast Natural Logarithm in Java
I have found the following approximation here, and there is not much information about it except that it is called “Borchardt’s Algorithm” and it is from the book “Dead Reconing: Calculating without instruments”. The approximation is not very good (some might say very bad…), it gets worse the larger the values are. But the approximation is also a monotonic, slowly increasing function, which is good enough for my use case.
public static double log(doublex) { return 6* (x - 1) / (x + 1+ 4* (Math.sqrt(x)));}This approximation is 11.7 times faster than Math.log().
Fast Power Calculation
Equiped with these optimized functions, it is possible to do several other optimizations. For example you can replace
Math.pow(a, b) with Math.exp(b*Math.log(a))
And then use the approximation functions for a highly optimized pow calculation. You can even combine the calculations and simplify it into this:
public static double pow(doublea, doubleb) { final long tmp = (long) (9076650* (a - 1) / (a + 1+ 4* (Math.sqrt(a))) * b + 1072632447); return Double.longBitsToDouble(tmp << 32);}This is 8.7 times faster than the Math.pow(a, b).
Accuracy
The above functions are very inaccurate, especially the log calculation. So before you use this code you have to test it if the approximation is good enough for you!
2:C/C++的实现来自RNNLM源码
static union{ double d; struct{ int j,i; } n;} d2i;#define EXP_A (1048576/M_LN2)#define EXP_C 60801#define FAST_EXP(y) (d2i.n.i = EXP_A*(y)+(1072693248-EXP_C),d2i.d)
转载自 :http://hi.baidu.com/eternity668/item/942a5089fb003516c3162753
- java和C中exp 指数和对数函数的快速实现
- 指数和对数的转化利用
- 指数和对数的公式总结
- exp()函数 快速算法实现
- 高精度对数函数的实现
- C语言编写自己的对数函数
- 对数函数的C、MATLAB表示
- 对数函数的C、MATLAB表示
- 高精度对数函数的实现二
- mysql中对数字处理的函数
- 关于java中对数的计算
- 关于java中对数的计算
- java实现对数正态分布
- C语言exp()函数
- 取对数的函数
- matlab中exp()函数的使用
- C语言对数函数log
- 实现log()和exp()函数的方法,并以此计算pow()
- ImageMagick的安装及使用
- 计数排序
- 【转】C#面向对象经典资料
- tcp的65535个连接之迷
- Java 读取文件夹下所有文件
- java和C中exp 指数和对数函数的快速实现
- vim编辑器学习
- spring 管理action实例 scope="session"
- 操作系统的理解
- Form表单域的动态设计-竖表方式存储数据的优势(3)
- Java虚拟机学习 - 对象访问
- 让root可以Telnet
- GDB调试精粹及使用实例
- java 获得外网ip