C# 和 XML 中的有理数
我正在处理一个遵循行业标准的 XML 文件。 该架构的标准文档将其中一个字段定义为有理数,其数据表示为两个整数,通常第二个值为 1(例如
)。 我一直在寻找是否有 XML 定义的有理数标准,但没有取得很大成功。 我确实在 XML-SCHEMA 的邮件列表上找到了这个(8 年前的)交换:
http://markmail.org /message/znvfw2r3a2pfeykl
我不清楚是否有一个标准的“XML 方式”来表示有理数,以及适用于该文档的标准是否正在订阅它,或者他们是否已经制定了自己的方式这样做是为了他们的文档,并依靠人们阅读该标准。 该文件除了说该字段是一个有理数之外并没有具体说明。
假设有一种表示有理数的标准方法,并且本文档正确地实现了它,那么 System.Xml 中的功能是否可以识别它? 再说一遍,我的搜索并没有特别有成果。
感谢任何人的反馈。
I'm working with an XML file that subscribes to an industry standard. The standards document for the schema defines one of the fields as a rational number and its data is represented as two integers, typically with the second value being a 1 (e.g. <foo>20 1</foo>
). I've been hunting around without a great deal of success to see if there's an XML-defined standard for rational numbers. I did find this (8 year old) exchange on the mailing list for XML-SCHEMA:
http://markmail.org/message/znvfw2r3a2pfeykl
I'm not clear that there is a standard "XML way" for representing rational numbers and whether the standard applying to this document is subscribing to it, or whether they've cooked up their own way of doing it for their documents and are relying on people to read the standard. The document is not specific beyond saying the field is a rational number.
Assuming there is a standard way of representing rational numbers and this document is correctly implementing it, does the functionality in System.Xml recognize it? Again, my searches have not been particularly fruitful.
Thanks for any feedback anyone has.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不完全是 XML 方面的答案,但如果您想要一个 C# 类来表示有理数,我不久前编写了一个非常灵活的类,作为我的 ExifUtils 库(因为大多数 EXIF 值都表示为有理数)。
Rational
https://github.com/mckamey/exif-utils.net/blob/master/ExifUtils/ExifUtils/Rational.cs该类本身是通用的,接受任何实现 IConvertable 类型的分子/分母 (包括所有 BCL 数字类型),并将序列化 (ToString) 和反序列化 (Parse/TryParse),这可能会为您提供 XML 表示所需的内容。
如果您绝对必须用空格表示有理数,则可以对其进行调整,以使用空格
' '
作为分隔符,并在源中实际上更改单个字符。作为对史蒂文·洛(Steven Lowe)评论的回应,稍微偏离主题,使用有理数虽然看似不直观,但有一些优点。 PI 等数字也不能表示为小数/浮点数。 PI 的近似(例如 Math.PI 中的值)可以精确地表示为有理数:
而非常简单的有理数
2/3
是不可能的表示与任何类型的浮点/固定精度十进制数相同的精度:This isn't exactly an answer to the XML-side of things, but if you are wanting a C# class for representing rational numbers, I write a very flexible one a while back as part of my ExifUtils library (since most EXIF values are represented as rational numbers).
Rational<T>
https://github.com/mckamey/exif-utils.net/blob/master/ExifUtils/ExifUtils/Rational.csThe class itself is generic accepting a numerator/denominator of any type implementing
IConvertable
(which includes all BCL number types) and will serialize (ToString) and deserialize (Parse/TryParse) which may give you exactly what you need for your XML representation.If you absolutely must represent a rational number with a space, you could adapt it to use space
' '
as the delimiter with literally a single character change in the source.As a slightly off-topic aside in response to Steven Lowe's comments, the use of rational numbers while seemingly unintuitive has some advantages. Numbers such as PI cannot be represented as a decimal/floating point number either. The approximation of PI (e.g. the value in Math.PI) can be just as precisely represented as a rational number:
Whereas the very simple rational number
2/3
is impossible to represent to the same precision as any sort of floating point / fixed precision decimal number:我很高兴他们没有接受这个建议作为标准! 提议将所有其他数字基于“有理数”基元的人从未听说过超越数(例如 Pi),它不能以这种方式表示,
但回到你的问题 - 我只遇到过有理数xml 作为与电力行业相关的某些工程值的 RDF 规范的一部分。 我认为这只是一对用逗号分隔的数字
本文档将格式定义为 N/M,而另一个参考文献将其定义为 N,M
i'm glad they didn't accept this proposal as a standard! the guy proposing to base all other numbers on a 'rational number' primitive has never heard of transcendental numbers (like Pi, for example) which cannot be represented in this manner
but back to your question - i've only run across rational numbers in xml as part of an RDF specification for certain engineering values related to the power industry. I think it was just a pair of numbers separated by a comma
this document defines the format as N/M, while another reference has it as N,M
您可以在 MathML 中表达分数。 这就是行业标准 AFAIK。
You can express fractions in MathML. That is the industry standard AFAIK.