By design, why does the C# compiler allows any float or double values to be divided by zero?
class Program
{
static void Main(string[] args)
{
double x = 0.0 / 0;
float y = 1f / 0;
}
}
Because IEEE 754 floating-point values have special non-numeric values to deal with this:
PS Home:\> 1.0/0
Infinity
PS Home:\> 0.0/0
NaN
whereas dividing an integer by zero is always an exception (in the C# sense1), so you could just throw the exception directly.
1 Dividing a floating-point number by zero is also an exception but at a completely different level and many programming languages abstract this away.
Because floating point values have a valid (and genuinely useful) representation of infinity, whereas integers of any type do not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With