0

I am trying to output flouting point numbers from c# in the format used by VRML 2.0.

According to the vrml spec, this is

double-precision floating point number in IEEE 1xxx floating point format (see 2.[IEEE]).

[IEEE]: ISO/IEC 9899:1990 Programming languages -- C. http://www.iso.ch/isob/switch-engine-cate.pl?searchtype=refnumber&KEYWORDS=9899 (dead link)

  • what is the correct string representation for this format? I am having trouble finding a reference. I found several explanations about the binary representation, but none about the string representation.
  • how can I configure a NumberFormatInfo or a CultureInfo in accordance with this format?

clarification:

I want to produce a vrml file and I need to know how to assure

var myCultureInfo = CultureInfo.InvariantCulture;
myCultureInfo.NumberFormat = ....
StringWriter sw = new StringWriter(MyCultureInfo)
sw.Write(myDouble);

will result in a number that is 100% in accordance to the vrml spezification.

HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • Do you mean, what is the correct string represenation *in VRML*? Because AFAIK the IEEE standard defines a binary representation, but there could be several string representations for the same number (e.g. `12.34`, `12,34`, `+12.34`, `1.234E+1`). – stakx - no longer contributing Aug 20 '12 at 09:21
  • Yes, I want to generate a VRML file, and I need to know how to format my numbers according to spec. Since VRML is a plain text format, I need to convert my number to a string – HugoRune Aug 20 '12 at 09:30

1 Answers1

2

I saw somewhere that VRML uses the same format for floating-point numbers as ISO C. Even the VRML spec page that you're mentioning hints at that:

The SFFloat field specifies one single-precision floating point number. MFFloat specifies zero or more single-precision floating point numbers. SFFloats and MFFloats are written to the VRML file in IEEE 1xxx floating point format (see 2.[IEEE]).

Note where that hyperlink points to: to the bibliography entry for ISO C.

So you should get the proper format if you simply disable any culture-specific formatting (such as special decimal points, characters that separate blocks of three digits each, etc.). You do this by using the "invariant culture":

double number = …;
string numberEncoding = number.ToString(CultureInfo.InvariantCulture.NumberFormat);

See also Standard Numeric Format Strings if you want to control the output format of the number, e.g.

  • for the scientific format:

    number.ToString("E", CultureInfo.InvariantCulture.NumberFormat)
    
  • for the round-trip format:

    number.ToString("R", CultureInfo.InvariantCulture.NumberFormat)
    

…and so on.

P.S.: If you don't quite trust that the invariant culture will be exactly the right match, you could always create your own instance of NumberFormatInfo:

var vrmlNumberFormat = new NumberFormatInfo { NumberDecimalSeparator = ".", … };
string numberEncoding = number.ToString(vrmlNumberFormat);
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • I did that, but it is not at all clear to me whether this will result in a number representation that is 100% compatible with VRML. In particular I am worried that the maximum allowed number of digits or the exponential formating used for very small numbers will be different – HugoRune Aug 20 '12 at 09:32
  • @HugoRune: If you can verify in the VRML standard that VRML indeed uses the same format for specifying floating-point numbers as ISO C, then I believe the .NET invariant culture will do what you need. – stakx - no longer contributing Aug 20 '12 at 09:59
  • well yes, but that is exactly the part I am having trouble verifying :) – HugoRune Aug 20 '12 at 10:08
  • @HugoRune: Updated beginning of my answer. – stakx - no longer contributing Aug 20 '12 at 10:12