473,386 Members | 1,867 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.

how does (23+40)/2 become 31?

Hi everyone,

I wrote a quick method to perform a calculation to determine the average of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.

Thanks,
Scott
Nov 17 '05 #1
2 1310
> I wrote a quick method to perform a calculation to determine the average
of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.


(long) 23 + (long) 40 / (long) 2 = long

it always rounds to smaller number: 1.99 rounded is 1.

Cast first operand to double, do calculations, add 0.5 and then round:

(int) (((double) x + y) / 2 + 0.5)
Nov 17 '05 #2
Marko,

In integer (longs) math, the fractional part is thrown away.

Math.Round() can round. But, see the posts in this newsgroup regarding
rounding. Math.Round() may not work the way you want it to. It follows IEEE
specifications.

If you want a fractional result, you must convert or use floating point
numbers (e.g. float, double, decimal) prior to multiplication or division.
Obviously addition and subtraction cannot introduce fractional parts.

When using constants like 23 and 40, you can append them with a letter to
tell the compiler their type. (f=float, d=double, m=decimal)

So, 23f is the float number 23.0, and 40d is the double 40.0.

Otherwise, as you suggest, you must cast (convert) to the appropriate
floating point representation prior to making the conversion.

Hope this helps...

Frisky

"Marko Becirevic" <ma*****************@zg.t-com.hr> wrote in message
news:da**********@ss405.t-com.hr...
I wrote a quick method to perform a calculation to determine the average

of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.


(long) 23 + (long) 40 / (long) 2 = long

it always rounds to smaller number: 1.99 rounded is 1.

Cast first operand to double, do calculations, add 0.5 and then round:

(int) (((double) x + y) / 2 + 0.5)

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: lawrence | last post by:
I'm not sure how this is normally done, on a large site, perhaps one running Phorum. Occassionally a thread will have hundreds of entries, perhaps a meg or two worth of data. You won't necessarily...
9
by: I Report, You Decide | last post by:
A hosting service lungcapage has banned yabb, because it takes too much CPU/memory, but phpBB is fine. Is that perl/php or the script itself? why perl sucks more resource of a server than php? i...
8
by: Ken Varn | last post by:
Everytime I call Process.GetCurrentProcess() the system goes out to the floppy drive. Why does this happen. I decided to use FileMon to get a log of what is happening when this occurs. Here is...
6
by: bayram guzer | last post by:
hi everybody, i have very strange error. i can not see some of the asp pages on my browser. when i look from view source, all the source is there but browser does not show anything, just an empty...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
3
by: Dan | last post by:
Hello, Am trying to construct a query that'll both compute elapsed time based on certain criteria and sum this up for a subset of records. Sample Data that I'm working with: Row Ev_Time ...
13
by: ranjeet.gupta | last post by:
Dear All What does exactly below code means struct File { void* data; }; typedef struct File File; typedef File* fl;
1
by: Dave | last post by:
I am getting te following error in a ASP.Net app that is running on Win XP Pro (SP2): Server cannot access application directory 'C:\Documents and Settings\dave\My Documents\My Visual Studio...
7
by: Papelotte | last post by:
Hi all, I'm new to this forum and I am hoping that there is someone here who can help me. I have an ASP page that has javascript that works perfectly in IE, but not in Firefox. Can anybody tell...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.