Why is division by zero not throwing exception

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
56
Location
UK
Programming Experience
3-5
As the title explains, I would have excepted an exception on this line

C#:
float result = fn / (float)sn;
if fn = 45 and sn = 0

Instead I get 8 as the answer.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
214
Location
Virginia Beach, VA
Programming Experience
10+
In that code there, you are doing floating point division, not integer division. That result you are getting is an infinity symbol, not an 8.

As the DivideByZeroException documentation says:
Trying to divide an integer or Decimal number by zero throws a DivideByZeroException exception. To prevent the exception, ensure that the denominator in a division operation with integer or Decimal values is non-zero.

Dividing a floating-point value by zero doesn't throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic
 

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
56
Location
UK
Programming Experience
3-5
ok, but how do I still a maintain floating point division that also kicks back an exception when someone tries to do division by zero?
 

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
56
Location
UK
Programming Experience
3-5
I think I know the answer, cast it as decimal instead of float
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,286
Location
Sydney, Australia
Programming Experience
10+
I think I know the answer, cast it as decimal instead of float
Uh, no. The thing to do would be to test the value you're dividing by first and don't do it if it's zero. Prevention is almost always better than cure.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
519
Location
Norway
Programming Experience
10+
You can also test result with float.IsInfinity.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
214
Location
Virginia Beach, VA
Programming Experience
10+
ok, but how do I still a maintain floating point division that also kicks back an exception when someone tries to do division by zero?
Why? Are you porting code from another language which does throw division by zero exceptions and you want to try to keep the ported close as close to the original code as possible?
 

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
56
Location
UK
Programming Experience
3-5
Uh, no. The thing to do would be to test the value you're dividing by first and don't do it if it's zero. Prevention is almost always better than cure.
Thanks jmcilhinney :)
 

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
56
Location
UK
Programming Experience
3-5
Why? Are you porting code from another language which does throw division by zero exceptions and you want to try to keep the ported close as close to the original code as possible?
No, this is me just trying to practice C# and figuring out ways to do stuff.
 
Top Bottom