Chip Directory
Mirror sites
Mailing list
Forum
Have this site translated
 Embedded Systems
Host site

Square and other roots


Date:    20010129
From:    Carlos Gomez Agis
To:      MC6811 Mailing List
Subject: Square root and trigonometric functions?

I'm working with l6 and 32bits numbers and I don't know what method should have to use to calculate a square root,sine and cosine.
I have been doing that using tables, but obviusly this is not a good manner to solve the problem, and I want to apply a method.
I hope you can give a method or if you have something done I'll appreciate your help.


Date:    20010129
From:    Al Williams
To:      MC6811 Mailing List
Subject: RE: Square root and trigonometric functions?

Do a Web search on Cordic and another one on Newtonian approximation.

Another alternative is a coprocessor
(www.al-williams.com/awce/pak1.htm).
We've used these with 68HC11s successfully.


Date:    20010130
From:    Dimiter Popoff
To:      MC6811 Mailing List
Subject: RE: Square root and trigonometric functions?

For square root, try transgalactic.freeyellow.com/cpu32/sqrt.txt .

The algorithm is much simpler than others I have seen - and faster, needs no division (for the square root of a 32-bit number you'll have to do 16 16-bit multiplications). The sample code is for CPU32, but it is also described in plain words. You'll probably not see it elsewhere because I made it up 3-4 years ago when I needed square root.


Date:    20010130
From:    Tom Schmitt*
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

When I need some method of calculation I always try "Handbook of Mathenmatical Functions" by Abramowitz and Hill. It is now a Dover book meaning not too expensive. It should be in most libraries.
Dimeter's square root method may be easier on a 68HC11 than those in the book though.


Date:    20010130
From:    Scott Grodevant
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

I can't speak for Sin and Cos, but here's a square root algorithm I got some time ago from Motorola's BBS.

// Compute square root of an unsigned long n, returned as an unsigned int.
// The algorithm comes from Motorola's Freeware BBS 512 891-3733.
unsigned sqrtu( unsigned long n )
{
  unsigned  long rem, quo, tmp;
  unsigned  ctr;

  rem = quo = 0;

  for ( ctr = 0; ctr < 16; ctr ++ ) {
    rem <<= 1;
    if ( n & 0x80000000L ) rem ++;
    n <<= 1;
    rem <<= 1;
    if ( n & 0x80000000L ) rem ++;
    n <<= 1;
    quo = ( quo << 1 ) + 1;
    tmp = rem - quo;
    if ( rem < quo )
      quo --;
    else {
      rem = tmp;
      quo ++;
    }
  }
  return( (unsigned) ( quo >> 1 ) );
}

Date:    20010130
From:    Ed Taylor
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

there is a complete floating point package written by gordon doughman (sp?) in assembler. i got it from motorola years ago and it's slick.


Date:    20010130
From:    Thomas D. Dean
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

The package is available on the Motorola site, titled MATH11.ARC.


Date:    20010130
From:    Mike McCarty
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

On Mon, 29 Jan 2001, Carlos Gomez Agis wrote:

I'm working with l6 and 32bits numbers and I don't know what method should have to use to calculate a square root,sine and cosine.

Unfortunately, you do not specify your requirements precisely enough for us to advise you. You are working with 16 bits and 32 bits, what? Floating point? Fixed point? What is your representation? How much precision do you need? How much accuracy? Any time related requirements? Callable from what languages?

I recently saw some information in "Floating Point Approximations for Embedded Systems" which failed miserably a suite of tests I ran it through. It claims to provide approximations for

Function  Decimals
sin       3.2, 5.2, 7.3, 12.1, 14.7, 20.2, 23.1
cos       3.2, 5.2, 7.3, 12.1, 14.7, 20.2, 23.1
tan       3.2, 5.6, 8.2, 14.1, 20.3, 23.6
atan      6.6, 13.7

He recommends computing asin from atan, a procedure which is egregiously wrong for floating point computations.

In any case, I tested sin and cos for 3.2, 5.2, 7.3, and 12.1 decimals. I found that all of them badly failed the tests I put them through (adaptations of Cody and Waite's tests). Each of them actually achieved 0 bits of accuracy. I believe that this is because those routines attempt to reduce absolute error rather than relative error (the tan and atan attempt to reduce relative error, but I did not check them; it is not clear whether the atan functions attempt to reduce relative or absolute error). This may be acceptable for fixed point, so if you are using fixed point, this package may be something you could use. But this type of error is disastrous for floating point, which should be based on relative error. I saw errors of at least four orders of magnitude in some of the computed values (IOW, [computedsin(x) - sin(x)]/sin(x) ~ 10^4), which indicates that there were no correct bits actually computed. Also, sin(x) is not approximately x for small x, again relative errors of orders of magnitude were observed. Also, sin(-x) is not equal to -sin(x). Try looking at www.ganssle.com/

I also recently acquired a copy of fdlibm. All the routines supplied (the full Standard C Library, I believe) passed the tests with flying colors. It assumes IEEE floats, however, and some of the routines are very definitely dependent on that format. Only one precision level is supplied, approximately 16 decimal digits. Also the square root routine is pretty slow, though it requires no division, and is quite accurate, never losing even one bit of accuracy.

Plaugher's library (from his book) also passes with flying colors, as does the gcc library. Plaugher's library requires licensing, and the gcc library has other restrictions which may affect your use of it. Both of them assume IEEE, I believe.

Jack Crenshaw published several years ago in "Embedded Systems Programming" a suite of functions for fixed point which are simple and presumably fast, but have no basis in theory to show that they are good, nor have I ever tested them.


Date:    20010130
From:    Art Burke
To:      MC6811 Mailing List
Subject: RE: Square root and trigonometric functions?

sin(-x) does = -sin(x)...It is an odd function...

Date:    20010130
From:    Mike McCarty
To:      MC6811 Mailing List
Subject: RE: Square root and trigonometric functions?

On Tue, 30 Jan 2001, Art Burke wrote:

sin(-x) does = -sin(x)...It is an odd function...

Precisely. But this implementation does not satisfy that relation. That is one of my complaints against it.


Date:    20010201
From:    Max Mastail
Organization: ifremer
To:      MC6811 Mailing List
Subject: Re: Square root and trigonometric functions?

For sinus fonction, if you use the taylor series:

/***********************************************
  sin(x) = x-x3/3!+x5/5!-x7/7!+x9/9!-+...
 *************************************************/
double sinM(double grad)
{
   double x = grad;//=2*PI*grad/360;
   double Result;
   double R1=0.0;
   double R3=0.0;
   double R5=0.0;
   double R7=0.0;
   double R9=0.0;
   R1 = x;
   R3 = x*x*x /(1*2*3);
   R5 = x*x*x*x*x /(1*2*3*4*5);
   R7 = x*x*x*x*x*x*x/(1*2*3*4*5*6*7);
   R9 = x*x*x*x*x*x*x*x*x/(1*2*3*4*5*6*7*8*9);  // enough i think..
   double R11 = x*x*x*x*x*x*x*x*x*x*x/(1*2*3*4*5*6*7*8*9*10*11);
   return Result =R1-R3+R5-R7+R9-R11;
}

Use like this:
printf("sinus 0.3840)=%le \n\n",sinM(0.3840));

I think the code is good, but verify it. You can modulate float and the number of term. For verification you can consult this link:
www.sisweb.com/math/trig/tables.htm
You can also write your table with the precision that you want...


Ad for PCI video capturing boards by Dektec.
Goto: Main Mirror About Author
Register: Yourself Company
Feedback: Correction Addition Question Forum
Request quote: Chips (Deutsch) Chips (English) Chips (Nederlands)

Advertisement by Adprov

Viewable with any browser


Site version is  Version GIF
Page last edited on 20010813
Page was never integrally checked for link correctness