Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0.0/0.0 in C# won't throw "Attempted to divide by zero."?

Tags:

c#

.net

double

I've seen this finance calculation code at my friend's computer :

double Total = ...
double Paid = ...
double Wating_For_Details = ...
double Decuctibles = ...
double Rejected = ...

Well , the moment I saw this , I told him that double is represented at base 2 and can NOT represent finance calculation. use decimal instead.

great.

But the moment I change it to double Ive encountered :

Attempted to divide by zero.

HUH ?

Apparently - using double , when dividing with 0.0 it does NOT throws exception :

enter image description here

But returns NAN.

While my code (using decimal) does throw exception ( when Total is zero)

And so I ask :

I checked 0.0==0 and it returns true. so why I'm not getting exception but NAN? I know thats how it should be but where is the common sence of not throwing exception when dividing double by zero ?

like image 493
Royi Namir Avatar asked Jul 04 '13 14:07

Royi Namir


People also ask

What is host 0. 0 0. 0?

It tells a server to "listen" for and accept connections from any IP address. On PCs and client devices. A 0.0. 0.0 address indicates the client isn't connected to a TCP/IP network, and a device may give itself a 0.0. 0.0 address when it is offline.

Why is hostname 0. 0 0. 0?

In the context of routing, 0.0. 0.0 usually means the default route, i.e. the route which leads to 'the rest of' the Internet instead of somewhere on the local network.

Is 0. 0 0. 0 a valid IP?

for all intents and purposes, yes. Each of the four numbers separated by the period have a value ranging from 0-255, so 0.0. 0.0 is technically valid.

Is 127. 0 0. 0 same as 0. 0 0. 0?

127.0. 0.1 is the loopback address (also known as localhost). 0.0. 0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target (a no particular address placeholder).


1 Answers

Unlike operations with integral types, which throw exceptions in cases of overflow or illegal operations such as division by zero, operations with floating-point values do not throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, positive infinity, negative infinity, or not a number (NaN):

From Double on MSDN.

like image 107
ken2k Avatar answered Oct 16 '22 15:10

ken2k