Does C# have an Unsigned Double?

后端 未结 4 679
忘掉有多难
忘掉有多难 2020-12-20 11:14

I need to use an unsigned double but it turns out C# does not provide such a type.

Does anyone know why?

4条回答
  •  伪装坚强ぢ
    2020-12-20 11:32

    I rolled out a more detailed implementation of @Isak Savo's with tweaks here and there. Not sure if its perfect, but it's a great place to start.

    public struct UDouble
    {
        /// 
        /// Equivalent to .
        /// 
        public static UDouble Epsilon = double.Epsilon;
    
        /// 
        /// Represents the smallest possible value of  (0).
        /// 
        public static UDouble MinValue = 0d;
    
        /// 
        /// Represents the largest possible value of  (equivalent to ).
        /// 
        public static UDouble MaxValue = double.MaxValue;
    
        /// 
        /// Equivalent to .
        /// 
        public static UDouble NaN = double.NaN;
    
        /// 
        /// Equivalent to .
        /// 
        public static UDouble PositiveInfinity = double.PositiveInfinity;
    
        double value;
    
        public UDouble(double Value)
        {
            if (double.IsNegativeInfinity(Value))
                throw new NotSupportedException();
    
            value = Value < 0 ? 0 : Value;
        }
    
        public static implicit operator double(UDouble d)
        {
            return d.value;
        }
    
        public static implicit operator UDouble(double d)
        {
            return new UDouble(d);
        }
    
        public static bool operator <(UDouble a, UDouble b)
        {
            return a.value < b.value;
        }
    
        public static bool operator >(UDouble a, UDouble b)
        {
            return a.value > b.value;
        }
    
        public static bool operator ==(UDouble a, UDouble b)
        {
            return a.value == b.value;
        }
    
        public static bool operator !=(UDouble a, UDouble b)
        {
            return a.value != b.value;
        }
    
        public static bool operator <=(UDouble a, UDouble b)
        {
            return a.value <= b.value;
        }
    
        public static bool operator >=(UDouble a, UDouble b)
        {
            return a.value >= b.value;
        }
    
        public override bool Equals(object a)
        {
            return !(a is UDouble) ? false : this == (UDouble)a;
        }
    
        public override int GetHashCode()
        {
            return value.GetHashCode();
        }
    
        public override string ToString()
        {
            return value.ToString();
        }
    }
    

    As to why one would need an unsigned double, consider that width and height dimensions of UI elements cannot be negative in most applications as that would be illogical; why, then, support negative numbers where they're not needed?

    Some values like PositiveInfinity and NaN may still be applicable; therefore, we provide intuitive references to them. The big difference between double and UDouble is UDouble wouldn't need the constant NegativeInfinity (or at least I assume this much; I am not a mathematician, after all) and MinValue constant is simply 0. Also, Epsilon is positive, though, I am uncertain whether or not it is logical to use in the same context as unsigned numbers.

    Note, this implementation automatically truncates negative numbers and an exception is thrown if you attempt to set to NegativeInfinity.

提交回复
热议问题