views:

42

answers:

1

I want to create a structure Degrees for a GPX library. In the XSD for GPX (GPX 1.1 Schema) degreesType is defined as minInclusive = 0 and maxExclusive = 360. The structure now shall have two public static fields MinValue = 0 and MaxValue = x:

public struct Degrees : IFormattable, IComparable, IComparable<Degrees>, IEquatable<Degrees>
{
    private decimal value;

    public static Degrees MinValue = 0M;
    //public static Degrees MaxValue = x;
}

What is the best way to specify the value of x? 360D-1 would be to inaccurate, 360D-0.001 would be an assumption that no one ever wants a better accuracy than 1/1000 degree.

+1  A: 

I can think of two approaches:

  • Have your struct faithfully represent the fact that the range is specified with an inclusive minimum and an exclusive maximum; ie, give your struct MinInclusive and MaxExclusive members. This might be regarded as teaching your struct too much about the implementation detail of the XSD, though

  • Define MaxValue as the highest representable decimal value less than 360. Since decimal is a decimal floating point type, we have to be a little careful here, but I think I'm right in saying that since the smallest possible value is 10^-28, and with 360 we have two powers of ten to the left of the decimal point, the relevant value is 360 - 10^-26, or

    const decimal MaxValue = 359.99999999999999999999999999m;
    

    I assume you're taking care of the conversion from decimal to Degree. Note that the type declaration character for decimal is m or M - d or D is for double.

AakashM
Yes, I knew about (and used) the M/m literal, until I was irritated today by the following sentence in the MSDN Library under the topic "D literal type character": Appending the literal type character D to a literal forces it to the Decimal data type. Appending the identifier type character @ to any identifier forces it to Decimal.Nevertheless I'm taking care of all conversions that have to take place. For the reason of clarity I did not include the whole structure definition.
PVitt
That page (http://msdn.microsoft.com/en-us/library/xtba3z33.aspx I guess) is about VB.net ...
AakashM