Is it a good idea to compare double.MaxValue for equality?

The same question can be asked using float ... or MinValue.

I think using it as a special meaning. Will I see errors due to accuracy? I do not expect to do arithmetic with these numbers, just set them and that.

Clarification: I use this for the watchdog value.

Is this something described in the C # spec?

+6
source share
5 answers

It depends on how you use it.

If you use double.MaxValue as a token or guard value that has special semantics, then yes, it's just a bit pattern with which you are comparing. For example, you can use double.MaxValue to indicate an "uninitialized" or "unknown" value. There are other ways to do this (for example, using a nullable double? ), But using double.MaxValue also reasonable if this value does not occur naturally in your domain.

If you have some kind of arbitrary double value, and you want to see if it is equal to double.MaxValue , then you will need to see if the numbers are within a small range (epsilon) of each other, since some accuracy could be lost when calculating another double value. The issue to keep in mind is values ​​that go beyond double.MaxValue , creating an overflow situation.

+9
source

If you compare double.MaxValue with double.MaxValue , yes, they will be the same. The binary representation is identical, there will be no problems. If, on the other hand, you try something like:

 double myDouble = (double.MaxValue - 3.1415) / 2; if((myDouble * 2) + 3.1415 == double.MaxValue) { ... } 

then yes, you will probably begin to see how problems arise with a high degree of accuracy.

The following are special constants that cannot be compared with yourself. Everything else is fair play.

  • NaN
  • NegativeInfinity
  • PositiveInfinity
+2
source

Using "special values" is usually bad practice. I would prefer to use an object with some kind of status code, and then double (/ float / whatever), which is populated only if the status is not exclusive.

 public class CalcNumber { public CalcNumberStatus Status {get; private set;} public double Value {get; private set;} public CalcNumber(double value) { Status = CalcNumberStatus.Normal; Value = value; } public CalcNumber(CalcNumberStatus status) { if(status == CalcNumberStatus.Normal) { throw new Exception("Cannot create a normal CalcNumber without a value"); } Status = status; Value = 0; } } public enum CalcNumberStatus { Normal, Error } 

You can even do some fancy operator overloading to simplify the conversion and arithmetic if you need to.

As for accuracy problems, since it seems that you are not planning to do arithmetic on this number, therefore, you should not encounter accuracy problems that interfere with equality checking to work.

+1
source

If you want a "special" value, my suggestion would be to use Nullable instead:

 double? val = ...; if(val.HasValue) // do something with val.Value 
+1
source

I do not think this should be a problem. I have no technical explanation, but I have used MaxValue or MinValue many times for comparison, and this has never been a problem.

0
source

Source: https://habr.com/ru/post/886427/


All Articles