From 092ae2529dd1d3603b811a56e7c7a1c4f6acb12c Mon Sep 17 00:00:00 2001 From: Johan Larsson Date: Fri, 7 Nov 2014 12:06:56 +0100 Subject: [PATCH] Added some tests & fixed bug in code generation for conversion formula. --- Gu.Units.Generator.Tests/QuantityTests.cs | 2 +- .../Descriptors/InverseOverload.cs | 29 ++ .../Descriptors/OperatorOverload.cs | 7 - Gu.Units.Generator/Descriptors/Quantity.cs | 11 +- Gu.Units.Generator/GeneratorSettings.xml | 4 +- Gu.Units.Generator/Gu.Units.Generator.csproj | 6 +- Gu.Units.Generator/MainWindow.xaml | 17 +- Gu.Units.Generator/Templates/Quantity.tt | 4 +- .../Templates/QuantityGenerator.txt | 6 +- Gu.Units.Generator/Templates/Unit.tt | 4 +- Gu.Units.Generator/Templates/Unit.txt | 94 ------- .../Templates/UnitGenerator.txt | 6 +- Gu.Units.Tests/ConversionTests.cs | 10 - Gu.Units.Tests/Helpers/ConversionProvider.cs | 10 + Gu.Units/Gu.Units.csproj | 12 +- Gu.Units/Properties/AssemblyInfo.cs | 4 +- Gu.Units/TemperatureUnit.generated.cs | 4 +- ...ion.generated.cs => Unitless.generated.cs} | 252 +++++++++--------- ...generated.cs => UnitlessUnit.generated.cs} | 36 +-- 19 files changed, 225 insertions(+), 293 deletions(-) create mode 100644 Gu.Units.Generator/Descriptors/InverseOverload.cs delete mode 100644 Gu.Units.Generator/Templates/Unit.txt rename Gu.Units/{Fraction.generated.cs => Unitless.generated.cs} (55%) rename Gu.Units/{FractionUnit.generated.cs => UnitlessUnit.generated.cs} (63%) diff --git a/Gu.Units.Generator.Tests/QuantityTests.cs b/Gu.Units.Generator.Tests/QuantityTests.cs index f7d058b3..84d64a7d 100644 --- a/Gu.Units.Generator.Tests/QuantityTests.cs +++ b/Gu.Units.Generator.Tests/QuantityTests.cs @@ -85,7 +85,7 @@ public void TimeOverloads() public void Inversions() { var actual = _settings.Time.Inverse; - Assert.AreEqual(_settings.Frequency.ToString(), actual.ToString()); + Assert.AreEqual("1 / Time = Frequency", actual.ToString()); Assert.IsNull(_settings.Length.Inverse); } } diff --git a/Gu.Units.Generator/Descriptors/InverseOverload.cs b/Gu.Units.Generator/Descriptors/InverseOverload.cs new file mode 100644 index 00000000..e7ecf8ca --- /dev/null +++ b/Gu.Units.Generator/Descriptors/InverseOverload.cs @@ -0,0 +1,29 @@ +namespace Gu.Units.Generator +{ + using System.Linq; + + public class InverseOverload + { + public InverseOverload(Quantity original, Quantity inverted) + { + this.Original = original; + this.Inverted = inverted; + } + + public Quantity Original { get; private set; } + + public Quantity Inverted { get; private set; } + + public override string ToString() + { + return string.Format("1 / {0} = {1}", this.Original.ClassName, this.Inverted.ClassName); + } + + public static bool IsInverse(Quantity left, Quantity right) + { + var leftParts = UnitParts.CreateFrom(left); + var rightParts = UnitParts.CreateFrom(right); + return leftParts.Flattened.SequenceEqual(rightParts.Flattened.Select(x => x ^ -1)); + } + } +} \ No newline at end of file diff --git a/Gu.Units.Generator/Descriptors/OperatorOverload.cs b/Gu.Units.Generator/Descriptors/OperatorOverload.cs index e7b7d014..a631218a 100644 --- a/Gu.Units.Generator/Descriptors/OperatorOverload.cs +++ b/Gu.Units.Generator/Descriptors/OperatorOverload.cs @@ -133,12 +133,5 @@ private int FindPower(Quantity left, Quantity right, Quantity result) // throw new ArgumentException("message"); //} } - - public static bool IsInverse(Quantity left, Quantity right) - { - var leftParts = UnitParts.CreateFrom(left); - var rightParts = UnitParts.CreateFrom(right); - return leftParts.Flattened.SequenceEqual(rightParts.Flattened.Select(x => x ^ -1)); - } } } \ No newline at end of file diff --git a/Gu.Units.Generator/Descriptors/Quantity.cs b/Gu.Units.Generator/Descriptors/Quantity.cs index 41c60c8b..033427d5 100644 --- a/Gu.Units.Generator/Descriptors/Quantity.cs +++ b/Gu.Units.Generator/Descriptors/Quantity.cs @@ -137,15 +137,20 @@ public IEnumerable OperatorOverloads } [XmlIgnore] - public Quantity Inverse + public InverseOverload Inverse { get { - if (Settings == null || Settings.Quantities == null) + if (Settings == null || Settings.Quantities == null) // For the template in designtime { return null; } - return Settings.Quantities.FirstOrDefault(x => x != null && OperatorOverload.IsInverse(this, x)); + var inverse = Settings.Quantities.FirstOrDefault(x => x != null && InverseOverload.IsInverse(this, x)); + if (inverse == null) + { + return null; + } + return new InverseOverload(this, inverse); } } diff --git a/Gu.Units.Generator/GeneratorSettings.xml b/Gu.Units.Generator/GeneratorSettings.xml index faa7ebaf..6e6e5cc1 100644 --- a/Gu.Units.Generator/GeneratorSettings.xml +++ b/Gu.Units.Generator/GeneratorSettings.xml @@ -1158,9 +1158,9 @@ - Fractions + Scalar ul - Fraction + Unitless PartsPerMillion diff --git a/Gu.Units.Generator/Gu.Units.Generator.csproj b/Gu.Units.Generator/Gu.Units.Generator.csproj index fba45b88..3964c766 100644 --- a/Gu.Units.Generator/Gu.Units.Generator.csproj +++ b/Gu.Units.Generator/Gu.Units.Generator.csproj @@ -83,6 +83,7 @@ + @@ -192,11 +193,6 @@ Designer - - True - True - Unit.tt - True True diff --git a/Gu.Units.Generator/MainWindow.xaml b/Gu.Units.Generator/MainWindow.xaml index 42058c56..43954ecf 100644 --- a/Gu.Units.Generator/MainWindow.xaml +++ b/Gu.Units.Generator/MainWindow.xaml @@ -157,13 +157,16 @@ - - - - - - - + + + + + + + + + + diff --git a/Gu.Units.Generator/Templates/Quantity.tt b/Gu.Units.Generator/Templates/Quantity.tt index 88b8fe45..bbf44e5c 100644 --- a/Gu.Units.Generator/Templates/Quantity.tt +++ b/Gu.Units.Generator/Templates/Quantity.tt @@ -157,9 +157,9 @@ namespace <#= Settings.Namespace #> { #> - public static <#= quantity.Inverse.ClassName #> operator/(double left, <#= quantity.ClassName #> right) + public static <#= quantity.Inverse.Inverted.ClassName #> operator/(double left, <#= quantity.ClassName #> right) { - return <#= quantity.Inverse.ClassName #>.From<#= quantity.Inverse.Unit.ClassName #>(left / right.<#= quantity.Unit.ClassName #>); + return <#= quantity.Inverse.Inverted.ClassName #>.From<#= quantity.Inverse.Inverted.Unit.ClassName #>(left / right.<#= quantity.Unit.ClassName #>); } <# } diff --git a/Gu.Units.Generator/Templates/QuantityGenerator.txt b/Gu.Units.Generator/Templates/QuantityGenerator.txt index cc61a8ce..32f35906 100644 --- a/Gu.Units.Generator/Templates/QuantityGenerator.txt +++ b/Gu.Units.Generator/Templates/QuantityGenerator.txt @@ -22,8 +22,6 @@ Read Settings found 29 units //-> Force.generated.cs -//-> Fraction.generated.cs - //-> Frequency.generated.cs //-> Inductance.generated.cs @@ -52,6 +50,8 @@ Read Settings found 29 units //-> Torque.generated.cs +//-> Unitless.generated.cs + //-> Voltage.generated.cs //-> Volume.generated.cs @@ -83,7 +83,7 @@ Read Settings found 29 units // Gu.Units\Gu.Units\Area.generated.cs // Gu.Units\Gu.Units\LuminousIntensity.generated.cs // Gu.Units\Gu.Units\Current.generated.cs -// Gu.Units\Gu.Units\Fraction.generated.cs +// Gu.Units\Gu.Units\Unitless.generated.cs // Gu.Units\Gu.Units\Angle.generated.cs // Gu.Units\Gu.Units\Temperature.generated.cs // Gu.Units\Gu.Units\Time.generated.cs diff --git a/Gu.Units.Generator/Templates/Unit.tt b/Gu.Units.Generator/Templates/Unit.tt index 280a7bf5..c308c6c2 100644 --- a/Gu.Units.Generator/Templates/Unit.tt +++ b/Gu.Units.Generator/Templates/Unit.tt @@ -162,7 +162,7 @@ namespace <#= Settings.Namespace #> if (unit.AnyOffsetConversion) { #> - return _conversionFactor * value + _offset; + return (value - _offset) / _conversionFactor; <# } else @@ -185,7 +185,7 @@ namespace <#= Settings.Namespace #> if (unit.AnyOffsetConversion) { #> - return (value - _offset) / _conversionFactor; + return _conversionFactor * value + _offset; <# } else diff --git a/Gu.Units.Generator/Templates/Unit.txt b/Gu.Units.Generator/Templates/Unit.txt deleted file mode 100644 index 4ac4bf76..00000000 --- a/Gu.Units.Generator/Templates/Unit.txt +++ /dev/null @@ -1,94 +0,0 @@ -namespace Gu.Units -{ - using System; - /// - /// A type for the unit . - /// Contains conversion logic. - /// - [Serializable] - public struct LengthUnit : IUnit - { - /// - /// The unit - /// Contains coonversion logic to from and formatting. - /// - public static readonly LengthUnit Metres = new LengthUnit(1.0, "m"); - /// - /// The unit - /// Contains conversion logic to from and formatting. - /// - public static readonly LengthUnit m = Metres; - - /// - /// The unit - /// Contains conversion logic to from and formatting. - /// - public static readonly LengthUnit Centimetres = new LengthUnit(0, "cm"); - /// - /// The unit - /// Contains coonversion logic to from and formatting. - /// - public static readonly LengthUnit cm = Centimetres; - - /// - /// The unit - /// Contains conversion logic to from and formatting. - /// - public static readonly LengthUnit Millimetres = new LengthUnit(0, "mm"); - /// - /// The unit - /// Contains coonversion logic to from and formatting. - /// - public static readonly LengthUnit mm = Millimetres; - - private readonly double _conversionFactor; - private readonly string _symbol; - - public LengthUnit(double conversionFactor, string symbol) - { - _conversionFactor = conversionFactor; - _symbol = symbol; - } - - /// - /// The symbol for . - /// - public string Symbol - { - get - { - return _symbol; - } - } - - public static Length operator *(double left, LengthUnit right) - { - return Length.From(left, right); - } - - /// - /// Converts a value to . - /// - /// - /// The converted value - public double ToSiUnit(double value) - { - return _conversionFactor * value; - } - - /// - /// Converts a value from Metres. - /// - /// The value in Metres - /// The converted value - public double FromSiUnit(double value) - { - return value / _conversionFactor; - } - - public override string ToString() - { - return string.Format("1{0} == {1}{2}", _symbol, this.ToSiUnit(1), Metres.Symbol); - } - } -} \ No newline at end of file diff --git a/Gu.Units.Generator/Templates/UnitGenerator.txt b/Gu.Units.Generator/Templates/UnitGenerator.txt index d38cd75f..5a21b231 100644 --- a/Gu.Units.Generator/Templates/UnitGenerator.txt +++ b/Gu.Units.Generator/Templates/UnitGenerator.txt @@ -22,8 +22,6 @@ Read Settings found 8 units //-> ForceUnit.generated.cs -//-> FractionUnit.generated.cs - //-> FrequencyUnit.generated.cs //-> InductanceUnit.generated.cs @@ -52,6 +50,8 @@ Read Settings found 8 units //-> TorqueUnit.generated.cs +//-> UnitlessUnit.generated.cs + //-> VoltageUnit.generated.cs //-> VolumetricFlowUnit.generated.cs @@ -83,7 +83,7 @@ Read Settings found 8 units // Gu.Units\Gu.Units\AreaUnit.generated.cs // Gu.Units\Gu.Units\LuminousIntensityUnit.generated.cs // Gu.Units\Gu.Units\CurrentUnit.generated.cs -// Gu.Units\Gu.Units\FractionUnit.generated.cs +// Gu.Units\Gu.Units\UnitlessUnit.generated.cs // Gu.Units\Gu.Units\AngleUnit.generated.cs // Gu.Units\Gu.Units\TemperatureUnit.generated.cs // Gu.Units\Gu.Units\TimeUnit.generated.cs diff --git a/Gu.Units.Tests/ConversionTests.cs b/Gu.Units.Tests/ConversionTests.cs index 445d4f34..2b603098 100644 --- a/Gu.Units.Tests/ConversionTests.cs +++ b/Gu.Units.Tests/ConversionTests.cs @@ -49,16 +49,6 @@ public void Convert(ConversionProvider.IConversion conversion) Assert.AreEqual(conversion.FromQuantity, conversion.ToQuantity); } - [Test, Explicit] - public void DegRadFactor() - { - var d = Math.PI / 180; - Console.WriteLine(d.ToString(CultureInfo.InvariantCulture)); - Console.WriteLine(d.ToString("G17", CultureInfo.InvariantCulture)); - Console.WriteLine(d.ToString("R", CultureInfo.InvariantCulture)); - Console.WriteLine((90 * d).ToString("R", CultureInfo.InvariantCulture)); - } - [TestCaseSource(typeof(UnitsProvider))] public void Roundtrip(IUnit unit) { diff --git a/Gu.Units.Tests/Helpers/ConversionProvider.cs b/Gu.Units.Tests/Helpers/ConversionProvider.cs index 755f2283..f27ed4a5 100644 --- a/Gu.Units.Tests/Helpers/ConversionProvider.cs +++ b/Gu.Units.Tests/Helpers/ConversionProvider.cs @@ -17,6 +17,16 @@ public ConversionProvider() new Conversion("1.2mm^2","1.2E-6m^2", Area.Parse), new Conversion("90°","1.5707963267948966rad", Angle.Parse), new Conversion("1°","0.017453292519943295rad", Angle.Parse), + new Conversion("0°C","32°F", Temperature.Parse), + new Conversion("100°C","211.99999999999994°F", Temperature.Parse), + new Conversion("100°F","37.777777777777828°C", Temperature.Parse), + new Conversion("0K","-273.15°C", Temperature.Parse), + new Conversion("0K","-459.67°F", Temperature.Parse), + new Conversion("100K","-173.14999999999998°C", Temperature.Parse), + new Conversion("100K","-279.67°F", Temperature.Parse), + new Conversion("1ul","100%", Unitless.Parse), + new Conversion("1%","0.01ul", Unitless.Parse), + new Conversion("1%","10‰", Unitless.Parse), }; } diff --git a/Gu.Units/Gu.Units.csproj b/Gu.Units/Gu.Units.csproj index 2c98c58c..9477f3df 100644 --- a/Gu.Units/Gu.Units.csproj +++ b/Gu.Units/Gu.Units.csproj @@ -124,12 +124,6 @@ UnitGenerator.txt4 - - QuantityGenerator.txt4 - - - UnitGenerator.txt4 - QuantityGenerator.txt4 @@ -217,6 +211,12 @@ UnitGenerator.txt4 + + QuantityGenerator.txt4 + + + UnitGenerator.txt4 + diff --git a/Gu.Units/Properties/AssemblyInfo.cs b/Gu.Units/Properties/AssemblyInfo.cs index 94a178e3..ab33db3f 100644 --- a/Gu.Units/Properties/AssemblyInfo.cs +++ b/Gu.Units/Properties/AssemblyInfo.cs @@ -33,6 +33,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.0.0")] -[assembly: AssemblyFileVersion("0.2.0.0")] +[assembly: AssemblyVersion("0.2.1.0")] +[assembly: AssemblyFileVersion("0.2.1.0")] [assembly: NeutralResourcesLanguageAttribute("en")] diff --git a/Gu.Units/TemperatureUnit.generated.cs b/Gu.Units/TemperatureUnit.generated.cs index 8b348464..a2bf54f9 100644 --- a/Gu.Units/TemperatureUnit.generated.cs +++ b/Gu.Units/TemperatureUnit.generated.cs @@ -65,7 +65,7 @@ public string Symbol /// The converted value public double ToSiUnit(double value) { - return _conversionFactor * value + _offset; + return (value - _offset) / _conversionFactor; } /// @@ -75,7 +75,7 @@ public double ToSiUnit(double value) /// The converted value public double FromSiUnit(double value) { - return (value - _offset) / _conversionFactor; + return _conversionFactor * value + _offset; } public override string ToString() diff --git a/Gu.Units/Fraction.generated.cs b/Gu.Units/Unitless.generated.cs similarity index 55% rename from Gu.Units/Fraction.generated.cs rename to Gu.Units/Unitless.generated.cs index 5ed40995..aec17d65 100644 --- a/Gu.Units/Fraction.generated.cs +++ b/Gu.Units/Unitless.generated.cs @@ -7,39 +7,39 @@ using System.Xml.Serialization; /// - /// A type for the quantity . + /// A type for the quantity . /// [Serializable] - public partial struct Fraction : IComparable, IEquatable, IFormattable, IXmlSerializable, IQuantity + public partial struct Unitless : IComparable, IEquatable, IFormattable, IXmlSerializable, IQuantity { /// - /// The quantity in . + /// The quantity in . /// - public readonly double Fractions; + public readonly double Scalar; - private Fraction(double fractions) + private Unitless(double scalar) { - Fractions = fractions; + Scalar = scalar; } /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// /// - /// . - public Fraction(double value, FractionUnit unit) + /// . + public Unitless(double value, UnitlessUnit unit) { - Fractions = unit.ToSiUnit(value); + Scalar = unit.ToSiUnit(value); } /// - /// The quantity in Fractions + /// The quantity in Scalar /// public double SiValue { get { - return Fractions; + return Scalar; } } @@ -50,7 +50,7 @@ public double PartsPerMillion { get { - return FractionUnit.PartsPerMillion.FromSiUnit(Fractions); + return UnitlessUnit.PartsPerMillion.FromSiUnit(Scalar); } } @@ -61,7 +61,7 @@ public double Promilles { get { - return FractionUnit.Promilles.FromSiUnit(Fractions); + return UnitlessUnit.Promilles.FromSiUnit(Scalar); } } @@ -72,240 +72,240 @@ public double Percents { get { - return FractionUnit.Percents.FromSiUnit(Fractions); + return UnitlessUnit.Percents.FromSiUnit(Scalar); } } /// - /// Creates an instance of from its string representation + /// Creates an instance of from its string representation /// - /// The string representation of the + /// The string representation of the /// - public static Fraction Parse(string s) + public static Unitless Parse(string s) { - return Parser.Parse(s, From); + return Parser.Parse(s, From); } /// - /// Reads an instance of from the + /// Reads an instance of from the /// /// - /// An instance of - public static Fraction ReadFrom(XmlReader reader) + /// An instance of + public static Unitless ReadFrom(XmlReader reader) { - var v = new Fraction(); + var v = new Unitless(); v.ReadXml(reader); return v; } /// - /// Creates a new instance of . + /// Creates a new instance of . /// /// /// - public static Fraction From(double value, FractionUnit unit) + public static Unitless From(double value, UnitlessUnit unit) { - return new Fraction(unit.ToSiUnit(value)); + return new Unitless(unit.ToSiUnit(value)); } /// - /// Creates a new instance of . + /// Creates a new instance of . /// - /// The value in - public static Fraction FromFractions(double fractions) + /// The value in + public static Unitless FromScalar(double scalar) { - return new Fraction(fractions); + return new Unitless(scalar); } /// - /// Creates a new instance of . + /// Creates a new instance of . /// /// The value in ppm - public static Fraction FromPartsPerMillion(double partsPerMillion) + public static Unitless FromPartsPerMillion(double partsPerMillion) { - return From(partsPerMillion, FractionUnit.PartsPerMillion); + return From(partsPerMillion, UnitlessUnit.PartsPerMillion); } /// - /// Creates a new instance of . + /// Creates a new instance of . /// /// The value in ‰ - public static Fraction FromPromilles(double promilles) + public static Unitless FromPromilles(double promilles) { - return From(promilles, FractionUnit.Promilles); + return From(promilles, UnitlessUnit.Promilles); } /// - /// Creates a new instance of . + /// Creates a new instance of . /// /// The value in % - public static Fraction FromPercents(double percents) + public static Unitless FromPercents(double percents) { - return From(percents, FractionUnit.Percents); + return From(percents, UnitlessUnit.Percents); } - public static double operator /(Fraction left, Fraction right) + public static double operator /(Unitless left, Unitless right) { - return left.Fractions / right.Fractions; + return left.Scalar / right.Scalar; } /// - /// Indicates whether two instances are equal. + /// Indicates whether two instances are equal. /// /// /// true if the quantitys of and are equal; otherwise, false. /// - /// A . - /// A . - public static bool operator ==(Fraction left, Fraction right) + /// A . + /// A . + public static bool operator ==(Unitless left, Unitless right) { return left.Equals(right); } /// - /// Indicates whether two instances are not equal. + /// Indicates whether two instances are not equal. /// /// /// true if the quantitys of and are not equal; otherwise, false. /// - /// A . - /// A . - public static bool operator !=(Fraction left, Fraction right) + /// A . + /// A . + public static bool operator !=(Unitless left, Unitless right) { return !left.Equals(right); } /// - /// Indicates whether a specified is less than another specified . + /// Indicates whether a specified is less than another specified . /// /// /// true if the quantity of is less than the quantity of ; otherwise, false. /// - /// An . - /// An . - public static bool operator <(Fraction left, Fraction right) + /// An . + /// An . + public static bool operator <(Unitless left, Unitless right) { - return left.Fractions < right.Fractions; + return left.Scalar < right.Scalar; } /// - /// Indicates whether a specified is greater than another specified . + /// Indicates whether a specified is greater than another specified . /// /// /// true if the quantity of is greater than the quantity of ; otherwise, false. /// - /// An . - /// An . - public static bool operator >(Fraction left, Fraction right) + /// An . + /// An . + public static bool operator >(Unitless left, Unitless right) { - return left.Fractions > right.Fractions; + return left.Scalar > right.Scalar; } /// - /// Indicates whether a specified is less than or equal to another specified . + /// Indicates whether a specified is less than or equal to another specified . /// /// /// true if the quantity of is less than or equal to the quantity of ; otherwise, false. /// - /// An . - /// An . - public static bool operator <=(Fraction left, Fraction right) + /// An . + /// An . + public static bool operator <=(Unitless left, Unitless right) { - return left.Fractions <= right.Fractions; + return left.Scalar <= right.Scalar; } /// - /// Indicates whether a specified is greater than or equal to another specified . + /// Indicates whether a specified is greater than or equal to another specified . /// /// /// true if the quantity of is greater than or equal to the quantity of ; otherwise, false. /// - /// An . - /// An . - public static bool operator >=(Fraction left, Fraction right) + /// An . + /// An . + public static bool operator >=(Unitless left, Unitless right) { - return left.Fractions >= right.Fractions; + return left.Scalar >= right.Scalar; } /// - /// Multiplies an instance of with and returns the result. + /// Multiplies an instance of with and returns the result. /// - /// An instance of + /// An instance of /// An instance of - /// Multiplies an instance of with and returns the result. - public static Fraction operator *(double left, Fraction right) + /// Multiplies an instance of with and returns the result. + public static Unitless operator *(double left, Unitless right) { - return new Fraction(left * right.Fractions); + return new Unitless(left * right.Scalar); } /// - /// Multiplies an instance of with and returns the result. + /// Multiplies an instance of with and returns the result. /// - /// An instance of + /// An instance of /// An instance of - /// Multiplies an instance of with and returns the result. - public static Fraction operator *(Fraction left, double right) + /// Multiplies an instance of with and returns the result. + public static Unitless operator *(Unitless left, double right) { - return new Fraction(left.Fractions * right); + return new Unitless(left.Scalar * right); } /// - /// Divides an instance of with and returns the result. + /// Divides an instance of with and returns the result. /// - /// An instance of + /// An instance of /// An instance of - /// Divides an instance of with and returns the result. - public static Fraction operator /(Fraction left, double right) + /// Divides an instance of with and returns the result. + public static Unitless operator /(Unitless left, double right) { - return new Fraction(left.Fractions / right); + return new Unitless(left.Scalar / right); } /// - /// Adds two specified instances. + /// Adds two specified instances. /// /// - /// An whose quantity is the sum of the quantitys of and . + /// An whose quantity is the sum of the quantitys of and . /// - /// A . + /// A . /// A TimeSpan. - public static Fraction operator +(Fraction left, Fraction right) + public static Unitless operator +(Unitless left, Unitless right) { - return new Fraction(left.Fractions + right.Fractions); + return new Unitless(left.Scalar + right.Scalar); } /// - /// Subtracts an Fraction from another Fraction and returns the difference. + /// Subtracts an Unitless from another Unitless and returns the difference. /// /// - /// An that is the difference + /// An that is the difference /// - /// A (the minuend). - /// A (the subtrahend). - public static Fraction operator -(Fraction left, Fraction right) + /// A (the minuend). + /// A (the subtrahend). + public static Unitless operator -(Unitless left, Unitless right) { - return new Fraction(left.Fractions - right.Fractions); + return new Unitless(left.Scalar - right.Scalar); } /// - /// Returns an whose quantity is the negated quantity of the specified instance. + /// Returns an whose quantity is the negated quantity of the specified instance. /// /// - /// An with the same numeric quantity as this instance, but the opposite sign. + /// An with the same numeric quantity as this instance, but the opposite sign. /// - /// A - public static Fraction operator -(Fraction Fraction) + /// A + public static Unitless operator -(Unitless Unitless) { - return new Fraction(-1 * Fraction.Fractions); + return new Unitless(-1 * Unitless.Scalar); } /// - /// Returns the specified instance of . + /// Returns the specified instance of . /// /// - /// Returns . + /// Returns . /// - /// A - public static Fraction operator +(Fraction Fraction) + /// A + public static Unitless operator +(Unitless Unitless) { - return Fraction; + return Unitless; } public override string ToString() @@ -325,17 +325,17 @@ public string ToString(IFormatProvider provider) public string ToString(string format, IFormatProvider formatProvider) { - return this.ToString(format, formatProvider, FractionUnit.Fractions); + return this.ToString(format, formatProvider, UnitlessUnit.Scalar); } - public string ToString(string format, IFormatProvider formatProvider, FractionUnit unit) + public string ToString(string format, IFormatProvider formatProvider, UnitlessUnit unit) { - var quantity = unit.FromSiUnit(this.Fractions); + var quantity = unit.FromSiUnit(this.Scalar); return string.Format("{0}{1}", quantity.ToString(format, formatProvider), unit.Symbol); } /// - /// Compares this instance to a specified object and returns an integer that indicates whether this is smaller than, equal to, or greater than the object. + /// Compares this instance to a specified object and returns an integer that indicates whether this is smaller than, equal to, or greater than the object. /// /// /// A signed number indicating the relative quantitys of this instance and . @@ -357,35 +357,35 @@ public string ToString(string format, IFormatProvider formatProvider, FractionUn /// This instance is larger than . /// /// - /// A object to compare to this instance. - public int CompareTo(Fraction quantity) + /// A object to compare to this instance. + public int CompareTo(Unitless quantity) { - return this.Fractions.CompareTo(quantity.Fractions); + return this.Scalar.CompareTo(quantity.Scalar); } /// - /// Returns a quantity indicating whether this instance is equal to a specified object. + /// Returns a quantity indicating whether this instance is equal to a specified object. /// /// - /// true if represents the same Fraction as this instance; otherwise, false. + /// true if represents the same Unitless as this instance; otherwise, false. /// - /// An object to compare with this instance. - public bool Equals(Fraction other) + /// An object to compare with this instance. + public bool Equals(Unitless other) { - return this.Fractions.Equals(other.Fractions); + return this.Scalar.Equals(other.Scalar); } /// - /// Returns a quantity indicating whether this instance is equal to a specified object within the given tolerance. + /// Returns a quantity indicating whether this instance is equal to a specified object within the given tolerance. /// /// - /// true if represents the same Fraction as this instance; otherwise, false. + /// true if represents the same Unitless as this instance; otherwise, false. /// - /// An object to compare with this instance. + /// An object to compare with this instance. /// The maximum difference for being considered equal - public bool Equals(Fraction other, double tolerance) + public bool Equals(Unitless other, double tolerance) { - return Math.Abs(this.Fractions - other.Fractions) < tolerance; + return Math.Abs(this.Scalar - other.Scalar) < tolerance; } public override bool Equals(object obj) @@ -395,12 +395,12 @@ public override bool Equals(object obj) return false; } - return obj is Fraction && this.Equals((Fraction)obj); + return obj is Unitless && this.Equals((Unitless)obj); } public override int GetHashCode() { - return this.Fractions.GetHashCode(); + return this.Scalar.GetHashCode(); } /// @@ -425,7 +425,7 @@ public XmlSchema GetSchema() public void ReadXml(XmlReader reader) { // Hacking set readonly fields here, can't think of a cleaner workaround - XmlExt.SetReadonlyField(ref this, "Fractions", reader, "Value"); + XmlExt.SetReadonlyField(ref this, "Scalar", reader, "Value"); } /// @@ -434,7 +434,7 @@ public void ReadXml(XmlReader reader) /// The stream to which the object is serialized. public void WriteXml(XmlWriter writer) { - XmlExt.WriteAttribute(writer, "Value", this.Fractions); + XmlExt.WriteAttribute(writer, "Value", this.Scalar); } } } diff --git a/Gu.Units/FractionUnit.generated.cs b/Gu.Units/UnitlessUnit.generated.cs similarity index 63% rename from Gu.Units/FractionUnit.generated.cs rename to Gu.Units/UnitlessUnit.generated.cs index 806e1ded..f903614c 100644 --- a/Gu.Units/FractionUnit.generated.cs +++ b/Gu.Units/UnitlessUnit.generated.cs @@ -2,57 +2,57 @@ { using System; /// - /// A type for the unit . + /// A type for the unit . /// Contains conversion logic. /// [Serializable] - public struct FractionUnit : IUnit + public struct UnitlessUnit : IUnit { /// - /// The unit + /// The unit /// Contains coonversion logic to from and formatting. /// - public static readonly FractionUnit Fractions = new FractionUnit(1.0, "ul"); + public static readonly UnitlessUnit Scalar = new UnitlessUnit(1.0, "ul"); /// - /// The unit + /// The unit /// Contains conversion logic to from and formatting. /// - public static readonly FractionUnit ul = Fractions; + public static readonly UnitlessUnit ul = Scalar; /// /// The unit /// Contains conversion logic to from and formatting. /// - public static readonly FractionUnit PartsPerMillion = new FractionUnit(1E-06, "ppm"); + public static readonly UnitlessUnit PartsPerMillion = new UnitlessUnit(1E-06, "ppm"); /// /// The unit /// Contains coonversion logic to from and formatting. /// - public static readonly FractionUnit ppm = PartsPerMillion; + public static readonly UnitlessUnit ppm = PartsPerMillion; /// /// The unit /// Contains conversion logic to from and formatting. /// - public static readonly FractionUnit Promilles = new FractionUnit(0.001, "‰"); + public static readonly UnitlessUnit Promilles = new UnitlessUnit(0.001, "‰"); /// /// The unit /// Contains conversion logic to from and formatting. /// - public static readonly FractionUnit Percents = new FractionUnit(0.01, "%"); + public static readonly UnitlessUnit Percents = new UnitlessUnit(0.01, "%"); private readonly double _conversionFactor; private readonly string _symbol; - public FractionUnit(double conversionFactor, string symbol) + public UnitlessUnit(double conversionFactor, string symbol) { _conversionFactor = conversionFactor; _symbol = symbol; } /// - /// The symbol for . + /// The symbol for . /// public string Symbol { @@ -62,13 +62,13 @@ public string Symbol } } - public static Fraction operator *(double left, FractionUnit right) + public static Unitless operator *(double left, UnitlessUnit right) { - return Fraction.From(left, right); + return Unitless.From(left, right); } /// - /// Converts a value to . + /// Converts a value to . /// /// /// The converted value @@ -78,9 +78,9 @@ public double ToSiUnit(double value) } /// - /// Converts a value from Fractions. + /// Converts a value from Scalar. /// - /// The value in Fractions + /// The value in Scalar /// The converted value public double FromSiUnit(double value) { @@ -89,7 +89,7 @@ public double FromSiUnit(double value) public override string ToString() { - return string.Format("1{0} == {1}{2}", _symbol, this.ToSiUnit(1), Fractions.Symbol); + return string.Format("1{0} == {1}{2}", _symbol, this.ToSiUnit(1), Scalar.Symbol); } } } \ No newline at end of file