Can C# count?

M

Martin Hazell

I have just written part of an app that will sum up the
information from some objects in an array. This works
fine, and I didn't think there was a problem, until I
noticed that the total was incorrect!
Below is the code, and the output file generated -I make
it ad up to 32431.80, not 32431.79. I know it is only
small, but I am concerned that this small error may
indicate another problem.

Thanks for your help.

Martin

Code:

float totWeight = 0;
int totQty = 0;
System.IO.StreamWriter pout = new StreamWriter
(@"C:\ADDING.CSV", false); // added to debug
for(int i = 0; i < LevelMax; i++) {
for(int j = 0; j < ColMax; j++) {
if(row[i,j] != null) {
pout.WriteLine(row[i,j].pallet + "," + row
[i,j].qty + "," + row[i,j].weight); // debug
totWeight += row[i,j].weight;
totQty += row[i,j].qty;
}
}
}
pout.Close()

The output I get in the CSV file is:
9167 50 976.46
9168 50 1021.8
9169 50 1011.86
9170 50 1001.8
9171 50 1016.2
9172 50 1014.24
9173 50 1024.2
9176 50 1018.28
9177 50 1020.7
9178 50 1015.76
9179 50 1021.88
9180 50 1020
9181 50 994.26
9182 50 1021.12
9183 50 1021.22
9184 50 1006.76
9185 50 1019.04
9186 50 1022.34
9187 50 1020.74
9188 50 1009.02
9189 50 1018.12
9190 50 1020.8
9191 50 1019.86
9192 50 1020.56
9193 50 1000
9194 50 1022.48
9195 50 1013.28
9196 50 1022.48
9197 50 1014.08
9198 50 1022.46
20301 48 960
60201 51 1020
^- This col adds up to 32431.80
C# adds it up to 32431.79!
 
J

Jon Skeet

Martin Hazell said:
I have just written part of an app that will sum up the
information from some objects in an array. This works
fine, and I didn't think there was a problem, until I
noticed that the total was incorrect!
Below is the code, and the output file generated -I make
it ad up to 32431.80, not 32431.79. I know it is only
small, but I am concerned that this small error may
indicate another problem.

You need to read about floating point numbers. See
http://docs.sun.com/source/806-3568/ncg_goldberg.html

In this case, just using decimal instead of float will sort out your
problem.
 
R

Richard A. Lowe

Anytime you use any floating point type, you risk these
sort of inaccuracies. For guaranteed precision for all
financial calculations or other intolerant apps use the
decimal type.

Richard
 
M

Martin Hazell

Thanks for swift reply.

I would have thought that with only 2 decimal places that
a float would have been OK.

Will read up on that link.

Thanks

Martin
 
S

Simon Trew

No, not anytime. Only sometimes. You can guarantee the accuracy of FP
types-- or rather, how much inaccuracy they have-- quite exactly. Decimal
types are indeed better for financial calculations, but only because
financial calculations happen to be based on decimal these days, and the
type (mirabile dictu) has been made to suit. Binary floating types are
sometimes better suited. You didn't suggest they weren't but I thought I
should stress your point.

Gosh I am in a pedantic mood tonight.

S.

Richard A. Lowe said:
Anytime you use any floating point type, you risk these
sort of inaccuracies. For guaranteed precision for all
financial calculations or other intolerant apps use the
decimal type.

Richard
-----Original Message-----
I have just written part of an app that will sum up the
information from some objects in an array. This works
fine, and I didn't think there was a problem, until I
noticed that the total was incorrect!
Below is the code, and the output file generated -I make
it ad up to 32431.80, not 32431.79. I know it is only
small, but I am concerned that this small error may
indicate another problem.

Thanks for your help.

Martin

Code:

float totWeight = 0;
int totQty = 0;
System.IO.StreamWriter pout = new StreamWriter
(@"C:\ADDING.CSV", false); // added to debug
for(int i = 0; i < LevelMax; i++) {
for(int j = 0; j < ColMax; j++) {
if(row[i,j] != null) {
pout.WriteLine(row[i,j].pallet + "," + row
[i,j].qty + "," + row[i,j].weight); // debug
totWeight += row[i,j].weight;
totQty += row[i,j].qty;
}
}
}
pout.Close()

The output I get in the CSV file is:
9167 50 976.46
9168 50 1021.8
9169 50 1011.86
9170 50 1001.8
9171 50 1016.2
9172 50 1014.24
9173 50 1024.2
9176 50 1018.28
9177 50 1020.7
9178 50 1015.76
9179 50 1021.88
9180 50 1020
9181 50 994.26
9182 50 1021.12
9183 50 1021.22
9184 50 1006.76
9185 50 1019.04
9186 50 1022.34
9187 50 1020.74
9188 50 1009.02
9189 50 1018.12
9190 50 1020.8
9191 50 1019.86
9192 50 1020.56
9193 50 1000
9194 50 1022.48
9195 50 1013.28
9196 50 1022.48
9197 50 1014.08
9198 50 1022.46
20301 48 960
60201 51 1020
^- This col adds up to 32431.80
C# adds it up to 32431.79!
.
 
Top