Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have an Unsigned Double?

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

Does anyone know why?

like image 889
Amit Raz Avatar asked Sep 05 '11 08:09

Amit Raz


People also ask

Does hep C go away?

It is highly curable ... Direct-acting antiviral medications — given over a 12-week period — actually can cure early acute hepatitis C better than 90% of the time.

What is || in C programming?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .

How long does hep C take to show up?

The hepatitis C (HCV) window period is usually 4–10 weeks from the time of exposure. After 6 months , most people will have developed enough antibodies for an HCV test to detect. In rare cases, however, antibodies can take up to 9 months to develop.


2 Answers

As pointed out by Anders Forsgren, there is no unsigned doubles in the IEEE spec (and therefore not in C#).

You can always get the positive value by calling Math.Abs() and you could wrap a double in a struct and enforce the constraint there:

public struct PositiveDouble  {       private double _value;       public PositiveDouble() {}       public PositiveDouble(double val)        {           // or truncate/take Abs value automatically?           if (val < 0)               throw new ArgumentException("Value needs to be positive");           _value = val;       }        // This conversion is safe, we can make it implicit       public static implicit operator double(PositiveDouble d)       {           return d._value;       }       // This conversion is not always safe, so we make it explicit       public static explicit operator PositiveDouble(double d)       {           // or truncate/take Abs value automatically?           if (d < 0)               throw new ArgumentOutOfRangeException("Only positive values allowed");           return new PositiveDouble(d);       }       // add more cast operators if needed } 
like image 146
Isak Savo Avatar answered Sep 22 '22 21:09

Isak Savo


Floating point numbers are simply the implementation of the IEEE 754 spec. There is no such thing as an unsigned double there as far as i know.

http://en.wikipedia.org/wiki/IEEE_754-2008

Why do you need an unsigned floating point number?

like image 37
Anders Forsgren Avatar answered Sep 19 '22 21:09

Anders Forsgren