473,395 Members | 1,846 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,395 software developers and data experts.

C# double works differntly for the same set of values

Hi
Can you help me in understanding the following.

double x,y;
x=2.5;
y=22/7*x*x;;

Here the value displayed is 18.75.

when the above code is written as given below the value is 19.642857

x=2.5
y=x*x*22/7;

Actual value shall be 19.642857 only why it gives 18.75 in the first case
as both are literally same.

BMano
Apr 25 '08 #1
4 1157
Plater
7,872 Expert 4TB
Very strange.
Look at this:
Expand|Select|Wrap|Line Numbers
  1. double y;
  2. y = 22 / 7;
  3. Console.WriteLine(y.ToString()); 
  4.  
The output is "3".

Using two integers (despite that they also fit as doubles) tells the compiler to do integer math. So append a .0 to your numbers:

Expand|Select|Wrap|Line Numbers
  1. double y;
  2. y = 22.0 / 7.0;
  3. Console.WriteLine(y.ToString()); 
  4.  
Doing that gave me the 19 number each time. I think only ONE hardcoded (like the 22 or the 7) number has to be #.0 for it to work correctly.

Alternatively, type-casting one of your integer values toa double will work as well:
y = (double)22 / 7 * x * x; //gives the 19.whatever number
Apr 25 '08 #2
Dear Plater
Thank you very much for your reply.

but i need a bit more clarification. double x=2.5; double y;
As yo have mentioned y=22.0/7.0*x*x gives right value.
when it is written as y=x*x*22/7 also gives right value, only
y=22/7*x*x gives wrong value.

does it means that in y=x*x*22/7 the first two value multiplied gets a value of double and hence subsequent multiplying by 22 and dividing by 7 gives right value.
And in the case y=22/7*x*x the first division takes place as integer and subsequently multiplied by x (2..5) and x which gave 18.5 ( a double value.)

BMano
Apr 26 '08 #3
r035198x
13,262 8TB
Dear Plater
Thank you very much for your reply.

but i need a bit more clarification. double x=2.5; double y;
As yo have mentioned y=22.0/7.0*x*x gives right value.
when it is written as y=x*x*22/7 also gives right value, only
y=22/7*x*x gives wrong value.

does it means that in y=x*x*22/7 the first two value multiplied gets a value of double and hence subsequent multiplying by 22 and dividing by 7 gives right value.
And in the case y=22/7*x*x the first division takes place as integer and subsequently multiplied by x (2..5) and x which gave 18.5 ( a double value.)

BMano
That is correct. It's the division part that's important here. When the divisor and the dividend are integers, the result is truncated but when one of them is a double then the value of the expression becomes double.
Apr 28 '08 #4
Plater
7,872 Expert 4TB
you gotta think this:
double x; //x is a double
22; //22 is only an integer
Apr 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Simon | last post by:
Hi all I am writing a small app that uses real numbers all over the place for calculations. But when it comes to displaying values it is better to only display an it, (especially when it...
8
by: Philipp | last post by:
Hello, I would like to print (cout) the binary representation of a double. ie the values of the bytes 1-8. A typical output would be something like (in hex) ff af 12 d3 ab 9f 3c 00 Is there a...
2
by: Miguel Morales | last post by:
Hello: Currently, I am trying to solve a problem that requires the calculation of Airy functions for big arguments. Because Airy functions diverge rapidly for positive values, the results of the...
8
by: Ahmad A. Rahman | last post by:
Hi all, I have a problem constructing a regular expression using .net. I have a string, separated with comma, and I want to group the string together but, I failed to group a numeric character...
44
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b,...
6
by: Johs | last post by:
I have: int a = 1; int b = 2; double c = a/b; Is it somehow possible to divide these two integers and get the result as a double 0.5? Or do they both have to be declared as doubles?
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.