views:

483

answers:

4

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.

+1  A: 

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

Steven A. Lowe
Don't use N,M to express rational numbers. Many parts of the world use a comma as a decimal separator. That is, 1/2 = 0,5
Dour High Arch
@[Dour High Arch]: I think he's going to have to use whatever representation his 'industry standard' uses - which appears to be a space!
Steven A. Lowe
A: 

Thanks, Steven. The document I'm working with has to do with digital media playback. It's looking like I'll need to work up my own method for parsing the value into a usable value, which is ok. I was just thinking if there were built-in way to do it, I'd be better off using it. Thanks again!

BTW - I did wonder about the implications of basing numeric types in the fashion that the poster in the link suggested. I'm no mathie, though, and I often find architectural conversations like that get away from me pretty quickly. :-)

BobC
+1  A: 

You can express fractions in MathML. That is the industry standard AFAIK.

Andrew Cowenhoven
A: 

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).

The 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:

314159265358979323846 / 100000000000000000000

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:

0.66666666666666666667
McKAMEY