CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pow(x,y) code replacement with qip(x,y) function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jerry
Guest







pow(x,y) code replacement with qip(x,y) function
PostPosted: Mon Mar 10, 2003 7:43 pm     Reply with quote

I found this little snipet of code in the latest Dr. Dobbs Magazine to replace the f = pow(x,y) with f = qip(x,y).

It has a limitation that y has to be a positive integer.

It appears to be more accurate that CCS's pow() function. The qip(2.5, 5) function only 5\% rom, pow(2.5, 5) function 17\% rom.

My example below with some results.

Code:
// #define PIC_16F87X
#define PIC_18FXXX

#define 98MHZCLOCK
// #define 8MHZCLOCK

#case

#IFDEF PIC_16F87X
  #include <16F876.h>
  #fuses hs, nowdt, noprotect, nolvp, put
//  #device PIC16F876 *=16 icd=TRUE
   #device PIC16F876 *=16
  #device adc=8
#ENDIF


#IFDEF PIC_18FXXX
  #include <18F252.h>
  #include "defs_18f.h"
  #fuses hs, nowdt, noprotect, nolvp, put, NOOSCSEN
  #device PIC18F252 *=16
//  #device PIC18F252 *=16 icd=TRUE
  #device adc=8

 #build(reset=0x200)
 #build(interrupt=0x208)
 #org 0x0000,0x01ff
 void bootloader() {
 #asm
 nop
 #endasm
} // Reserve space for the bootloader

#ENDIF


#include <math.h>

#ifdef 8MHZCLOCK
    #use delay(clock=8000000)   /////////// Check Crystal Value ////////////
#endif

#ifdef 98MHZCLOCK
    #use delay(clock=9830400)   /////////// Check Crystal Value ////////////
#endif

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

/* Compute pow(x,n)
   For positive Int n
   through repeated squaring */

float qip(float x, unsigned int n)
{
   float h = 1.0;

   while(n)
   {
      if (n & 1)
         h *= x;

      x *= x;
      n >>= 1;
   }
    return h;
}


main()
{
    unsigned int i;

//    printf("\%9.2f  ", pow(2.5,i));


    for (i = 10; i < 33; i++)
    {
        printf("\%9.2f  ", qip(2,i));
        printf("\%9.2f\n", pow(2,i));
    }

    printf("\n");



    printf("x , y   qip(x,y)    pow(x,y)             Calculator\n");
    for (i = 18; i < 24; i++)
    {
        printf("2.5,\%d     ", i);
        printf("\%9.2f  ", qip(2.5,i));
        printf("\%9.2f\n", pow(2.5,i));
    }

    printf("\n");

    printf("x , y   qip(x,y)    pow(x,y)             Calculator\n");
    for (i = 3; i < 9; i++)
    {
//        printf("\%9.2f  \%9.2f\n", qip(12.1,i), pow(12.1,i));  // This does not work ???

        printf("12.1,\%d     ", i);
        printf("\%9.2f  ", qip(12.1,i));
        printf("\%9.2f\n", pow(12.1,i));


    }
    printf("\n");

    printf("x , y   qip(x,y)    pow(x,y)             Calculator\n");
    for (i = 7; i < 14; i++)
    {
        printf("5.75,\%d     ", i);
        printf("\%9.2f  ", qip(5.75,i));
        printf("\%9.2f\n", pow(5.75,i));
    }

    printf("\n");


    while(TRUE)
    {
    }
}


Code:
x , y      qip(x,y)    pow(x,y)          Calculator
2.5,18     14551914.85  14551939.88      14551915.23
2.5,19     36379784.94  36379801.33      36379788.07
2.5,20     90949465.03  90949496.62      90949470.18
2.5,21     227373659.61  227373722.19    227373675.4
2.5,22     568434113.26  568435010.31    568434188.6
2.5,23     1421085424.72  1421087451.27  1421085472.0

x , y      qip(x,y)    pow(x,y)          Calculator
12.1,3       1771.56    1771.56          1771.561
12.1,4      21435.88   21435.89          21435.888
12.1,5     259374.24  259374.70          259374.246
12.1,6     3138428.30  3138430.01        3138428.377
12.1,7     37974984.64  37975004.91      37974983.36
12.1,8     459497258.06  459497588.87    459497298.6

x , y      qip(x,y)    pow(x,y)          Calculator
5.75,7     207814.03  207814.11          207814.0532
5.75,8     1194930.68  1194930.68        1194930.806
5.75,9     6870851.39  6870858.43        6870852.133
5.75,10     39507392.94  39507421.25     39507399.76
5.75,11     227167513.96  227167899.90   227167548.6
5.75,12     1306213229.89  1306213229.89 1306213405.0
5.75,13     7510725915.43  7510743737.22 7510727077.0

=========================================
CCS PCH C Compiler, Version 3.146, 14129

Filename: c:\program files\picc\fp-ttspi\powtest.LST

ROM used: 1512 (5\%)
Largest free fragment is 30744
RAM used: 10 (1\%) at main() level
28 (2\%) worst case
Stack: 2 locations

only this output printf("\%9.2f ", qip(2.5,i));
======================+==================

*****************************************************
CCS PCH C Compiler, Version 3.146, 14129

Filename: c:\program files\picc\fp-ttspi\powtest.LST

ROM used: 5586 (17\%)
Largest free fragment is 26670
RAM used: 11 (1\%) at main() level
63 (4\%) worst case
Stack: 3 locations

only this output printf("\%9.2f ", pow(2.5,i));

****************************************************
___________________________
This message was ported from CCS's old forum
Original Post ID: 12512
Jerry
Guest







Re: pow(x,y) code replacement with qip(x,y) function
PostPosted: Mon Mar 10, 2003 7:54 pm     Reply with quote

I found that it did not paste all the code.

All I can see that is missing is.

#include <math.h> required for CCS's pow(x,y) function.

This is a keeper.



:=/*
:=I found this little snipet of code in the latest Dr. Dobbs Magazine to replace the f = pow(x,y) with f = qip(x,y).
:=
:=It has a limitation that y has to be a positive integer.
:=
:=It appears to be more accurate that CCS's pow() function. The qip(2.5, 5) function only 5\% rom, pow(2.5, 5) function 17\% rom.
:=
:=My example below with some results.
:=
:=*/
:=
:=// #define PIC_16F87X
:=#define PIC_18FXXX
:=
:=#define 98MHZCLOCK
:=// #define 8MHZCLOCK
:=
:=#case
:=
:=#IFDEF PIC_16F87X
:= #include <16F876.h>
:= #fuses hs, nowdt, noprotect, nolvp, put
:=// #device PIC16F876 *=16 icd=TRUE
:= #device PIC16F876 *=16
:= #device adc=8
:=#ENDIF
:=
:=
:=#IFDEF PIC_18FXXX
:= #include <18F252.h>
:= #include "defs_18f.h"
:= #fuses hs, nowdt, noprotect, nolvp, put, NOOSCSEN
:= #device PIC18F252 *=16
:=// #device PIC18F252 *=16 icd=TRUE
:= #device adc=8
:=
:= #build(reset=0x200)
:= #build(interrupt=0x208)
:= #org 0x0000,0x01ff
:= void bootloader() {
:= #asm
:= nop
:= #endasm
:=} // Reserve space for the bootloader
:=
:=#ENDIF
:=
:=
:=#include <math.h>
:=
:=#ifdef 8MHZCLOCK
:= #use delay(clock=8000000) /////////// Check Crystal Value ////////////
:=#endif
:=
:=#ifdef 98MHZCLOCK
:= #use delay(clock=9830400) /////////// Check Crystal Value ////////////
:=#endif
:=
:=#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
:=
:=/* Compute pow(x,n)
:= For positive Int n
:= through repeated squaring */
:=
:=float qip(float x, unsigned int n)
:={
:= float h = 1.0;
:=
:= while(n)
:= {
:= if (n & 1)
:= h *= x;
:=
:= x *= x;
:= n >>= 1;
:= }
:= return h;
:=}
:=
:=
:=main()
:={
:= unsigned int i;
:=
:=// printf("\%9.2f ", pow(2.5,i));
:=
:=
:= for (i = 10; i < 33; i++)
:= {
:= printf("\%9.2f ", qip(2,i));
:= printf("\%9.2f\n", pow(2,i));
:= }
:=
:= printf("\n");
:=
:=
:=
:= printf("x , y qip(x,y) pow(x,y) Calculator\n");
:= for (i = 18; i < 24; i++)
:= {
:= printf("2.5,\%d ", i);
:= printf("\%9.2f ", qip(2.5,i));
:= printf("\%9.2f\n", pow(2.5,i));
:= }
:=
:= printf("\n");
:=
:= printf("x , y qip(x,y) pow(x,y) Calculator\n");
:= for (i = 3; i < 9; i++)
:= {
:=// printf("\%9.2f \%9.2f\n", qip(12.1,i), pow(12.1,i)); // This does not work ???
:=
:= printf("12.1,\%d ", i);
:= printf("\%9.2f ", qip(12.1,i));
:= printf("\%9.2f\n", pow(12.1,i));
:=
:=
:= }
:= printf("\n");
:=
:= printf("x , y qip(x,y) pow(x,y) Calculator\n");
:= for (i = 7; i < 14; i++)
:= {
:= printf("5.75,\%d ", i);
:= printf("\%9.2f ", qip(5.75,i));
:= printf("\%9.2f\n", pow(5.75,i));
:= }
:=
:= printf("\n");
:=
:=
:= while(TRUE)
:= {
:= }
:=}
:=
:=/* RESULTS
:=
:=x , y qip(x,y) pow(x,y) Calculator
:=2.5,18 14551914.85 14551939.88 14551915.23
:=2.5,19 36379784.94 36379801.33 36379788.07
:=2.5,20 90949465.03 90949496.62 90949470.18
:=2.5,21 227373659.61 227373722.19 227373675.4
:=2.5,22 568434113.26 568435010.31 568434188.6
:=2.5,23 1421085424.72 1421087451.27 1421085472.0
:=
:=x , y qip(x,y) pow(x,y) Calculator
:=12.1,3 1771.56 1771.56 1771.561
:=12.1,4 21435.88 21435.89 21435.888
:=12.1,5 259374.24 259374.70 259374.246
:=12.1,6 3138428.30 3138430.01 3138428.377
:=12.1,7 37974984.64 37975004.91 37974983.36
:=12.1,8 459497258.06 459497588.87 459497298.6
:=
:=x , y qip(x,y) pow(x,y) Calculator
:=5.75,7 207814.03 207814.11 207814.0532
:=5.75,8 1194930.68 1194930.68 1194930.806
:=5.75,9 6870851.39 6870858.43 6870852.133
:=5.75,10 39507392.94 39507421.25 39507399.76
:=5.75,11 227167513.96 227167899.90 227167548.6
:=5.75,12 1306213229.89 1306213229.89 1306213405.0
:=5.75,13 7510725915.43 7510743737.22 7510727077.0
:=
:=================================================================
:=CCS PCH C Compiler, Version 3.146, 14129
:=
:=Filename: c:\program files\picc\fp-ttspi\powtest.LST
:=
:= ROM used: 1512 (5\%)
:= Largest free fragment is 30744
:= RAM used: 10 (1\%) at main() level
:= 28 (2\%) worst case
:= Stack: 2 locations
:=
:=only this output printf("\%9.2f ", qip(2.5,i));
:=================================================================
:=
:=****************************************************************
:=CCS PCH C Compiler, Version 3.146, 14129
:=
:=Filename: c:\program files\picc\fp-ttspi\powtest.LST
:=
:= ROM used: 5586 (17\%)
:= Largest free fragment is 26670
:= RAM used: 11 (1\%) at main() level
:= 63 (4\%) worst case
:= Stack: 3 locations
:=
:=only this output printf("\%9.2f ", pow(2.5,i));
:=
:=****************************************************************
:=*/
___________________________
This message was ported from CCS's old forum
Original Post ID: 12513
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group