View Full Version : 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

added to i/100 is 0.01

i is: 2.00

total is 1.01

added to i/100 is 0.02

i is: 3.00

total is 1.03

added to i/100 is 0.03

i is: 4.00

total is 1.06

added to i/100 is 0.04

i is: 5.00

total is 1.10

added to i/100 is 0.05

i is: 6.00

total is 1.15

added to i/100 is 0.06

i is: 7.00

total is 1.21

added to i/100 is 0.07

i is: 8.00

total is 1.28

added to i/100 is 0.08

i is: 9.00

total is 1.36

added to i/100 is 0.09

i is: 10.00

total is 1.45

added to i/100 is 0.10

i is: 11.00

total is 1.55

added to i/100 is 0.11

i is: 12.00

total is 1.66

added to i/100 is 0.12

i is: 13.00

total is 1.78

added to i/100 is 0.13

i is: 14.00

total is 1.91

added to i/100 is 0.14

i is: 15.00

total is 2.05

added to i/100 is 0.15

i is: 16.00

total is 2.20

added to i/100 is 0.16

i is: 17.00

total is 2.36

added to i/100 is 0.17

i is: 18.00

total is 2.53

added to i/100 is 0.18

i is: 19.00

total is 2.71

added to i/100 is 0.19

i is: 20.00

total is 2.90

added to i/100 is 0.20

i is: 21.00

total is 3.10

added to i/100 is 0.21

i is: 22.00

total is 3.31

added to i/100 is 0.22

i is: 23.00

total is 3.53

added to i/100 is 0.23

i is: 24.00

total is 3.76

added to i/100 is 0.24

i is: 25.00

total is 4.00

added to i/100 is 0.25

i is: 26.00

total is 4.25

added to i/100 is 0.26

i is: 27.00

total is 4.51

added to i/100 is 0.27

i is: 28.00

total is 4.78

added to i/100 is 0.28

i is: 29.00

total is 5.06

added to i/100 is 0.29

i is: 30.00

total is 5.35

added to i/100 is 0.30

i is: 31.00

total is 5.65

added to i/100 is 0.31

i is: 32.00

total is 5.96

added to i/100 is 0.32

i is: 33.00

total is 6.28

added to i/100 is 0.33

i is: 34.00

total is 6.61

added to i/100 is 0.34

i is: 35.00

total is 6.95

added to i/100 is 0.35

i is: 36.00

total is 7.30

added to i/100 is 0.36

i is: 37.00

total is 7.66

added to i/100 is 0.37

i is: 38.00

total is 8.03

added to i/100 is 0.38

i is: 39.00

total is 8.41

added to i/100 is 0.39

i is: 40.00

total is 8.80

added to i/100 is 0.40

i is: 41.00

total is 9.20

added to i/100 is 0.41

i is: 42.00

total is 9.61

added to i/100 is 0.42

i is: 43.00

total is 10.03

added to i/100 is 0.43

i is: 44.00

total is 10.46

added to i/100 is 0.44

i is: 45.00

total is 10.90

added to i/100 is 0.45

i is: 46.00

total is 11.35

added to i/100 is 0.46

i is: 47.00

total is 11.81

added to i/100 is 0.47

i is: 48.00

total is 12.28

added to i/100 is 0.48

i is: 49.00

total is 12.76

added to i/100 is 0.49

i is: 50.00

total is 13.25

added to i/100 is 0.50

i is: 51.00

total is 13.75

added to i/100 is 0.51

i is: 52.00

total is 14.26

added to i/100 is 0.52

i is: 53.00

total is 14.78

added to i/100 is 0.53

i is: 54.00

total is 15.31

added to i/100 is 0.54

i is: 55.00

total is 15.85

added to i/100 is 0.55

i is: 56.00

total is 16.40

added to i/100 is 0.56

i is: 57.00

total is 16.96

added to i/100 is 0.57

i is: 58.00

total is 17.53

added to i/100 is 0.58

i is: 59.00

total is 18.11

added to i/100 is 0.59

i is: 60.00

total is 18.70

added to i/100 is 0.60

i is: 61.00

total is 19.30

added to i/100 is 0.61

i is: 62.00

total is 19.91

added to i/100 is 0.62

i is: 63.00

total is 20.53

added to i/100 is 0.63

i is: 64.00

total is 21.16

added to i/100 is 0.64

i is: 65.00

total is 21.80

added to i/100 is 0.65

i is: 66.00

total is 22.45

added to i/100 is 0.66

i is: 67.00

total is 23.11

added to i/100 is 0.67

i is: 68.00

total is 23.78

added to i/100 is 0.68

i is: 69.00

total is 24.46

added to i/100 is 0.69

i is: 70.00

total is 25.15

added to i/100 is 0.70

i is: 71.00

total is 25.85

added to i/100 is 0.71

i is: 72.00

total is 26.56

added to i/100 is 0.72

i is: 73.00

total is 27.28

added to i/100 is 0.73

i is: 74.00

total is 28.01

added to i/100 is 0.74

i is: 75.00

total is 28.75

added to i/100 is 0.75

i is: 76.00

total is 29.50

added to i/100 is 0.76

i is: 77.00

total is 30.26

added to i/100 is 0.77

i is: 78.00

total is 31.03

added to i/100 is 0.78

i is: 79.00

total is 31.81

added to i/100 is 0.79

i is: 80.00

total is 32.60

added to i/100 is 0.80

i is: 81.00

total is 33.40

added to i/100 is 0.81

i is: 82.00

total is 34.21

added to i/100 is 0.82

i is: 83.00

total is 35.03

added to i/100 is 0.83

i is: 84.00

total is 35.86

added to i/100 is 0.84

i is: 85.00

total is 36.70

added to i/100 is 0.85

i is: 86.00

total is 37.55

added to i/100 is 0.86

i is: 87.00

total is 38.41

added to i/100 is 0.87

i is: 88.00

total is 39.28

added to i/100 is 0.88

i is: 89.00

total is 40.16

added to i/100 is 0.89

i is: 90.00

total is 41.05

added to i/100 is 0.90

i is: 91.00

total is 41.95

added to i/100 is 0.91

i is: 92.00

total is 42.86

added to i/100 is 0.92

i is: 93.00

total is 43.78

added to i/100 is 0.93

i is: 94.00

total is 44.71

added to i/100 is 0.94

i is: 95.00

total is 45.65

added to i/100 is 0.95

i is: 96.00

total is 46.60

added to i/100 is 0.96

i is: 97.00

total is 47.56

added to i/100 is 0.97

i is: 98.00

total is 48.53

added to i/100 is 0.98

i is: 99.00

total is 49.51

added to i/100 is 0.99

final total 50.50

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.