Regarding sumation of floating points

joeserhal's Avatar, Join Date: Feb 2008
Go4Expert Member
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!
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
How about something like the following:
Code:
float total=0;
for (int i=0; i<100; i++)
  total+= some_number();
0
oogabooga's Avatar
Ambitious contributor
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