473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can C# count?

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
5 4407
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
6
by: Geetha | last post by:
I searched in the Oracle documents what count (1) meant and I could not find an answer. Can some one explain what Oracle does internally when use count (1) VS count (*). Thank you very much in...
1
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> ...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
23
by: Gary Wessle | last post by:
Hi I have a vector<charwhich looks like this (a d d d a d s g e d d d d d k) I need to get the biggest count of consecutive 'd'. 5 in this example I am toying with this method but not sure if...
1
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
7
by: Chris | last post by:
I am trying to increase/decrease the value of $_SESSION by 1 after clicking on a link e.g index.php?gotoWk=nxtWk and index.php? gotoWk=lstWk. I'm sure you will get the drift if you look at the code...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.