如何在结构上指定 maxExclusive?
我想为 GPX 库创建一个结构 Degrees
。在 GPX 的 XSD 中 (GPX 1.1 Schema) DegreesType 定义为 minInclusive = 0 和 maxExclusive = 360。结构现在应有两个公共静态字段
MinValue = 0
和 MaxValue = x
:
public struct Degrees : IFormattable, IComparable, IComparable<Degrees>, IEquatable<Degrees>
{
private decimal value;
public static Degrees MinValue = 0M;
//public static Degrees MaxValue = x;
}
指定 x 值的最佳方法是什么? 360D-1 是不准确的,360D-0.001 是一种假设,没有人想要比 1/1000 度更好的精度。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以想到两种方法:
让你的结构忠实地代表
指定范围的事实
具有包容性最小值和
独占最大;即,给你的
结构体
MinInclusive
和MaxExclusive
成员。这可能被视为
教你的结构太多
XSD 的实现细节,
尽管
将
MaxValue
定义为小于360的最高可表示decimal
值。由于decimal
是十进制浮点类型,因此我们必须这里有点小心,但我认为我的说法是对的,因为最小的可能值是10^-28
,并且对于 360,我们有 10 的 2 次方小数点左边,相关值为360 - 10^-26
或我假设您正在处理从
十进制
到度
的转换。请注意,decimal
的类型声明字符为m
或M
-d
或D
> 代表double
。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
andMaxExclusive
members. This might be regarded as
teaching your struct too much about
the implementation detail of the XSD,
though
Define
MaxValue
as the highest representabledecimal
value less than 360. Sincedecimal
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 is10^-28
, and with 360 we have two powers of ten to the left of the decimal point, the relevant value is360 - 10^-26
, orI assume you're taking care of the conversion from
decimal
toDegree
. Note that the type declaration character fordecimal
ism
orM
-d
orD
is fordouble
.