473,395 Members | 1,474 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.

Why does this decimal division not give an exact result?

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

gives the result 0.8851041666666666666666666667

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 7393
Hmmm... I just ran this on Excel and got a result of
0.885104166666667. 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*******@gmail.comwrote in message
news:2e**********************************@t42g2000 hsg.googlegroups.com...
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLine(c.ToString());

gives the result 0.8851041666666666666666666667

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*******@gmail.com>
wrote:
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLine(c.ToString());

gives the result 0.8851041666666666666666666667

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.WriteLine(c.ToString());

What should that print out?

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

gives the result 0.8851041666666666666666666667

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.com>
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.comwrote:
Elroy Flynn <leotoh...@gmail.comwrote:
Decimal a = 42.485M;
Decimal b = 48M;
Decimal c = a / b;
Console.WriteLine(c.ToString());
gives the result 0.8851041666666666666666666667
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.com>
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*******@gmail.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.uswrote in message
news:20081017232431.338ecb01@zest...

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
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
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: 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...
0
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...

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.