Discussion:
Divide by Zero
Brady Kelly
2008-02-26 09:14:54 UTC
Permalink
Ryan Heath
2008-02-26 09:28:27 UTC
Permalink
Because float & double have a *magic number* for infinity.

BTW not only integers throw but also the decimal type.

// Ryan

On Tue, Feb 26, 2008 at 10:14 AM, Brady Kelly <***@chasesoftware.co.za> wrote:
> In a little more than a nutshell, why does the CLR only throw a divide by
> zero exception for integers?
>
>
> ===================================
> This list is hosted by DevelopMentor(R) http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-02-26 09:32:28 UTC
Permalink
Ryan Heath
2008-02-26 09:53:19 UTC
Permalink
On Tue, Feb 26, 2008 at 10:32 AM, Brady Kelly wrote:
> OK, so why can't integer have a *magic number*? Does it's discrete nature
> rule against this? Now I go back to discussions with school maths teachers,
> but, if an integer cannot be divided by something, then surely the integer
> remains undivided, i.e. it retains its original value?
>

What is the value of (+/-)infinity? Of course we could give up the
values 0x7ffffff and 0x80000000.
But then 0x7ffffffe + 1 would be infinity...

AFAIK you cannot reach infinity (with floats, doubles) just with a
simple addition.

// Ryan

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Frans Bouma
2008-02-26 09:35:15 UTC
Permalink
Efran Cobisi
2008-02-26 09:36:08 UTC
Permalink
A nice side effect of this approximation is that the two outputs of the
following example differ.
Awesome! :)

decimal d = 0m;
float f = 0f;

for (var i = 0; i < 10; i++)
{
d += 0.1m;
f += 0.1f;
}

Console.WriteLine(d == 1m);
Console.WriteLine(f == 1f);

--
Efran Cobisi
http://www.cobisi.com

Frans Bouma wrote:
>> In a little more than a nutshell, why does the CLR only throw a divide by
>> zero exception for integers?
>>
>
> I think because, if I recall correctly, a floating point number is
> always an approximation, so you can have a floating point value which seems 0
> but actually isn't.
>
> Though for decimals, it should indeed...
>
> FB
>
> ===================================
> This list is hosted by DevelopMentor® http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Per Bolmstedt
2008-02-26 09:28:37 UTC
Permalink
On Tue, 26 Feb 2008 10:35:15 +0100, Frans Bouma <***@XS4ALL.NL> wrote:

>> In a little more than a nutshell, why does the CLR only throw a divide by
>> zero exception for integers?
>
> I think because, if I recall correctly, a floating point number is
> always an approximation, so you can have a floating point value
> which seems 0 but actually isn't.

I disagree. PositiveInfinity, for example, is not an approximation. It's
really infinity.
http://msdn2.microsoft.com/en-us/library/system.double.positiveinfinity.aspx

I found this concisely helpful:
http://grouper.ieee.org/groups/754/faq.html#exceptions

Couldn't it be seen as that decimal/integer have been simplified in the CLR
(for programmers), whereas single/double retain their complexity (for
mathematicians)?

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-02-26 09:49:46 UTC
Permalink
Frans Bouma
2008-02-26 10:04:56 UTC
Permalink
Kevin Jones
2008-02-26 10:18:29 UTC
Permalink
I always understood this to be a feature of Intel processors. In the
old days (writing native code), floating point went to the FPU, which,
in the very old days, was a separate chip. This handled float divide
by zero and returned NaN. While the main CPU which handles integer
division threw an exception.

IIRC you can change this behavior with a setting in the registry,
although it's been about 12 years since I tried it and never on CLR,

Kevin

On Tue, Feb 26, 2008 at 9:28 AM, Per Bolmstedt
<***@ul7.info> wrote:
> On Tue, 26 Feb 2008 10:35:15 +0100, Frans Bouma <***@XS4ALL.NL> wrote:
>
> >> In a little more than a nutshell, why does the CLR only throw a divide by
> >> zero exception for integers?
> >
> > I think because, if I recall correctly, a floating point number is
> > always an approximation, so you can have a floating point value
> > which seems 0 but actually isn't.
>
> I disagree. PositiveInfinity, for example, is not an approximation. It's
> really infinity.
> http://msdn2.microsoft.com/en-us/library/system.double.positiveinfinity.aspx
>
> I found this concisely helpful:
> http://grouper.ieee.org/groups/754/faq.html#exceptions
>
> Couldn't it be seen as that decimal/integer have been simplified in the CLR
> (for programmers), whereas single/double retain their complexity (for
> mathematicians)?
>
>
>
> ===================================
> This list is hosted by DevelopMentor(R) http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Jon Skeet
2008-02-26 10:41:04 UTC
Permalink
Kevin Jones wrote:
> I always understood this to be a feature of Intel processors.

I think it's more that it's a feature of IEEE 754 floating point arithmetic.

As for why it doesn't happen for integer values - which current integer
values would you (i.e. anyone) want to lose in order to give room for
infinity?

Jon

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Marc Brooks
2008-02-26 18:20:40 UTC
Permalink
> > I always understood this to be a feature of Intel processors.
>
> I think it's more that it's a feature of IEEE 754 floating point arithmetic.
>
> As for why it doesn't happen for integer values - which current integer
> values would you (i.e. anyone) want to lose in order to give room for
> infinity?

In actuality, it's the MODE that the floating point processor is put
into _by the CLR_ that decides that floats don't throw exceptions (a
horrible decision, as you can always ignore exceptions, but you can't
"add em back"--see MS Feedback link below[0] ).

I STRONGLY recommend that everyone read the paper entitled What Every
Computer Scientist Should Know About Floating-Point Arithmetic[1] I
think it's AWESOME to understand how this works before that odd math
bug catches instead of after...

[0] http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=276107
[1] http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf

Oh, and [0] is yet another example of how useless the "Feedback" site
was.... resolved by passing the information on to the team? That's
not a resolution...

--
"He uses statistics as a drunken man uses lamp-posts… for support
rather than illumination." Andrew Lang

Marc C.
Richard
2008-02-26 10:41:05 UTC
Permalink
Per Bolmstedt
2008-02-26 10:10:14 UTC
Permalink
On Tue, 26 Feb 2008 11:04:56 +0100, Frans Bouma <***@XS4ALL.NL> wrote:

>>> I think because, if I recall correctly, a floating point number is
>>> always an approximation, so you can have a floating point value
>>> which seems 0 but actually isn't.
>>
>> I disagree. PositiveInfinity, for example, is not an approximation.
>> It's really infinity.
>
> Though how would you store 'e' or 'pi' ? You have just 4 or 8 bytes.

I think you read my response out of context. You are positing that the
reason for not throwing an exception when dividing a floating-point number
by zero is that floating-point numbers are always approximations. That's
with which I'm disagreeing, not your understand of floating-point arithmetic
(which obviously by far surpasses mine). The IEEE floating-point rules
dictate the CLR behavior. I just wanted to point out that not all
singles/doubles in the CLR are approximations.

Pi should be stored as a recursive lambda expression, of course. ;]

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Frans Bouma
2008-02-26 10:45:49 UTC
Permalink
Per Bolmstedt
2008-02-26 10:51:13 UTC
Permalink
On Tue, 26 Feb 2008 10:41:04 +0000, Jon Skeet <***@POBOX.COM> wrote:

> As for why it doesn't happen for integer values - which current integer
> values would you (i.e. anyone) want to lose in order to give room for
> infinity?

I've always hated 0x80004005. Let's make that negative infinity. That should
teach it!

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Toulakis, Dimitrios (RESC)
2008-02-26 11:06:09 UTC
Permalink
lol

________________________________

Von: Discussion of development on the .NET platform using any managed language im Auftrag von Per Bolmstedt
Gesendet: Di 26.02.2008 11:51
An: DOTNET-***@DISCUSS.DEVELOP.COM
Betreff: Re: [DOTNET-CLR] Divide by Zero



On Tue, 26 Feb 2008 10:41:04 +0000, Jon Skeet <***@POBOX.COM> wrote:

> As for why it doesn't happen for integer values - which current integer
> values would you (i.e. anyone) want to lose in order to give room for
> infinity?

I've always hated 0x80004005. Let's make that negative infinity. That should
teach it!

===================================
This list is hosted by DevelopMentor® http://www.develop.com <http://www.develop.com/>

View archives and manage your subscription(s) at http://discuss.develop.com <http://discuss.develop.com/>





===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Kevin Jones
2008-02-26 11:48:37 UTC
Permalink
:)

On Tue, Feb 26, 2008 at 10:51 AM, Per Bolmstedt
<***@ul7.info> wrote:
> On Tue, 26 Feb 2008 10:41:04 +0000, Jon Skeet <***@POBOX.COM> wrote:
>
> > As for why it doesn't happen for integer values - which current integer
> > values would you (i.e. anyone) want to lose in order to give room for
> > infinity?
>
> I've always hated 0x80004005. Let's make that negative infinity. That should
> teach it!
>
>
>
> ===================================
> This list is hosted by DevelopMentor(R) http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Continue reading on narkive:
Loading...