473,712 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decimal Precision - short code, strange problem

Hi

Could anyone help me to understand why in the following code sum1 is
different from sum2?
Basically it's the same calculation done. Keep in mind the x is 28
digits so it's within the range of decimal.

<codesnip>
decimal x = (-0.0084682975822 291150192357277 M);
decimal sum1 , sum2;

sum2 = (3500.91M * x) + (x * 3500M + 822m);
sum1 = (3500.91M * x) + (x * 3500M) + 822m;

Console.WriteLi ne (sum1);
Console.WriteLi ne (sum2);
</codesnip>

Thank you,
Saar Carmi

Feb 6 '06 #1
2 1602
<sa*******@gmai l.com> wrote:
Could anyone help me to understand why in the following code sum1 is
different from sum2?
Basically it's the same calculation done. Keep in mind the x is 28
digits so it's within the range of decimal.

<codesnip>
decimal x = (-0.0084682975822 291150192357277 M);
decimal sum1 , sum2;

sum2 = (3500.91M * x) + (x * 3500M + 822m);
sum1 = (3500.91M * x) + (x * 3500M) + 822m;

Console.WriteLi ne (sum1);
Console.WriteLi ne (sum2);
</codesnip>


I believe I understand the reason for the difference. Suppose you have
a type which is accurate to 3 significant digits. Now consider the
difference between:

100 + 1.49 + 0.03
and
100 + (1.49 + 0.03)

The first would be evaluated as:

(100 + 1.49) + 0.03
=> 101 + 0.03
=> 101

The second is:

100 + (1.49 + 0.03)
=> 100 + 1.52
=> 102

Do you follow why the order is important?

I haven't checked whether or not that's what's happening in your case,
but I suspect it is. You could print out each part of the sum and do it
manually to verify it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 6 '06 #2
Seems you're right.
Thank you.

Feb 7 '06 #3

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

Similar topics

21
4521
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
6145
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
1
3569
by: Tariq | last post by:
I've a SQL view performing a number of very precise calculations ie. 495/(1.112 - (.00043499*((Col1 + Col2 + Col3))) + ( .00000055 * ((Col1 + Col2 + Col3)) * ((Col1 + Col2 + Col3))) - (.00028826 * Col4))) - 450)/100 Obviously this sometimes causes a precision issues because the float and real datatypes don't hold precise values... My problem is that converting to the numeric or decimal datatype easily produces an error if the...
687
23489
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
8
11326
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
10
8137
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
11
2252
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
3
13656
by: Boot2TheHead | last post by:
This one cost me a solid half hour yesterday. I'm wondering why on earth the default precision for a decimal type is 18,0. Maybe I'm mistaken. A decimal datatype sort of implies that you'd want something after the decimal! Question is, can I set this database-wide? Like all new decimal datatypes have a precision of 12,6 or something like that? I haven't seen anything about this in the googling I have done...
23
9781
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
8794
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9300
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9007
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2101
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.