By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,673 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,673 IT Pros & Developers. It's quick & easy.

Can C# count?

P: n/a
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!
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Martin Hazell <pl**********@to.group> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
On Tue, 8 Jul 2003 17:23:47 +0100, Jon Skeet <sk***@pobox.com> wrote:
You need to read about floating point numbers. See
http://docs.sun.com/source/806-3568/ncg_goldberg.html


Nice find! I think I'll add this link to my website. For a (much)
shorter introduction to the topic, I'd also recommend my old Lahey
favourite Bruce M. Bush, "The Perils of Floating Point":
http://www.lahey.com/float.htm
--
http://www.kynosarges.de
Nov 13 '05 #3

P: n/a
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 <ch*****@yumspamyumYahoo.com> wrote in message
news:0b****************************@phx.gbl...
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!
.

Nov 13 '05 #4

P: n/a
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!
.

Nov 13 '05 #5

P: n/a
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

-----Original Message-----
Martin Hazell <pl**********@to.group> wrote:
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 yourproblem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.