Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/29/09 14:56:51 (16 years ago)
Author:
epitzer
Message:

Updated handling of floating and fixed point numbers, faster and more accurate serialization and parsing. (#646)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleArray2XmlSerializer.cs

    r1940 r1958  
    11using System;
    22using HeuristicLab.Persistence.Core;
    3 using System.Globalization;
     3using HeuristicLab.Persistence.Default.Xml.Primitive;
    44
    55namespace HeuristicLab.Persistence.Default.Xml.Compact {
     
    88
    99    protected override string FormatValue(object o) {
    10       return ((double)o).ToString("r", CultureInfo.InvariantCulture);
     10      return Double2XmlSerializer.FormatG17((double)o);
    1111    }
    1212
    1313    protected override object ParseValue(string o) {
    14       if (o == CultureInfo.InvariantCulture.NumberFormat.NaNSymbol)
    15         return double.NaN;
    16       return double.Parse(o, CultureInfo.InvariantCulture);
     14      return Double2XmlSerializer.ParseG17(o);
    1715    }
    1816  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlSerializer.cs

    r1940 r1958  
    33using System;
    44using HeuristicLab.Persistence.Core;
    5 using System.Globalization;
     5using HeuristicLab.Persistence.Default.Xml.Primitive;
    66
    77namespace HeuristicLab.Persistence.Default.Xml.Compact {
     
    1818
    1919    protected override string FormatValue(object o) {
    20       return ((double)o).ToString("r", CultureInfo.InvariantCulture);
     20      return Double2XmlSerializer.FormatG17((double)o);
    2121    }
    2222
    2323    protected override object ParseValue(string o) {
    24       if (o == CultureInfo.InvariantCulture.NumberFormat.NaNSymbol)
    25         return double.NaN;
    26       return double.Parse(o, CultureInfo.InvariantCulture);
     24      return Double2XmlSerializer.ParseG17(o);
    2725    }
    2826
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Decimal2XmlSerializer.cs

    r1853 r1958  
    77namespace HeuristicLab.Persistence.Default.Xml.Primitive {
    88
    9   public class Decimal2XmlSerializer : DecimalNumber2XmlSerializerBase<decimal> { }
     9  public class Decimal2XmlSerializer : PrimitiveXmlSerializerBase<decimal> {
    1010
     11    public static decimal ParseG30(string s) {
     12      decimal d;
     13      if (decimal.TryParse(s,
     14        NumberStyles.AllowDecimalPoint |
     15        NumberStyles.AllowExponent |
     16        NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out d))
     17        return d;
     18      throw new FormatException(
     19        String.Format("Invalid decimal G30 number format \"{0}\" could not be parsed", s));
     20    }
     21
     22    public static string FormatG30(decimal d) {
     23      return d.ToString("g30", CultureInfo.InvariantCulture);
     24    }
     25
     26    public override XmlString Format(decimal d) {
     27      return new XmlString(FormatG30(d));
     28    }
     29
     30    public override decimal Parse(XmlString t) {
     31      return ParseG30(t.Data);
     32    }
     33  }
    1134}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Double2XmlSerializer.cs

    r1853 r1958  
    77namespace HeuristicLab.Persistence.Default.Xml.Primitive {
    88
    9   public class Double2XmlSerializer : DecimalNumber2XmlSerializerBase<double> { }
     9  public class Double2XmlSerializer : PrimitiveXmlSerializerBase<double> {
     10
     11    public static double ParseG17(string s) {
     12      double d;
     13      if (double.TryParse(s,
     14        NumberStyles.AllowDecimalPoint |
     15        NumberStyles.AllowExponent |
     16        NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out d))
     17        return d;
     18      throw new FormatException(
     19        String.Format("Invalid G17 number format \"{0}\" could not be parsed", s));
     20    }
     21
     22    public static string FormatG17(double d) {
     23      return d.ToString("g17", CultureInfo.InvariantCulture);
     24    }
     25
     26    public override XmlString Format(double d) {
     27      return new XmlString(FormatG17(d));
     28    }
     29
     30    public override double Parse(XmlString t) {
     31      return ParseG17(t.Data);
     32    }
     33  }
     34
     35 
     36 
    1037
    1138}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/Float2XmlSerializer.cs

    r1853 r1958  
    77namespace HeuristicLab.Persistence.Default.Xml.Primitive {
    88
    9   public class Float2XmlSerializer : DecimalNumber2XmlSerializerBase<float> { }
     9  public class Float2XmlSerializer : PrimitiveXmlSerializerBase<float> {
    1010
     11    public static float ParseG8(string s) {
     12      float f;
     13      if (float.TryParse(s,
     14        NumberStyles.AllowDecimalPoint |
     15        NumberStyles.AllowExponent |
     16        NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out f))
     17        return f;
     18      throw new FormatException(
     19        String.Format("Invalid float G8 number format \"{0}\" could not be parsed", s));
     20    }
     21
     22    public static string FormatG8(float f) {
     23      return f.ToString("g8", CultureInfo.InvariantCulture);
     24    }
     25
     26    public override XmlString Format(float f) {
     27      return new XmlString(FormatG8(f));
     28    }
     29
     30    public override float Parse(XmlString t) {
     31      return ParseG8(t.Data);
     32    }
     33  }
    1134}
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r1853 r1958  
    151151    <Compile Include="Default\Xml\Primitive\DateTime2XmlSerializer.cs" />
    152152    <Compile Include="Default\Xml\Primitive\Decimal2XmlSerializer.cs" />
    153     <Compile Include="Default\Xml\Primitive\DecimalNumber2XmlSerializerBase.cs" />
    154153    <Compile Include="Default\Xml\Primitive\Double2XmlSerializer.cs" />
    155154    <Compile Include="Default\Xml\Primitive\Float2XmlSerializer.cs" />
Note: See TracChangeset for help on using the changeset viewer.