Regarding sumation of floating points

Discussion in 'C' started by joeserhal, Sep 16, 2008.

  1. joeserhal

    joeserhal New Member

    Joined:
    Feb 13, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Hi there,
    I have a question about coding with single precision floating point numbers (i.e, float)...how can I sum a certain number of floating pt #s within an interval...let's say I want to add a 100 numbers, starting from 1 within the interval [1,2) ? :confused:

    Anybody knows how can this be done?

    Any info would be greatly appreciated! ;)
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How about something like the following:
    Code:
    float total=0;
    for (int i=0; i<100; i++)
      total+= some_number();
    
     
  3. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    I think he meant something more like this:
    Code:
      float x;
      float sum = 0.0;
      for (x = 1.00; x < 2.00; x += 0.01)
        sum += x;
      printf ("%.2f\n", sum); // this rounds the result
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice