Splitting result into different varibles for Maclaurin expansion

This is a discussion on Splitting result into different varibles for Maclaurin expansion within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Brother_Sharp This is supposed to make you use series like x[n] in order to divide a result ...

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Brother_Sharp View Post
    This is supposed to make you use series like x[n] in order to divide a result into multiple parts and use functions to calculate the results and such.

    According to it,
    I need to find the specific part of the Maclaurin's Expansion and calculate it.

    So the X in e=1+x+(1/2!)*x and so on is always 1
    Giving us e=1+1+1/2!+1/3!+1/n! to calculate

    The program should calculate it in order of the N

    so If N is 1 it will calculate only the corresponding factorial division part;
    meaning that one part of the variable will hold the result of the calculation which will be x=1.00000000~ and the other will hold the actual sum up until now which is e=2.000000~

    For N=2
    x=1/2!, e=previous e+x

    for N=3
    x=1/3!, e=previous e+x

    each time the result is calculated, it needs to hold all the numbers after the dot into separate variables like x[1] x[2] x[3] until all the 30~35 digits are filled with them.
    so when printing out, in the case of N=2

    x[0].x[1]x[2]x[3]~
    should come out as
    0.50000000000000000000
    where x[0] should hold the value above the dot and x[1~3] would be holding the rest in 5 digits each.

    Well yeah Sorry if my explanation sucks but This is what its asking.
    And I cant use bigint as it defeats the purpouse
    I'm with tabstop that you're misunderstanding what's being asked. Can you copy and paste the exact text of the question?

    This part :
    meaning that one part of the variable will hold the result of the calculation which will be x=1.00000000~ and the other will hold the actual sum up until now which is e=2.000000~
    Makes me think you have two variables. One's the sum so far. The other is X = 1/n! for the previous value of n. To get the next sum, do

    Code:
    X /= n;
    sum += other;
    n += 1
    That is, each x[N] isn't a discrete set of digits that gets mushed together to form the final output, it's then next value you add to sum. Each x[N] has digits which contribute to change many of the digits instead of your idea that x[1] is digits 1-5 after the decimal point and the other x[] values have nothing at all to do with them.

    x[0] = 1 = 1.00000
    x[1] = 1 = 1.00000
    x[2] = 1/2 = 0.50000
    x[3] = 1/6 = 0.16667
    x[4] = 1/24 = 0.04167

    Note how both x[0] and x[1] change the integer part of the sum, and x[2] and x[3] both contribute to the digit right after the decimal point (the tenth's place) while x[3] and x[4] both contribute to change every digit in the fractional part (.16666 + 0.4167 = .20833 so every digit is different than before). You can't just line up x[0].x[1]x[2]x[3]x[4] and get 1.100000500001666704167, you have to add up each of the terms to get the final result.

    To say it a different way : read through the assignment and notice it isn't telling you to keep an array of X values, just one. It's telling you to keep track of exactly two values : the sum so far and 1/N! for the current value of N. Why do you need an array of 5 values plus a sum to hold two items?

    Also, does it specifically say 30-35 digits, or 30-35 iterations of the loop (i.e. N can go up to 30)? There's a big difference between the two. There's no reason to assume each iteration series will give you exactly one digit of accuracy. That's easy to see since doing 1 iteration gives you 1 as the result, so you need two iterations to get the first digit correct. If you're supposed to run 30 iterations to get 15 digits that'll fit in a double - no need for arbitrary precision numbers.

  2. #17
    Registered User
    Join Date
    May 2011
    Posts
    9
    After poking around this some more, it appears that this is done by using the following.
    Code:
    for (int J=0; J<8; J++)
    {
    if (J<7)
       {
       a[j+1]+=(a[j]%n)*10000
        }
    
    a[j]=a[j]/n
    }

    But I cant figure out what happens if you run that.
    I know that a[] is the one that holds the current decimal value up to 7 parts each having 5 digits.
    and that N is the current power which goes only up to 30.
    Still cant understand why this is the main part of this program
    Last edited by Brother_Sharp; 07-17-2011 at 04:01 AM.

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    9
    Er, the capital J is a typo, its supposed to be a lowercase J.

    Then I realized I needed the division part by N to be with the factorial so I edited it a bit when conpiled.

    So I tried running the above one but,
    On the 3rd power I keep getting 16666166661666616666 instead of 1666666666666666


    Disregard that too,
    I managed to get 166666666666666666 by using the factorial of N for both the Ns there.

    The other problem I have is, while doing the operations, it goes well till the 7th.
    Starting from the 8th and so on It wont continue without giving me negative numbers.

    for N=8
    It should be 00002480158730158730158730.
    Instead I get 00002 48015 -19220 -41904 30331 53015 -19220

    That is obviously due to int's limit and since at that part it does
    1936000000%40320
    in order to get a[3]'s value which then is 35200 which is then multiplied by 100000
    giving us a 3520000000/40320, though the value of a[3] exceeds the limit of integer, any way to fix this?
    I cannot use doubles or Bigints for this so if anyone has a workaround for this, it would be appreciated.
    Last edited by Brother_Sharp; 07-17-2011 at 05:12 AM.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Brother_Sharp View Post
    in order to get a[3]'s value which then is 35200 which is then multiplied by 100000
    giving us a 3520000000/40320, though the value of a[3] exceeds the limit of integer, any way to fix this?
    Use unsigned int, unsigned long or unsigned long long as appropriate.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 11:20 AM
  2. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  3. Macro expansion
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 11-02-2007, 03:08 AM
  4. taylor series expansion
    By noor_mirza in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 10:02 PM
  5. Expansion
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-02-2001, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21