static void Verify(string ieeeHex, string expected) { while (ieeeHex.Length < 16) { ieeeHex = '0' + ieeeHex; } double ieeeF64 = BitConverter.Int64BitsToDouble((long)Convert.ToUInt64(ieeeHex, 16)); try { String es6Created = NumberToJson.SerializeNumber(ieeeF64); if (!es6Created.Equals(expected)) { conversionErrors++; Console.WriteLine("ES6={0,-24:S} C#={1,-24:S} Original=" + ieeeHex, expected, es6Created); } else { if (ieeeF64 != double.Parse(expected, System.Globalization.CultureInfo.InvariantCulture)) { Console.WriteLine("ES6={0,-24:S} C#={1,-24:S} Original=" + ieeeHex, expected, es6Created); } } } catch (ArgumentException) { if (!expected.Equals(INVALID_NUMBER)) { conversionErrors++; Console.WriteLine("ES6={0,-24:S} Original=" + ieeeHex, expected); } } }
void Serialize(object o) { if (o is SortedDictionary <string, object> ) { buffer.Append('{'); bool next = false; foreach (var keyValuePair in (SortedDictionary <string, object>)o) { if (next) { buffer.Append(','); } next = true; SerializeString(keyValuePair.Key); buffer.Append(':'); Serialize(keyValuePair.Value); } buffer.Append('}'); } else if (o is List <object> ) { buffer.Append('['); bool next = false; foreach (object value in (List <object>)o) { if (next) { buffer.Append(','); } next = true; Serialize(value); } buffer.Append(']'); } else if (o == null) { buffer.Append("null"); } else if (o is String) { SerializeString((string)o); } else if (o is Boolean) { buffer.Append(o.ToString().ToLowerInvariant()); } else if (o is Double) { buffer.Append(NumberToJson.SerializeNumber((Double)o)); } else { throw new InvalidOperationException("Unknown object: " + o); } }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("es6cli {xhhhhhhhhh | floating point number}"); Environment.Exit(0); } string inData = args[0]; double value; if (inData.StartsWith("x")) { string origIeeeHex = inData.Substring(1); while (origIeeeHex.Length < 16) { origIeeeHex = '0' + origIeeeHex; } ulong origBin = Convert.ToUInt64(origIeeeHex, 16); value = BitConverter.Int64BitsToDouble((long)origBin); } else { value = double.Parse(inData, System.Globalization.CultureInfo.InvariantCulture); } string es6 = NumberToJson.SerializeNumber(value); ulong ieeeLong = (ulong)BitConverter.DoubleToInt64Bits(value); ulong ulongMask = 0x8000000000000000L; string binary = ""; for (int counter = 0; counter < 64; counter++) { binary += (ulongMask & ieeeLong) == 0 ? '0' : '1'; ulongMask >>= 1; if (counter == 0 || counter == 11) { binary += ' '; } } string hex = ieeeLong.ToString("x16"); Console.WriteLine("G17=" + value.ToString("G17") + " Hex=" + hex + " ES6=" + es6 + "\nBinary=" + binary); }
static void Main(string[] args) { using (StreamReader sr = new StreamReader("c:\\es6\\numbers\\es6testfile100m.txt")) { string line; // Read test lines from the file until EOF is reached long counter = 0; long fails = 0; while ((line = sr.ReadLine()) != null) { // Each line contains // Hexadecimal,Number\n // where Hexadecimal is the IEEE-754 double precision // equivalent of an optimal (ES6 compliant) Number string origIeeeHex = line.Substring(0, line.IndexOf(',')); while (origIeeeHex.Length < 16) { origIeeeHex = '0' + origIeeeHex; } ulong origBin = Convert.ToUInt64(origIeeeHex, 16); double orig = BitConverter.Int64BitsToDouble((long)origBin); string es6Representation = line.Substring(line.IndexOf(',') + 1); if (++counter % 100000 == 0) { Console.WriteLine("Count=" + counter); } String serializedNumber = NumberToJson.SerializeNumber(orig); if (!serializedNumber.Equals(es6Representation)) { fails++; Console.WriteLine("ES6={0,-24:S} C#={1,-24:S} Original=" + origIeeeHex, es6Representation, serializedNumber); } } Console.WriteLine("Number of failures: " + fails); } }