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 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
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
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
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.
"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
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.
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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}...
|
by: Neo |
last post by:
Hi Friends,
I am trying following code
int main(void)
{
try
{
int i,j,k;
i=10;
j=0;
|
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...
|
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,...
|
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
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |