替换'pow()'功能 [英] replacement for 'pow()' function

查看:67
本文介绍了替换'pow()'功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想简单地替换嵌入式

平台的''pow()''函数我'正在努力。什么是更好的方式,可能有点转移?

谢谢。


祝你好运,Roman Mashak。

Hello,

I''d like to make a simple replacement of ''pow()'' function for the embedded
platform I''m working on. What is the better way, probably bit shifting?
Thanks.

Best regards, Roman Mashak.

推荐答案

Roman Mashak写道,在12/12/07 00:43:
Roman Mashak wrote, On 14/12/07 00:43:

你好,


我想为我正在研究的嵌入式

平台简单地替换''pow()''函数。什么是更好的方式,可能是位移?

谢谢。
Hello,

I''d like to make a simple replacement of ''pow()'' function for the embedded
platform I''m working on. What is the better way, probably bit shifting?
Thanks.



这一切都取决于您的要求的详细信息,包括

速度,准确度或空间是否更重要以及每个的限制。我知道代码使用了一个小型查找表来执行trig函数,一个

导致很多不准确但它足够好,快速很小。

我知道其他代码使用查找表和一些

形式的插值(我从未检查过细节)因为速度有点小

很重要(处理器更快)。

-

Flash Gordon

It all depends on the details of your requirements, including whether
speed, accuracy or space is more important and on the limits for each. I
know of code that uses a small look-up-table for trig functions, one
that leads to a lot of inaccuracy but it "good enough", fast and small.
I know other code that uses a look-up-table and interpolation of some
form (I never checked the details) because speed was a bit less
important (the processor was faster).
--
Flash Gordon


Roman Mashak写道:
Roman Mashak wrote:

>

您好,


我想要为我正在研究的嵌入式平台做一个简单的替换''pow()''

函数。
>
Hello,

I''d like to make a simple replacement of ''pow()''
function for the embedded platform I''m working on.



/ * BEGIN fs_pow_test.c * /


#include< stdio.h>

/ *

** fs_pow以便携式独立代码实现

* /

#include" fs_pow.h"


int main(无效)

{

puts(" / * BEGIN fs_pow_test.c output * / \ n");

printf(" fs_pow(2,4)is%f\\\
\ n",

fs_pow(2,4));

printf(" fs_pow(0.0001,-0.25) - 10是%e \ n \ nn",

fs_pow(0.0001,-0.25) - 10);

puts(" / * END fs_pow_test.c输出* /");

返回0;

}


/ * END fs_pow_test.c * /


/ * BEGIN fs_pow.h * /

/ *

* *可独立或托管的便携代码

**实施C.

* /

#ifndef H_FS_POW_H

#define H_FS_POW_H


double fs_pow(double x,double y);

double fs_fmod(d ouble x,double y);

double fs_exp(double x);

double fs_log(double x);

double fs_sqrt(double x );


#endif


/ * END fs_pow.h * /


/ * BEGIN fs_pow.c * /


#include< float.h>


#include" fs_pow.h"


double fs_pow(double x,double y)

{

double p;


if (0 x&& fs_fmod(y,1)== 0){

if(fs_fmod(y,2)== 0){

p = fs_exp(fs_log(-x)* y );

}其他{

p = -fs_exp(fs_log(-x)* y);

}

} else {

if(x!= 0 || 0> = y){

p = fs_exp(fs_log(x)* y);

}否则{

p = 0;

}

}

返回p;

}


double fs_fmod(double x,double y)

{

double a,b;

const double c = x;


if(0 c){

x = -x;

}

if(0 y){

y = -y;

}

if(y!= 0&& DBL_MAX> = y&& DBL_MAX> = x){

而(x> = y){

a = x / 2 ;

b = y;

while(a> = b){

b * = 2;

}

x - = b;

}

}否则{

x = 0;

}

返回0 c? -x:x;

}


double fs_exp(double x)

{

unsigned n,square;

double b,e;

静态双x_max,x_min;

static int initialized;


if(!initialized){

initialized = 1;

x_max = fs_log(DBL_MAX);

x_min = fs_log( DBL_MIN);

}

if(x_max> = x&& x> = x_min){

for(square = 0; x 1; x / = 2){

++ square;

}

while(-1 x){

++ square;

x / = 2;

}

e = b = n = 1;

做{

b / = n ++;

b * = x;

e + = b;

b / = n ++;

b * = x;

e + = b;

} while(b DBL_EPSILON / 4);

while(square--!= 0){

e * = e;

}

} else {

e = x 0? DBL_MAX:0;

}

返回e;

}


double fs_log(double x )

{

int n;

加倍a,b,c,epsilon;

静态双A, B,C;

static int initialized;


if(x 0&& LDBL_MAX> = x){

if(!initialized){

initialized = 1;

A = fs_sqrt(2);

B = A / 2;

C = fs_log(A);

}

for(n = 0; x A; x / = 2){

++ n;

}

而(B x){

--n;

x * = 2;

}

a =(x - 1)/(x + 1);

x = C * n + a;

c = a * a;

n = 1;

epsilon = DBL_EPSILON * x;

if(0 a) {

if(epsilon 0){

epsilon = -epsilon;

}

do {

n + = 2;

a * = c;

b = a / n;

x + = b;

} while(epsilon b);

} else {

if(0 epsilon){

epsilon = -epsilon;

}

做{

n + = 2;

a * = c;

b = a / n;

x + = b;

}而(b epsilon);

}

x * = 2;

}否则{

x = -DBL_MAX;

}

返回x;

}


double fs_sqrt(double x)

{

int n;

加倍a,b;


if(x 0&& DBL_MAX> = x){

for(n = 0; x 2; x / = 4){

++ n;

}

while(0.5 x){

--n;

x * = 4;

}

a = x;

b =(1 + x)/ 2;

do {

x = b;

b =(a / b + b)/ 2;

} while(xb);

while(n 0){

x * = 2;

- n;

}

而(0 n){

x / = 2;

++ n;

}

}否则{

if(x!= 0) {

x = DBL_MAX;

}

}

返回x;

}


/ * END fs_pow.c * /

-

pete

/* BEGIN fs_pow_test.c */

#include <stdio.h>
/*
** fs_pow is implemented in portable freestanding code
*/
#include "fs_pow.h"

int main(void)
{
puts("/* BEGIN fs_pow_test.c output */\n");
printf("fs_pow(2, 4) is %f\n\n",
fs_pow(2, 4));
printf("fs_pow(0.0001, -0.25) - 10 is %e\n\n",
fs_pow(0.0001, -0.25) - 10);
puts("/* END fs_pow_test.c output */");
return 0;
}

/* END fs_pow_test.c */

/* BEGIN fs_pow.h */
/*
** Portable code for either freestanding or hosted
** implementations of C.
*/
#ifndef H_FS_POW_H
#define H_FS_POW_H

double fs_pow(double x, double y);
double fs_fmod(double x, double y);
double fs_exp(double x);
double fs_log(double x);
double fs_sqrt(double x);

#endif

/* END fs_pow.h */

/* BEGIN fs_pow.c */

#include <float.h>

#include "fs_pow.h"

double fs_pow(double x, double y)
{
double p;

if (0 x && fs_fmod(y, 1) == 0) {
if (fs_fmod(y, 2) == 0) {
p = fs_exp(fs_log(-x) * y);
} else {
p = -fs_exp(fs_log(-x) * y);
}
} else {
if (x != 0 || 0 >= y) {
p = fs_exp(fs_log( x) * y);
} else {
p = 0;
}
}
return p;
}

double fs_fmod(double x, double y)
{
double a, b;
const double c = x;

if (0 c) {
x = -x;
}
if (0 y) {
y = -y;
}
if (y != 0 && DBL_MAX >= y && DBL_MAX >= x) {
while (x >= y) {
a = x / 2;
b = y;
while (a >= b) {
b *= 2;
}
x -= b;
}
} else {
x = 0;
}
return 0 c ? -x : x;
}

double fs_exp(double x)
{
unsigned n, square;
double b, e;
static double x_max, x_min;
static int initialized;

if (!initialized) {
initialized = 1;
x_max = fs_log(DBL_MAX);
x_min = fs_log(DBL_MIN);
}
if (x_max >= x && x >= x_min) {
for (square = 0; x 1; x /= 2) {
++square;
}
while (-1 x) {
++square;
x /= 2;
}
e = b = n = 1;
do {
b /= n++;
b *= x;
e += b;
b /= n++;
b *= x;
e += b;
} while (b DBL_EPSILON / 4);
while (square-- != 0) {
e *= e;
}
} else {
e = x 0 ? DBL_MAX : 0;
}
return e;
}

double fs_log(double x)
{
int n;
double a, b, c, epsilon;
static double A, B, C;
static int initialized;

if (x 0 && LDBL_MAX >= x) {
if (!initialized) {
initialized = 1;
A = fs_sqrt(2);
B = A / 2;
C = fs_log(A);
}
for (n = 0; x A; x /= 2) {
++n;
}
while (B x) {
--n;
x *= 2;
}
a = (x - 1) / (x + 1);
x = C * n + a;
c = a * a;
n = 1;
epsilon = DBL_EPSILON * x;
if (0 a) {
if (epsilon 0) {
epsilon = -epsilon;
}
do {
n += 2;
a *= c;
b = a / n;
x += b;
} while (epsilon b);
} else {
if (0 epsilon) {
epsilon = -epsilon;
}
do {
n += 2;
a *= c;
b = a / n;
x += b;
} while (b epsilon);
}
x *= 2;
} else {
x = -DBL_MAX;
}
return x;
}

double fs_sqrt(double x)
{
int n;
double a, b;

if (x 0 && DBL_MAX >= x) {
for (n = 0; x 2; x /= 4) {
++n;
}
while (0.5 x) {
--n;
x *= 4;
}
a = x;
b = (1 + x) / 2;
do {
x = b;
b = (a / b + b) / 2;
} while (x b);
while (n 0) {
x *= 2;
--n;
}
while (0 n) {
x /= 2;
++n;
}
} else {
if (x != 0) {
x = DBL_MAX;
}
}
return x;
}

/* END fs_pow.c */
--
pete

pete写道:
pete wrote:

>

/ * BEGIN fs_pow.h * /

/ *

**独立或托管的便携代码

**实施C.

* /

#ifndef H_FS_POW_H

#define H_FS_POW_H


double fs_pow(double x,双y);

double fs_fmod(double x,double y);

double fs_exp(double x);

double fs_log(double x );

double fs_sqrt(double x);
>
/* BEGIN fs_pow.h */
/*
** Portable code for either freestanding or hosted
** implementations of C.
*/
#ifndef H_FS_POW_H
#define H_FS_POW_H

double fs_pow(double x, double y);
double fs_fmod(double x, double y);
double fs_exp(double x);
double fs_log(double x);
double fs_sqrt(double x);



有趣。代码的许可要求是什么?


-

Thad

Interesting. What are the license requirements on the code?

--
Thad


这篇关于替换'pow()'功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆