Regarding sumation of floating points

Go4Expert Member
16Sep2008,11:44   #1
joeserhal's Avatar
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) ?

Anybody knows how can this be done?

Any info would be greatly appreciated!
Mentor
17Sep2008,05:28   #2
xpi0t0s's Avatar
How about something like the following:
Code:
float total=0;
for (int i=0; i<100; i++)
  total+= some_number();
Ambitious contributor
17Sep2008,06:04   #3
oogabooga's Avatar
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