How to check if a number is mathematically undefined or indefinite in vb.net

984 Views Asked by At

In one of my program's validation, I need to check if a resulting number stored in a session is undefined (this is due to one of the formula may have a dividend of zero).

What I tried so far (and it may be a very inefficient way of doing it) is this:

  Dim x As Double
  Dim valid As Boolean = True
  Try
      Double.TryParse(Session("result"), x)
  Catch ex As Exception
      valid = False   
  End Try

I figured that if the number stored in the session is indefinite or undefined, the TryParse function will fail. What do you think is the better way to catch undefined numbers stored in object?

P.S. Unfortunately, I cannot validate the function where the Session("result") will originate. This is because another module created by another coder is just passing that to the module I'm coding.

1

There are 1 best solutions below

1
Slai On

System.Decimal does not have NaN or infinity, so you can use it instead:

Dim valid = Decimal.TryParse(Session("result").ToString, x)

It will result in False if Session("result") is Double.NaN, .PositiveInfinity or .NegativeInfinity