Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Regarding sumation of floating points (http://www.go4expert.com/forums/regarding-sumation-floating-t13935/)

joeserhal 16Sep2008 11:44

Regarding sumation of floating points
 
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! ;)

xpi0t0s 17Sep2008 05:28

Re: Regarding sumation of floating points
 
How about something like the following:
Code:

float total=0;
for (int i=0; i<100; i++)
  total+= some_number();


oogabooga 17Sep2008 06:04

Re: Regarding sumation of floating points
 
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



All times are GMT +5.5. The time now is 07:16.