473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this decimal division not give an exact result?

Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLi ne(c.ToString() );

gives the result 0.8851041666666 666666666666667

The correct, exact answer is .885.

I know that floating point types cannot represent some values
precisely, but the decimal type (which is a base 10 FP type) CAN
represent these three values (the dividend, divisor, and quotient)
exactly.

Furthermore, doc states that "The result of an operation on values of
type decimal is that which would result from calculating an exact
result (preserving scale, as defined for each operator) and then
rounding to fit the representation. "

Could someone explain this unexpected result?

Oct 17 '08 #1
9 7420
Hmmm... I just ran this on Excel and got a result of
0.8851041666666 67. So, I calculated it by hand and got the same
result as Excel. Am I not understanding the setup of you problem? I
don't think the .885 is the propper result.

John
Oct 17 '08 #2
"Elroy Flynn" <le*******@gmai l.comwrote in message
news:2e******** *************** ***********@t42 g2000hsg.google groups.com...
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLi ne(c.ToString() );

gives the result 0.8851041666666 666666666666667

The correct, exact answer is .885.
Methinks you were using the numeric keypad and accidentally hit 5 when you
hit 8, because 42.48 / 48 = .885, not 42.485.
Oct 17 '08 #3
On Fri, 17 Oct 2008 11:15:40 -0700, Elroy Flynn <le*******@gmai l.com>
wrote:
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLi ne(c.ToString() );

gives the result 0.8851041666666 666666666666667

The correct, exact answer is .885.

I know that floating point types cannot represent some values
precisely, but the decimal type (which is a base 10 FP type) CAN
represent these three values (the dividend, divisor, and quotient)
exactly.
Why do you say that?
Furthermore, doc states that "The result of an operation on values of
type decimal is that which would result from calculating an exact
result (preserving scale, as defined for each operator) and then
rounding to fit the representation. "

Could someone explain this unexpected result?
Look at this code:

decimal a = 2;
decimal b = 3;
decimal c = a / b;

Console.WriteLi ne(c.ToString() );

What should that print out?

Pete
Oct 17 '08 #4
Elroy Flynn <le*******@gmai l.comwrote:
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLi ne(c.ToString() );

gives the result 0.8851041666666 666666666666667

The correct, exact answer is .885.
It's really not. 0.885 * 48 = 42.48.
I know that floating point types cannot represent some values
precisely, but the decimal type (which is a base 10 FP type) CAN
represent these three values (the dividend, divisor, and quotient)
exactly.

Furthermore, doc states that "The result of an operation on values of
type decimal is that which would result from calculating an exact
result (preserving scale, as defined for each operator) and then
rounding to fit the representation. "

Could someone explain this unexpected result?
Your expectations are flawed :) Make a = 42.48M and you'll get your
expected result.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 17 '08 #5
On Oct 17, 3:21*pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Elroy Flynn <leotoh...@gmai l.comwrote:
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLi ne(c.ToString() );
gives the result 0.8851041666666 666666666666667
The correct, exact answer is .885.

It's really not. 0.885 * 48 = 42.48.
I know that floating point types cannot represent some values
precisely, but the decimal type (which is a base 10 FP type) CAN
represent these three values (the dividend, divisor, and quotient)
exactly.
Furthermore, doc states that "The result of an operation on values of
type decimal is that which would result from calculating an exact
result (preserving scale, as defined for each operator) and then
rounding to fit the representation. "
Could someone explain this unexpected result?

Your expectations are flawed :) Make a = 42.48M and you'll get your
expected result.

--
Jon Skeet - <sk...@pobox.co m>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
where's the "delete from web" command?
Oct 17 '08 #6
Michael B. Trausch wrote:
On Fri, 17 Oct 2008 13:56:44 -0700 (PDT)
Elroy Flynn <le*******@gmai l.comwrote:
>where's the "delete from web" command?

This is a newsgroup. You cannot delete your post once you have sent
it, because most servers on Usenet do not honor the commands that used
to be used for that since they've been abused in the past.

As far as Google Groups, you'll have to go through the same process
that anyone that wants to have their messages pulled from their
archives has to do: Find the form if they still have it and request
that the message(s) be removed by their Message ID. You will also have
to prove that you own the email address associated with the Message ID.
I don't think it was meant that literally.

Arne
Oct 18 '08 #7
Are you posting in HTML format or something? To me your posts are empty but
have two attachments...

01: ATT0096.txt
02: signature.asc

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 18 '08 #8
As Peter Morris indicated with your other post, something strange is
happening. You actual text is showing as an attachment to a blank post.
The post on this part of the thread that Arne V. responded to did not come
through at all. I'm using Windows Mail to view the group.

Both appear fine at the microsoft discussion groups page though, but it's
strange...

"Michael B. Trausch" <mi**@trausch.u swrote in message
news:2008101723 2431.338ecb01@z est...

Oct 18 '08 #9
Family Tree Mike wrote:
As Peter Morris indicated with your other post, something strange is
happening. You actual text is showing as an attachment to a blank post.
The post on this part of the thread that Arne V. responded to did not
come through at all. I'm using Windows Mail to view the group.

Both appear fine at the microsoft discussion groups page though, but
it's strange...
I use Thunderbird to view the newsgroups, and there the posts show up
just as usual.

--
Göran Andersson
_____
http://www.guffa.com
Oct 18 '08 #10

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

Similar topics

3
1997
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does not give exactly 0.07 in javascript. Javascript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an...
2
1582
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does not give exactly 0.07 in javascript. Javascript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an...
1
1246
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does not give exactly 0.07 in javascript. Javascript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an...
2
1900
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, ` 5 * 1.015 ` does not give exactly ` 5.075 ` and ` 0.06+0.01 ` does not give exactly ` 0.07 ` in javascript. ECMAScript numbers are represented in binary as IEEE-754 (IEC 559)
1
1632
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, ` 5 * 1.015 ` does not give exactly ` 5.075 ` and ` 0.06+0.01 ` does not give exactly ` 0.07 ` in javascript. ECMAScript numbers are represented in binary as IEEE-754 (IEC 559)
1
10321
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
10077
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
9152
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
7620
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
6853
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
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.