Divison by zero

How to you handle/detect division by zero in your apps for the different data types?

This code has the following result

  dim val1 as Currency
  val1 = 0.0 / 0.0 
  TextArea1.Text = val1.ToText + EndOfLine
  
  val1 = 1.0 / 0.0
  TextArea1.Text = TextArea1.Text + val1.ToText + EndOfLine
  
  
  dim val2 as double
  val2 = 0 / 0
  TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  
  val2 = 1 / 0
  TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine

Result in TextArea1:

-0.5808
-0.5808
nan
inf

For me the values for the currency data type seems to be confusing because they also can be real results.

IsNanMBS() function helps me.

as well as IsInfMBS and IsFiniteMBS.

Or check yourself the arguments before dividing.

Just dont perform the division

  dim val1 as Currency
  if divisor <> 0.0 then
    val1 = 0.0 / divisor
    TextArea1.Text = val1.ToText + EndOfLine
 
    val1 = 1.0 / divisor
    TextArea1.Text = TextArea1.Text + val1.ToText + EndOfLine
  end if
  
  dim val2 as double
  if divisor <> 0.0 then
     val2 = 0 / divisor
    TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  
    val2 = 1 / divisor
    TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  end if

Why ? Because Inf and Nan have no representation in an integer - only in doubles
So this is the only reliable way to avoid issues in ALL numeric types
And comparison to 0 or 0.0 is safe in all of them as well

Oh and because INF and Nan in doubles may have many different representations quite legally
See https://en.wikipedia.org/wiki/NaN and https://en.wikipedia.org/wiki/IEEE_floating_point
IEEE 754 NaNs are represented with the exponent field filled with ones (like infinity values), and some non-zero number in the significand (to make them distinct from infinity values); this representation allows the definition of multiple distinct NaN values, depending on which bits are set in the significand, but also on the value of the leading sign bit (not all applications are required to provide distinct semantics for those distinct NaN values).