...

# Question regarding sumation of floating point numbers

joeserhal
09-16-2008, 08:15 AM
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! ;)

Phoncible
09-28-2008, 05:17 AM
So you essentially want 1 + .01 + .02 + .03 + .. + .99? or up to + 1.99? Sounds like whatever you're trying to do it needs a for loop. I thought of something like this (included output). The while loop may not be necessary. Hope this helps you. Done in C++ by the way.

float i;
float j;
float total = 1.0;

j = total;
while (j <= 2)
{
for (i = 0.0; i < 100; i++)
{
cout << "i is: " << i << endl;
cout << "total is " << total << endl
<< "added to i/100 is " << i/100 << endl;
total = total + i/100;
}
cout << total << endl;
j = total;
}

total is 1.00
i is: 2.00
total is 1.01
i is: 3.00
total is 1.03
i is: 4.00
total is 1.06
i is: 5.00
total is 1.10
i is: 6.00
total is 1.15
i is: 7.00
total is 1.21
i is: 8.00
total is 1.28
i is: 9.00
total is 1.36
i is: 10.00
total is 1.45
i is: 11.00
total is 1.55
i is: 12.00
total is 1.66
i is: 13.00
total is 1.78
i is: 14.00
total is 1.91
i is: 15.00
total is 2.05
i is: 16.00
total is 2.20
i is: 17.00
total is 2.36
i is: 18.00
total is 2.53
i is: 19.00
total is 2.71
i is: 20.00
total is 2.90
i is: 21.00
total is 3.10
i is: 22.00
total is 3.31
i is: 23.00
total is 3.53
i is: 24.00
total is 3.76
i is: 25.00
total is 4.00
i is: 26.00
total is 4.25
i is: 27.00
total is 4.51
i is: 28.00
total is 4.78
i is: 29.00
total is 5.06
i is: 30.00
total is 5.35
i is: 31.00
total is 5.65
i is: 32.00
total is 5.96
i is: 33.00
total is 6.28
i is: 34.00
total is 6.61
i is: 35.00
total is 6.95
i is: 36.00
total is 7.30
i is: 37.00
total is 7.66
i is: 38.00
total is 8.03
i is: 39.00
total is 8.41
i is: 40.00
total is 8.80
i is: 41.00
total is 9.20
i is: 42.00
total is 9.61
i is: 43.00
total is 10.03
i is: 44.00
total is 10.46
i is: 45.00
total is 10.90
i is: 46.00
total is 11.35
i is: 47.00
total is 11.81
i is: 48.00
total is 12.28
i is: 49.00
total is 12.76
i is: 50.00
total is 13.25
i is: 51.00
total is 13.75
i is: 52.00
total is 14.26
i is: 53.00
total is 14.78
i is: 54.00
total is 15.31
i is: 55.00
total is 15.85
i is: 56.00
total is 16.40
i is: 57.00
total is 16.96
i is: 58.00
total is 17.53
i is: 59.00
total is 18.11
i is: 60.00
total is 18.70
i is: 61.00
total is 19.30
i is: 62.00
total is 19.91
i is: 63.00
total is 20.53
i is: 64.00
total is 21.16
i is: 65.00
total is 21.80
i is: 66.00
total is 22.45
i is: 67.00
total is 23.11
i is: 68.00
total is 23.78
i is: 69.00
total is 24.46
i is: 70.00
total is 25.15
i is: 71.00
total is 25.85
i is: 72.00
total is 26.56
i is: 73.00
total is 27.28
i is: 74.00
total is 28.01
i is: 75.00
total is 28.75
i is: 76.00
total is 29.50
i is: 77.00
total is 30.26
i is: 78.00
total is 31.03
i is: 79.00
total is 31.81
i is: 80.00
total is 32.60
i is: 81.00
total is 33.40
i is: 82.00
total is 34.21
i is: 83.00
total is 35.03
i is: 84.00
total is 35.86
i is: 85.00
total is 36.70
i is: 86.00
total is 37.55
i is: 87.00
total is 38.41
i is: 88.00
total is 39.28
i is: 89.00
total is 40.16
i is: 90.00
total is 41.05
i is: 91.00
total is 41.95
i is: 92.00
total is 42.86
i is: 93.00
total is 43.78
i is: 94.00
total is 44.71
i is: 95.00
total is 45.65
i is: 96.00
total is 46.60
i is: 97.00
total is 47.56