The Math Forum

Ask Dr. Math - Questions and Answers from our Archives
Associated Topics || Dr. Math Home || Search Dr. Math

Writing an Arcsin Function in C++

Date: 04/17/2002 at 19:17:56
From: Matt
Subject: Writing an arcsin function in C++


I'm taking a computer science class, and I'm stuck on some homework.
I have to write a function that computes the arcsin of an input. The 
book gives this formula:

arcsin x = x + (x^3)/3! + x^5 * (3^2)/5! + x^7 * 3^2 * (5^2)/7! + x^9 
* 3^2 * 5^2 * (7^2)/9! +...

The problem is that I have to do this until a newterm is < 10^-6.

What I'm stuck on is the formula. I have to break down the formula 
above, and write the function. It's Computer Science 111, so I'm just 
learning C++. The most advanced thing I can use right now is loops, so 
I was planning on breaking the formula down and using loops to repeat 
until that 10^-6 marker, but I can't seem to break it down properly. I 
see a pattern, but the beginning is throwing me.

I'd appreciate any help...

Thanks a lot. :)

Date: 04/18/2002 at 17:52:15
From: Doctor Jeremiah
Subject: Re: Writing an arcsin function in C++

Hey Matt,

Cool assignment.

There are two ways: loops and function calls (the function calls can 
be used recursively). Since you know about loops and calling functions
we could use either, so I will use both.

Say we have a function called newterm() that makes a term given the 
term number:

arcsin x = x^1 / 1!                 (term 1)
         + x^3 / 3! * (1)^2         (term 2)
         + x^5 / 5! * (1*3)^2       (term 3)
         + x^7 / 7! * (1*3*5)^2     (term 4)
         + x^9 / 9! * (1*3*5*7)^2   (term 5)
         + ...
         + x^(2k-1) / (2k-1)!
           * (product of odd numbers less than 2k-1)^2   (term k)

The exponent is the kth odd number. Odd numbers have the form 2k-1 so 
when k = 5, for example, the exponent will be 2(5)-1 = 5.

To calculate an exponent we multiply the base times itself a certain 
number of times. We will have a function like:

double power(double base,long exponent)
   // start with power=base and then
   // use the loop to multiply it the
   // required number of times
   double power=base;
   for(long i=1;i<exponent;i++)

   return power;

The factorial is the product of all the values less than a number and 
greater than one. As a recursive function we could do something like:

long factorial(long value)
   // stop when we get to 1
   if (value < 2) return 1;

   // otherwise multiply by the number
   // that is one lower
   return value * factorial(value - 1);

So putting in factorial(3) would return 3*2*1 = 6

The product of odd numbers is just as easy:

long oddfactorial(long value)
   // stop when we get to 1
   if (value < 2) return 1;

   // otherwise multiply by the number
   // that is two lower (next odd number)
   return value * oddfactorial(value - 2);

So putting in oddfactorial(7) would return 7*5*3*1 = 105

Then our function newterm() would look like:

double newterm(double value,long term)
   long oddnum = 2*term-1;

   // the reason we use oddfactorial(oddnum-2)
   // is because we don't want to include the
   // odd number itself, just the ones below it
   return power(value,oddnum) / factorial(oddnum)
      * oddfactorial(oddnum-2) * oddfactorial(oddnum-2);

Then the main function must add the terms together. A pretty simple 
while loop. The while loop just adds the term and checks it see how 
small it is.

Let me know if you need more info on what I did here. If you aren't 
allowed to use recursion you will have to change factorial and 
oddfactorial to use loops. If you need help with that let me know.

- Doctor Jeremiah, The Math Forum 
Associated Topics:
High School Calculators, Computers

Search the Dr. Math Library:

Find items containing (put spaces between keywords):
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

Math Forum Home || Math Library || Quick Reference || Math Forum Search

Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.