Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

passing System.Double.NaN is wonderful for stating that the value is not available or not possible to calculate. How can I implement this on integers and longs?

 

Regards

/Kejpa

Posted

Perhaps Int32.MaxValue or Int32.MinValue would do the trick? I like that they're not just some arbitrary number that you decide means this is a bad return, they're build in to the datatype.

 

I also make use of those when looking for the upper or lower extremities in a range of ints - like if trying for the maximum value, I init to Int32.MinValue, since all my values will be greater than (or equal to) this, etc.

Posted
Perhaps Int32.MaxValue or Int32.MinValue would do the trick?

No, the max/min values _could_ be valid values although very rare. Just like using 0. What I need is to return a value that is not valid, just like System.Double.NaN is a valid double return value but you cannot use it for further calculations

Posted

If you're that worried about edge cases, you're probably going to be putting in checks for Double.NaN in your code, so why not a check for Int.MinValue.

 

What kind of system are you coding where you'd ever have this value?

Int64.MinValue = -9,223,372,036,854,775,808 :eek:

Posted

mocella is right... unless you are calculating distance of the nearest star from the sun... you'll probably never reach a high value like this ... and you even there it's negative :p lol

 

And as it's negative... I don't remember seeing a so low number in the nature.

This could really be an equivalent to NaN.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

Hi,

I am reading external devices which at an end point gives me Integer.MinValue and at the other end point is giving me Integer.MaxValue. Both are valid values and so is any value in between, thus when I don't get a signal (or the checksum is invalid) I can't return MinValue nor MaxValue nor any other. That's why NaN would come in handy.

 

With VB6 you could trick the program to return 1.#INF using "On error resume next" and do some overflow arithmetic. Any calculations would result in 1.#INF and when showing the value I could have a quick check for 1.#INF and either show the correct value or "NaN"

 

Best regards

/Kejpa

Posted
In that case, I'd probably define/use an exception (like InvalidChecksumException) to indicate to the rest of my code that something went wrong with the signal.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...