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

Divide by Zero Question.

Hello! I was working on some code the other day, and I came across an odd
discrepancy between the decimal and the double type.

If I attempt to divide a decimal by zero, the framework throws an error.
If I attempt to divide a double by zero, the framework returns infinity.

From a mathematical standpoint, it would seem to me that the decimal handles
this division correctly, while the double's handling of this is flawed.

Is there a practical reason why these two types handle this so differently?

Thanks in advance!

Mike
Oct 12 '07 #1
8 4770
michaelgweier <mi***********@discussions.microsoft.comwrote:
Hello! I was working on some code the other day, and I came across an odd
discrepancy between the decimal and the double type.

If I attempt to divide a decimal by zero, the framework throws an error.
If I attempt to divide a double by zero, the framework returns infinity.

From a mathematical standpoint, it would seem to me that the decimal handles
this division correctly, while the double's handling of this is flawed.

Is there a practical reason why these two types handle this so differently?
In a word, standards. At least, I suspect that's the reason.
float/double follow the IEC 60559 standard rules for arithmetic,
including division by zero resulting in an "infinite" value rather than
throwing an exception.

Decimal doesn't (or at least doesn't *have* to - the C# spec allows for
the possibility) support an "infinite" value whereas float/double do.
Similar decimal doesn't have a NaN specified.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '07 #2
michaelgweier wrote:
Hello! I was working on some code the other day, and I came across an odd
discrepancy between the decimal and the double type.

If I attempt to divide a decimal by zero, the framework throws an error.
If I attempt to divide a double by zero, the framework returns infinity.

From a mathematical standpoint, it would seem to me that the decimal handles
this division correctly, while the double's handling of this is flawed.

Is there a practical reason why these two types handle this so differently?
I suspect it has to do with the FPU behavor, since I would expect the
double to be handled by hardware, but the decimal type to be handled in
software.

There may in fact be a way to set the FPU to raise an exception in the
divide by zero case. Whether this would be propagated back to your
managed code, I don't know.

Pete
Oct 12 '07 #3
Thank you. I appreciate the quick answers!

MW

"michaelgweier" wrote:
Hello! I was working on some code the other day, and I came across an odd
discrepancy between the decimal and the double type.

If I attempt to divide a decimal by zero, the framework throws an error.
If I attempt to divide a double by zero, the framework returns infinity.

From a mathematical standpoint, it would seem to me that the decimal handles
this division correctly, while the double's handling of this is flawed.

Is there a practical reason why these two types handle this so differently?

Thanks in advance!

Mike
Oct 12 '07 #4
That's not an exception you want to catch either way. Catching that
exception is 400x slower (literally) than checking the divisor for
zero with an "if" statement.

Oct 12 '07 #5
"not_a_commie" <no********@gmail.comschrieb im Newsbeitrag
news:11*********************@q3g2000prf.googlegrou ps.com...
That's not an exception you want to catch either way. Catching that
exception is 400x slower (literally) than checking the divisor for
zero with an "if" statement.
How did you measure this?

Christof

Oct 15 '07 #6
How did you measure this?

Okay, so my measurement wasn't exactly fair; it was assuming an awful
lot of divide by zeros. If you only divide by zero one in a thousand
times, the exception might be worth it. I did my measurements a number
of months ago. It's logged as MS feedback item 256733.

Oct 15 '07 #7
I'd actually blogged some similar results to try catch timing recently. See
my post at
http://dotnetthoughts.wordpress.com/...est-practices/.

Cheers!

MW

"Christof Nordiek" wrote:
"not_a_commie" <no********@gmail.comschrieb im Newsbeitrag
news:11*********************@q3g2000prf.googlegrou ps.com...
That's not an exception you want to catch either way. Catching that
exception is 400x slower (literally) than checking the divisor for
zero with an "if" statement.
How did you measure this?

Christof

Oct 16 '07 #8
"michaelgweier" <mi***********@discussions.microsoft.comschrieb im
Newsbeitrag news:66**********************************@microsof t.com...
I'd actually blogged some similar results to try catch timing recently.
See
my post at
http://dotnetthoughts.wordpress.com/...est-practices/.
The first thing I see from that blog is, that Exceptions are much slower in
the IDE than without. I felt that often, but never measured it.
Second: the version with Exception runs 250 times resp. 500 ns slower. So
the blog says nothing about the performance, it still gives a rough measure
of when this will be a performance issue and when not.
Thanks for the work.

Christof

Oct 26 '07 #9

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

Similar topics

3
by: Ryan | last post by:
I have the following line in a select statement which comes up with a divide by zero error. CAST(CASE Splinter_Status WHEN 'SUR' THEN 0 ELSE CASE WHEN Sacrifice>=1 THEN...
2
by: Mike Leahy | last post by:
All... I have a query that calculates various using variables from a survey database. As with any survey, there are many instantces of null values. I'm wondering if there is any way to escape...
0
by: Gary Carson | last post by:
Can anyone tell why the query below would throw a divide-by-zero error? The only reason I can see for the error happening would be if SUM() came out to be zero, but this never happens with the...
3
by: Joriveek | last post by:
When I specify a formula between Computed Column Specification, I have two zero values, getting Divide by Zero error, any idea how can I avoid this? I still want SQL Server to display Zero if it is...
4
by: shuisheng | last post by:
Dear all, Assume I have two big arrays A and B. And I want to element-wise divide A by B. When an element of B is zero, the results are also zero. Such as A = { 2, 4, 0, 6} B = { 1, 0, 0, 2}...
5
by: Neo | last post by:
Hi Friends, I am trying following code int main(void) { try { int i,j,k; i=10; j=0;
9
ollyb303
by: ollyb303 | last post by:
Hi, I have created a report in my Access db which has two columns - I'll call them Number1 and Number2. I have added a textbox to calculate a percentage from these two numbers =(/) with the...
2
by: JenavaS | last post by:
Hi all, I have a query: SELECT Data.Region, Data.Dept, Data.Year, Data.Month, Data.Week, Data.Elapsed, Data. AS WpComp, Data. AS CpComp, IIf(=53 And ="JAN,WK4",/2,) AS AdjLyComp,...
1
by: zufie | last post by:
Help! How do I divide by zero on my form (in a text box). That is, how do I carry out a division problem when the denominator is zero? Thanks!, John
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
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...

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.