Algorithms for calculating the built-in functions

Algorithms for calculating the following eleven functions are built in to the computer chips used by most scientific calculators and by the Algebra Coach:
  sine (sin)   cosine (cos)   tangent (tan)
  arcsine (arcsin)   arccosine (arccos)          arctangent (arctan)
  base 10 logarithm (log)   natural logarithm (ln)   exponential with base e (exp or e x )
  square root (sqrt)     power ( y x )  
This section explains the algorithms. Studying the algorithms is educational because you learn a lot about the symmetries, identities and other properties of the functions and how they are exploited to make accurate evaluations of the functions.



Algorithm for calculating sin (x)

This algorithm makes it possible for the sine of any angle to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to use a polynomial approximation (step 4) to calculate the sine an angle x. But because this approximation is only accurate for small x we must take steps (1 to 3) to reduce x to the smallest angle that still has the same value of the sine function.
  1. Use periodicity: If x is in the range 0≤x≤360° then go on to step 2. Otherwise, reduce x so that it lies in the range 0≤x≤360° by adding or subtracting a suitable multiple of 360° from it (we are assuming that angle x is measured in degrees). This new x has the same value of the sine function that the original x has.

  2. Use symmetry: If x is in quadrant 1 then go on to step 3. Otherwise if it is in quadrant 2 call it x2, if it is in quadrant 3 call it x3, and if it is in quadrant 4 call it x4.

    The graph shows that four angles: x, x2, x3, and x4, all have the same value of the sine function (except for a − sign.) Thus instead of calculating the sine of x2, x3, or x4 we can calculate the sine of x (the red one in quadrant 1) and just attach a − sign to the answer if the angle was in quadrant 3 or 4. Here are some examples. In each case the quadrant 1 angle is found by using the symmetry of the sine function:


  3. Use the cofunction: The argument x has now been reduced to be between 0° and 90°. If it is smaller than 45° then go on to step 4. Otherwise it can be made even smaller by using the following trick: instead of calculating sin(x) we calculate cos(90° − x). This picture shows why we can replace the sine of a big angle, B, by the cosine of a small angle, S:

         
    Set x to the small angle S. Angle x is now small enough to apply the polynomial approximation. Click here to use the cosine polynomial, or proceed to step 4 to use the sine polynomial.

  4. Use the sine polynomial: If angle x is in degrees then convert it to radians by multiplying it by π/180. Then substitute x into the formula:
    For x ≤ π/4 radians (i.e. 45°) this polynomial is accurate to within ±0.00004.


Example: calculate sin (565°). Here are the steps
  1. = sin (205°)       ← use the periodicity of the sin function
  2. = − sin (25°)       ← use the symmetry of the sin function
  3. nothing to do; the argument is already less than 45°
  4.       ← 25° = 0.4363 radians


Example: calculate sin (1.830 radians). Here are the steps
  1. nothing to do; the argument is already between 0 and 2π radians
  2. = sin (1.312)       ← use the symmetry of the sin function
  3. = cos(0.2592)       ← use the cofunction because the argument was greater than 45°
  4.       ← use the cosine polynomial


Algorithm for calculating cos (x)

This algorithm makes it possible for the cosine of any angle to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to use a polynomial approximation (step 4) to calculate the cosine an angle x. But because this approximation is only accurate for small x we must take steps (1 to 3) to reduce x to the smallest angle that still has the same value of the cosine function.
  1. Use periodicity: If x is in the range 0≤x≤360° then go on to step 2. Otherwise, reduce x so that it lies in the range 0≤x≤360° by adding or subtracting a suitable multiple of 360° from it (we are assuming that angle x is measured in degrees). This new x has the same value of the cosine function that the original x has.

  2. Use symmetry: If x is in quadrant 1 then go on to step 3. Otherwise if it is in quadrant 2 call it x2, if it is in quadrant 3 call it x3, and if it is in quadrant 4 call it x4.

    The graph shows that four angles: x, x2, x3, and x4, all have the same value of the cosine function (except for a − sign.) Thus instead of calculating the cosine of x2, x3, or x4 we can calculate the cosine of x (the red one in quadrant 1) and just attach a − sign to the answer if the angle was in quadrant 2 or 3. Here are some examples. In each case the quadrant 1 angle is found by using the symmetry of the cosine function:


  3. Use the cofunction: The argument x has now been reduced to be between 0° and 90°. If it is smaller than 45° then go on to step 4. Otherwise it can be made even smaller by using the following trick: instead of calculating cos(x) we calculate sin(90° − x). This picture shows why we can replace the cosine of a big angle, B, by the sine of a small angle, S:

         
    Set x to the small angle S. Angle x is now small enough to apply the polynomial approximation. Click here to use the sine polynomial, or proceed to step 4 to use the cosine polynomial.

  4. Use the cosine polynomial: If angle x is in degrees then convert it to radians by multiplying it by π/180. Then substitute x into the formula:
    For x ≤ π/4 radians (i.e. 45°) this polynomial is accurate to within ±0.000004.


Example: calculate cos (565°). Here are the steps
  1. = cos (205°)       ← use the periodicity of the cosine function
  2. = − cos (25°)       ← use the symmetry of the cosine function
  3. nothing to do; the argument is already less than 45°
  4.       ← 25° = 0.4363 radians


Example: calculate cos (1.830 radians). Here are the steps
  1. nothing to do; the argument is already between 0 and 2π radians
  2. = −cos (1.312)       ← use the symmetry of the cosine function
  3. = −sin(0.2592)       ← use the cofunction because the argument was greater than 45°
  4.       ← use the sine polynomial


Algorithm for calculating tan (x)

This algorithm makes it possible for the tangent of any angle to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to use a polynomial approximation (step 5) to calculate the tangent an angle x. But because this approximation is only accurate for small x we must take steps (1 to 4) to reduce x to the smallest angle that still has the same value of the tangent function.
  1. Use periodicity: If x is in the range 0≤x≤180° then go on to step 2. Otherwise, reduce x so that it lies in the range 0≤x≤180° by adding or subtracting a suitable multiple of 180° from it (we are assuming that angle x is measured in degrees). This new x has the same value of the tangent function that the original x has.

  2. Use symmetry: If x is in quadrant 1 then go on to step 3. Otherwise it is in quadrant 2, in which case we will call it x2. The graph shows that, except for a − sign, x and x2 have the same value of the tangent function. Thus instead of calculating the tangent of x2 we can calculate the tangent of x (the red one in quadrant 1) and just attach a − sign to the answer.

    The quadrant 1 angle is found by using the symmetry of the tangent function. For example tan (110°) = − tan (70°).

  3. Use the cofunction: The argument x has now been reduced to be between 0° and 90°. If it is smaller than 45° then go on to step 4. Otherwise it can be made even smaller by using the following trick: instead of calculating tan(x) we calculate tan(90° − x) and take the reciprocal. This picture shows why we can replace the tangent of a big angle, B, by the reciprocal of the tangent of a small angle, S:

         
    Set x to the small angle S. x is now less than 45°.

  4. Use a trigonometric identity: If x is less than 22.5° then go on to step 5. Otherwise use this trig identity:
    to replace a calculation of the tangent of some angle by a calculation involving an angle that is half as big. The argument of the tangent function is now less than 22.5°.

  5. Use the polynomial: If angle x is in degrees then convert it to radians by multiplying it by π/180. Then substitute x into the formula:
    For x ≤ π/8 radians (i.e. 22.5°) this polynomial is accurate to within ±0.000006.


Example: calculate tan (194°). Here are the steps
  1. = tan (14°)       ← use the periodicity of the tangent function
  2. nothing to do; the argument is already between 0 and 90°
  3. nothing to do; the argument is already between 0 and 45°
  4. nothing to do; the argument is already between 0 and 22.5°
  5.       ← 14° = 0.2443 radians


Example: calculate tan (110°). Here are the steps
  1. nothing to do; the argument is already between 0 and 180°
  2. = − tan (70°)       ← use the symmetry of the tangent function
  3. = − 1 / tan(20°)       ← use the “cofunction” because the argument was greater than 45°
  4. nothing to do; the argument is already less than 22.5°
  5.       ← 20° = 0.3491 radians


Example: calculate tan (40°). Here are the steps
  1. nothing to do; the argument is already between 0 and 180°
  2. nothing to do; the argument is already between 0 and 90°
  3. nothing to do; the argument is already between 0 and 45°
  4. use the trig identity
  5. Calculate the polynomial for tan (20°) using 20° = 0.3491 radians:
    and substitute the result into the trig identity of step 4:


Algorithms for calculating arcsin (x) and arccos (x)

These algorithms use the algorithm for the arctangent function. Click here to see that algorithm.

arcsine: Consider the triangle shown to the right. Notice that both of the following statements describe the angle θ:
Putting them together we get the equation:
We can use this equation to replace the calculation of the arcsine of some quantity x by the calculation of the arctangent of some related quantity. Click here to see the arctangent algorithm.

arccosine: Look at the triangle again. Notice that both of the following statements describe the angle φ:
Putting them together we get the equation:
We can use this equation to replace the calculation of the arccosine of some quantity x by the calculation of the arctangent of some related quantity. Click here to see the arctangent algorithm.





Example: Consider the right triangle shown here.

We can calculate angle θ using the arcsine:
Alternatively we can calculate angle θ using the arccosine:
In either case we have reduced the problem to calculating arctangent. Using the algorithm for calculating the arctangent (given below), this becomes:



Algorithm for calculating arctan (x)

This algorithm makes it possible for the arctangent of any number to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to to use a polynomial approximation (step 4) to calculate the arctan(x). But because this approximation is only accurate for small arguments x we must take several steps (1 to 3) to reduce x to a smaller value. We assume that the desired output of the arctangent function is an angle in radians.
  1. Use symmetry: If the argument x is positive then go on to step 2. Otherwise use the fact that arctan(−x) = −arctan(x) to make the argument positive.

  2. Use the complementary angle: If the argument x is less than 1 then go on to step 3. Otherwise use the fact that:
    to replace the arctangent of a value bigger than 1 by the arctangent of a value smaller than 1. This equation follows from the fact that angles B and S in the triangle shown are given by:

         

    and that .

  3. Use an identity: If x (about 0.268) then go on to step 3. Otherwise use the identity:
    to replace the arctangent of a value x bigger than by the arctangent of a value smaller than .

  4. Use the polynomial: Substitute the argument x into the formula:
    For x (about 0.268) this polynomial gives an angle in radians that is accurate to within ±0.00002. To convert this angle to degrees multiply it by 180/π.



Example: Calculate arctan (0.7813) and express the answer in degrees. Here are the steps:
  1. nothing to do; the argument is already positive
  2. nothing to do; the argument is already less than 1
  3. use the identity:
  4. use the polynomial to evaluate the new arctangent:
    Substitute this back into the original arctangent and then convert the answer to degrees:



Algorithm for calculating log (x)

This algorithm makes it possible for the base 10 logarithm (log) of any positive number to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to to use a polynomial approximation (step 4) to calculate log(x). But because this approximation is only accurate for small arguments x we must take several steps (1 to 3) to reduce x to a smaller value.
  1. Express the number x in scientific notation:   x = m · 10 p.   Then
    log (x) = log (m) + p,
    where p is an integer and m (the new argument of the base 10 logarithm function) is a number between 1 and 10.

  2. Use the square root: Use the fact that:
    to reduce the argument of the base 10 logarithm function even further, to a value between 1 and .

  3. Replace log by ln: Use the change of base formula:
    log (x) = 0.43429448 ln (x)
    (This follows from the fact that 1 / ln (10) = 0.43429448). Putting steps 1, 2 and 3 together gives:
  4. Use the polynomial: For any argument x between 1 and , the following polynomial:
    where , accurately gives ln (x) to within ±0.0008.



Example: Calculate log (900). Here are the steps:
  1. 900 = 9 · 10 2  so  log (900) = log (9) + 2
  2. log (900) = 2 log (3) + 2
  3. log (900) = 0.86858896 ln (3) + 2
  4. use the polynomial to evaluate ln (3).
    x = 3 gives y = 2/4 = 0.5 so the polynomial becomes:
    Substitute this back into the result from step 3:
    log (900) = 0.86858896 · 1.098 + 2 = 2.954



Algorithm for calculating ln (x)

This algorithm makes it possible for the natural logarithm (ln) of any positive number to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to to use a polynomial approximation in step 3 to calculate ln (x). But because this approximation is only accurate for small arguments x we must take steps 1 and 2 to reduce x to a smaller value.
  1. Express the number x in scientific notation:   x = m · 10 p.   Then
    ln (x) = ln (m) + 2.3025851 p,
    where p is an integer and m (the new argument of the natural logarithm function) is a number between 1 and 10.
    (Note that ln (10) = 2.3025851)

  2. Use the square root: Use the fact that:
    to reduce the argument of the natural logarithm function even further, to a value between 1 and .
    Putting steps 1 and 2 together gives:
  3. Use the polynomial: For any argument x between 1 and , the following polynomial:
    where , accurately gives ln (x) to within ±0.0008.



Example: Calculate ln (9000). Here are the steps:
  1. 9000 = 9 · 10 3  so  ln (9000) = ln (9) + 3 · 2.3025851
  2. ln (9000) = 2 · ln (3) + 3 · 2.3025851
  3. use the polynomial to evaluate ln (3).
    x = 3 gives y = 2/4 = 0.5 so the polynomial becomes:
    Substitute this back into the result from step 2:
    ln (9000) = 2 · 1.098 + 3 · 2.3025851 = 9.104



Algorithm for calculating exp (x) or e x

This algorithm makes it possible for exp (x) or e x to be calculated using only the operations of addition, subtraction, multiplication and division. The basic idea is to to use a polynomial approximation in step 3 to calculate e x. But because this approximation is only accurate for small arguments x we must take steps 1 and 2 to reduce x to a smaller value.
  1. Split up x: Write x = n + r, where n is the nearest integer to x and r is a real number between −½ and +½. Then e x = e n · e r.

  2. Evaluate e n: Multiply the number e by itself n times. To 14 digits, e = 2.7182818284590. The multiplication can be done quite efficiently. For example e 8 can be evaluated with just 3 multiplications if it is written as (((e) 2 ) 2 ) 2. To further increase efficiency various integer powers of e can be calculated once and stored in a lookup table.

  3. Evaluate e r using the polynomial:
    For r between −½ and +½ this polynomial is accurate to within ±0.00003.



Example: Calculate exp (−3.8) or e −3.8 :
  1. Split it into two factors:
  2. Evaluate the first factor using multiplication:
  3. Evaluate the second factor using the polynomial:
    Multiply the two factors to get the answer:



Algorithm for calculating the square root

This algorithm uses Newton's method. It makes it possible for the square root of any positive number to be calculated using only the operations of addition, subtraction, multiplication and division.

The basic idea is this: if we want to calculate the square root of some number m then we draw a graph of the function y = x 2m, as shown in the picture to the right, and find the point where the graph crosses the x axis. This is the red dot at x = .

Newton's method for finding this point starts with any initial estimate of the square root, indicated by the point x1 in the picture. It uses the height and slope of the of the curve at x1 (the blue dot and the red dashed line) to obtain a better estimate, x2, of the square root. This better estimate is then used to get an even better estimate, x3, and so on. Each iteration (cycle) doubles the number of digits of accuracy of the square root. When two successive estimates of the square root differ by less than some small value, say 0.0001, then the algorithm stops and we have the square root, accurate to within ±0.0001.

Given any estimate x1 of the square root of m we can obtain the improved estimate x2 by using the formula:
This formula is easily derived by solving the equation:
for x2. This equation is just a statement that the slope of the curve at the blue dot (which is derived using calculus) equals the slope of the slanted side of the yellow triangle.




Example: Calculate the square root of 900 accurate to ±0.000001.
Starting with an initial guess of 50 here are the results of the first 6 iterations:
x1 50
x2
x3 30.2352941176471
x4 30.0009155413138
x5 30.0000000139698
x6 30.0000000000000
We have shown the calculation for x2 in detail. We have also shown the 6th decimal place in red. On the 6th iteration there is no change in this decimal place so we can stop and give x6 as the answer. Since we know that the exact answer is 30 we can see how the number of correct digits roughly doubles with every iteration.




Algorithm for calculating the power function

We can use the following equation to replace the calculation of the power function with a calculation of the natural logarithm and the e x function:
This equation follows from the identity e ln (x) = x and the exponention property of exponents.



Example: Evaluate 5.2 3.6





If you found this page in a web search you won’t see the
Table of Contents in the frame on the left.
Click here to display it.