Math Function for 8051 in C

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
8051 pow computation

Hi,

Anyone can help me in Math Function, I am using Keil Compiler. Is it any unfavorable for 89c52 microcontroller?

Can you show me the sample Math Function code? Give some sample so that i can try: eg:
square root, Power, and so on....

Thank You
 

Code:
#include <math.h>
#include <stdio.h> /* for printf */

void tst_pow (void) {
  float base;
  float power;
  float y;

  base = 2.0;
  power = 8.0;

  y = pow (base, power); /* y = 256 */

  printf ("%f ^ %f = %f\n", base, power, y);
}

https://www.keil.com/support/man/docs/c51/c51_lib_math.htm

Regards,
IanP
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Hi,

Thank for your usefull example. I try already, it will take alot of spacing for one equation y = pow (base, power);.

Normal spacing for data = 12 and code = 88. After add the equation than data = 40, and code 1944. It is very bad!!!

Do you know why? Got any solution to solve it?

Thank.
 

Because of the "printf " command , it take much space of data code .
 

Hi,

Sorry, I am not using "printf" command also take much space of data code.

Thank
 

Help said:
Sorry, I am not using "printf" command also take much space of data code.

Inside MATH.H header file is defined the prototype of function pow :
Code:
extern float pow   (float x, float y);
In order to match the type of parameters that will receive the value of the arguments you have to use float, even if playing with integer type variable covers your need as well.
Unfortunately the 8051 instruction set was not design for floating point operation, that's why the big object code.
Try to make your own function "pow" with integer type parameters. You'll notice a huge code size decreasing compared with the above approach.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…