+ Reply to Thread
Results 1 to 7 of 7

code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

  1. #1
    Registered User
    Join Date
    11-09-2014
    Location
    drenthe
    MS-Off Ver
    2013
    Posts
    5

    code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    Hello,

    I would like some help with the following.

    I'm working on an assignment in VBA code.

    The results must be calculated from a set of numbers. The calculation starts with 1 and then it goes on with ½ + ¼ + 1/8 + 1/16.....
    So the assignment is: Calculate 1 + ½ + ¼ + 1/8 + 1/16.....
    Ask the user through msgbox beforehand how many terms must be calculated.

    What is the best way to write this code?

    With kind regards,

    Peter

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    We don't do homework, but will help if you have a specific and narrow question, and show some effort of your own.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-09-2014
    Location
    drenthe
    MS-Off Ver
    2013
    Posts
    5

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    Thanks for the response.

    This is not homework. I try to develop myself in VBA code for my job/career. In my job I come across VBA code more and more.

    What I had so far is (some things are in Dutch):
    -------

    Sub reeksdelen()
    'Bereken de volgende reeks 1 + ½ + ¼ + 1/8 + 1/16 …'
    'Vraag vooraf aan de gebruiker hoeveel termen er moeten opgeteld worden. Druk het resultaat af.'

    Dim imput As Byte, som As Double, teller As Single

    teller = 0.5

    imput = InputBox("hoeveel termen moeten er opgeteld worden?")

    teller = 1

    Do While invoer = 0


    som = 1 + teller + invoer

    Loop

    MsgBox (som)

    End Sub

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    Please Login or Register  to view this content.
    Last edited by shg; 11-09-2014 at 05:37 PM.

  5. #5
    Registered User
    Join Date
    11-09-2014
    Location
    drenthe
    MS-Off Ver
    2013
    Posts
    5

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    Thank you very much!

    Now I can carry on and I get what my mistake was.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    You're welcome.

    As the sum of a power series, you can also do this without a loop:

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: code for Calculate the following series 1 + ½ + ¼ + 1/8 + 1/16 ... "

    [This forum's editor has problems with quoting some content. So I had to re-enter the "quoted" part. I took the liberty of translating some of the Dutch.]
    Quote Originally Posted by petrwurfs View Post
    This is not homework. I try to develop myself in VBA code for my job/career. In my job I come across VBA code more and more. What I had so far is (some things are in Dutch):
    Please Login or Register  to view this content.
    Some binary number theory might help.

    First, Excel and VBA use 64-bit binary floating-point to represent numbers. Consequently, the series is limited to 1 + 1/2 + 1/4 + ... + 1/2^52 if you want exact results.

    Second, if "imput" is n (1 to 53), the sum of the series is simply 1 + (1 - 2^-(n-1)) [1], which is 2 - 2^-(n-1). So the VBA implementation can be simplified to:
    Please Login or Register  to view this content.
    Caveat: Excel and VBA formatting is limited to 15 significant digits. So some values of "som" will not appear exactly. For example, when "imput" is 53, "som" will appear to be 2. But it is actually 1.99999999999999,97779553950749686919152736663818359375 as intended. (I use period for the decimal point and comma to demarcate 15 significant digits.)

    A subtle detail.... Generally, it is better to use type Long for integers and type Double for non-integers in order to avoid arithmetic limitations.

    For example, type Byte cannot be negative, and it is limited to 255. You might have intended to take advantage of that to limit the range of input values. But exceeding those limits would result in VBA programming errors, which is not a good way to handle user errors.

    And with type Single, you lose precision, which further limits the size of "imput" if you want exact results.

    [EDIT] As a teachable moment, I was going to discuss the loop implementation, even though it is unnecessary. But I see that shg already provided an example, albeit without explanation.


    -----
    [1] 2 - 2^-52 is computable despite 64-bit binary floating-point limitations because the arithmetic actually uses an 80-bit binary floating-point representation in Intel-compatible computers, which I believe is all PCs and Macs these days.
    Last edited by joeu2004; 11-09-2014 at 06:20 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2014, 05:03 AM
  2. How to get only "Sunday" highlighted in a series of dates in "cloumns"
    By Anuru in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-20-2014, 02:56 AM
  3. [SOLVED] Converting values from a "1,2,3,..." series to "10,12,14..." series
    By Equipoise in forum Excel General
    Replies: 3
    Last Post: 07-19-2013, 03:30 AM
  4. Replies: 1
    Last Post: 03-08-2013, 05:20 AM
  5. [SOLVED] algebraic formula to calculate "net uniform series" (NUS)
    By Johnny Logan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1