473,765 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replacement for 'pow()' function

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.
Dec 13 '07 #1
15 7414
Roman Mashak wrote, On 14/12/07 00:43:
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.
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
Dec 13 '07 #2
Roman Mashak wrote:
>
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 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
Dec 13 '07 #3
pete wrote:
>
/* 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);
Interesting. What are the license requirements on the code?

--
Thad
Dec 13 '07 #4
"Roman Mashak" <mr*@tusur.ruwr ote:
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?
Bit shifting may suffice for a function intended to produce positive
integral powers of integers, but for a complete implementation of pow()
it won't do: you try shifting a float by a double and see what the
result is...

Richard
Dec 13 '07 #5
Thad Smith wrote:
>
pete wrote:

/* 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);

Interesting. What are the license requirements on the code?
It's just trivial code.

--
pete
Dec 13 '07 #6
Thad Smith wrote:
>
pete wrote:

/* 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);

Interesting. What are the license requirements on the code?
I don't know. I think it's public domain.
Here's some more:
http://www.mindspring.com/~pfilandr/C/fs_math/
I got bored with it after writing fs_cos.

--
pete
Dec 13 '07 #7
pete wrote:
Thad Smith wrote:
>Interesting. What are the license requirements on the code?

It's just trivial code.
Licensing isn't about whether the code is trivial or not.

--
Chris "bits typed with meta-bits [typed with meta-meta-bits ...]" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Dec 13 '07 #8
On Thu, 13 Dec 2007 16:43:45 -0800, Roman Mashak wrote:
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.
You can implement exp and log via the cordic algorithm. See eg
http://en.wikipedia.org/wiki/CORDIC
and links therein

Dec 13 '07 #9
On Dec 13, 4:43 pm, "Roman Mashak" <m...@tusur.ruw rote:
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.
Cephes by Moshier
http://www.moshier.net/#Cephes

Or libfdm by SunSoft
http://www.koders.com/noncode/fid7DF...4BEBB9931.aspx

If you already have an exp() then pow() is trivial to implement.
Dec 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13062
by: Arik Peltz | last post by:
Hi, Does anybody know how pow() function is implemented in the standard libraries of visual c++ 6.0 ? Thanks, Arik
0
308
by: Robert Smith | last post by:
Sorry if I posted this incorrectly and/or in the wrong place. I'm an idiot. Nevertheless, it would be kind if someone forwarded this to the appropriate people VC 6 VC 7 (.net) float Test = pow(2, 32); Test = 4.29497e+009 Test = 0 int Test = pow(2, 32); Test = 0 Test = 0
8
29249
by: Black Eagle | last post by:
Hey guys, Under linux, I tried this : #include <stdio.h> #include <math.h> main() { double a= 2.0; double b= 3.0; printf("%f^%f=%f",a,b,pow(a,b));
5
2866
by: Magix | last post by:
#include <math.h> double pow( double base, double exp ); will pow (2, -30) works fine ? (exp is negative value). If not, what are the workaround for exp to be negative?
2
3314
by: matrim | last post by:
What I'm trying to do: 1. Attempt to open the file. The filename, c:\\windData.txt, should be hardcoded into your program. Note the two slash characters in the file name. If the file cannot be opened, display a message and exit the program. 2. From the file read a temperature and a wind speed. Both values should be stored in variables declared as double. The file is a text file. 3. Calculate the wind chill factor using a programmer...
2
10250
by: satya3005 | last post by:
Dear members can u please tell me how to write code for x power n with out using pow() function.The manipulation code must be in only one line.
1
5914
by: denxx | last post by:
Hi, I am new in C++ and was trying to run a programme where I found an error using the pow statement. Error showed is error C2660: 'pow' : function does not take 1 parameters. As I have a square on the whole equation, I am not sure whether I am sure the power function right. Please advise, anyone? Code shown as below: eqn=vf+((vf*(1-x)+vg)*x); fun3 = 1000*hf+1000*hfg*x+(m*m*(pow(eqn,2.0))/2-1000*h-(pow(V,2.0)/2)); many thanks!
2
4331
by: Richard Ballin | last post by:
Hello All, I am taking a class using Visual Studio C++ Express 2010 and on one of the first projects i need to use the Math::Pow() function. The book does not do a good job of explaining how to use this. I was able to create the program and calculate simple interest fine with a normal formula however once I needed to calculate compounded interest it required a exponent. Line 203 of the Code is where the error is. "error C2660:...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.