2

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.

4

3 回答 3

3

这并不完全是对 XML 方面的回答,但是如果您想要一个 C# 类来表示有理数,我不久前写了一个非常灵活的类作为我的ExifUtils 库的一部分(因为大多数 EXIF 值都表示作为有理数)。

该类本身是通用的,接受任何实现类型的分子/分母IConvertable(包括所有 BCL 数字类型),并将序列化(ToString)和反序列化(Parse/TryParse),这可能会为您提供 XML 表示所需的确切内容。

如果您绝对必须用空格表示一个有理数,您可以将其调整为使用空格' '作为分隔符,并在源中更改单个字符。


作为对 Steven Lowe 评论的回应,除了稍微偏离主题之外,使用有理数虽然看似不直观,但有一些优势。诸如 PI 之类的数字也不能表示为十进制/浮点数。PI的近似值(例如 Math.PI 中的值)可以精确地表示为有理数:

314159265358979323846 / 100000000000000000000

而非常简单的有理数2/3不可能以与任何类型的浮点/固定精度十进制数相同的精度表示:

0.66666666666666666667
于 2009-12-19T18:01:32.503 回答
1

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

于 2008-11-14T21:08:37.640 回答
1

您可以在MathML中表达分数。那是行业标准AFAIK。

于 2008-11-14T21:35:07.777 回答