Skip to content

Commit 69bfa75

Browse files
committed
Stop unnecessary conversion to/from base unit type. Only As() requires a double conversion.
1 parent eb4bb02 commit 69bfa75

File tree

92 files changed

+3719
-3635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3719
-3635
lines changed

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public Acceleration(double meterspersecondsquared)
113113
#else
114114
public
115115
#endif
116-
Acceleration(double numericValue, AccelerationUnit unit)
116+
Acceleration(double numericValue, AccelerationUnit unit)
117117
{
118118
_value = numericValue;
119119
_unit = unit;
120-
}
120+
}
121121

122122
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
123123
/// <summary>
@@ -749,7 +749,7 @@ public int CompareTo(object obj)
749749
#endif
750750
int CompareTo(Acceleration other)
751751
{
752-
return AsBaseUnitMetersPerSecondSquared().CompareTo(other.AsBaseUnitMetersPerSecondSquared());
752+
return AsBaseUnit().CompareTo(other.AsBaseUnit());
753753
}
754754

755755
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -797,7 +797,7 @@ public override bool Equals(object obj)
797797
return false;
798798
}
799799

800-
return AsBaseUnitMetersPerSecondSquared().Equals(((Acceleration) obj).AsBaseUnitMetersPerSecondSquared());
800+
return AsBaseUnit().Equals(((Acceleration) obj).AsBaseUnit());
801801
}
802802

803803
/// <summary>
@@ -810,7 +810,7 @@ public override bool Equals(object obj)
810810
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
811811
public bool Equals(Acceleration other, Acceleration maxError)
812812
{
813-
return Math.Abs(AsBaseUnitMetersPerSecondSquared() - other.AsBaseUnitMetersPerSecondSquared()) <= maxError.AsBaseUnitMetersPerSecondSquared();
813+
return Math.Abs(AsBaseUnit() - other.AsBaseUnit()) <= maxError.AsBaseUnit();
814814
}
815815

816816
public override int GetHashCode()
@@ -828,14 +828,48 @@ public override int GetHashCode()
828828
/// <returns>Value converted to the specified unit.</returns>
829829
public double As(AccelerationUnit unit)
830830
{
831-
if (Unit == unit)
831+
if(Unit == unit)
832+
return Convert.ToDouble(Value);
833+
834+
var converted = AsBaseNumericType(unit);
835+
return Convert.ToDouble(converted);
836+
}
837+
838+
/// <summary>
839+
/// Converts the current value + unit to the base unit.
840+
/// This is typically the first step in converting from one unit to another.
841+
/// </summary>
842+
/// <returns>The value in the base unit representation.</returns>
843+
private double AsBaseUnit()
844+
{
845+
switch(Unit)
832846
{
833-
return (double)Value;
847+
case AccelerationUnit.CentimeterPerSecondSquared: return (_value) * 1e-2d;
848+
case AccelerationUnit.DecimeterPerSecondSquared: return (_value) * 1e-1d;
849+
case AccelerationUnit.FootPerSecondSquared: return _value*0.304800;
850+
case AccelerationUnit.InchPerSecondSquared: return _value*0.0254;
851+
case AccelerationUnit.KilometerPerSecondSquared: return (_value) * 1e3d;
852+
case AccelerationUnit.KnotPerHour: return _value*0.5144444444444/3600;
853+
case AccelerationUnit.KnotPerMinute: return _value*0.5144444444444/60;
854+
case AccelerationUnit.KnotPerSecond: return _value*0.5144444444444;
855+
case AccelerationUnit.MeterPerSecondSquared: return _value;
856+
case AccelerationUnit.MicrometerPerSecondSquared: return (_value) * 1e-6d;
857+
case AccelerationUnit.MillimeterPerSecondSquared: return (_value) * 1e-3d;
858+
case AccelerationUnit.NanometerPerSecondSquared: return (_value) * 1e-9d;
859+
case AccelerationUnit.StandardGravity: return _value*9.80665;
860+
default:
861+
throw new NotImplementedException($"Can not convert {Unit} to base units.");
834862
}
863+
}
864+
865+
private double AsBaseNumericType(AccelerationUnit unit)
866+
{
867+
if(Unit == unit)
868+
return _value;
835869

836-
double baseUnitValue = AsBaseUnitMetersPerSecondSquared();
870+
var baseUnitValue = AsBaseUnit();
837871

838-
switch (unit)
872+
switch(unit)
839873
{
840874
case AccelerationUnit.CentimeterPerSecondSquared: return (baseUnitValue) / 1e-2d;
841875
case AccelerationUnit.DecimeterPerSecondSquared: return (baseUnitValue) / 1e-1d;
@@ -850,9 +884,8 @@ public double As(AccelerationUnit unit)
850884
case AccelerationUnit.MillimeterPerSecondSquared: return (baseUnitValue) / 1e-3d;
851885
case AccelerationUnit.NanometerPerSecondSquared: return (baseUnitValue) / 1e-9d;
852886
case AccelerationUnit.StandardGravity: return baseUnitValue/9.80665;
853-
854887
default:
855-
throw new NotImplementedException("unit: " + unit);
888+
throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
856889
}
857890
}
858891

@@ -1202,38 +1235,6 @@ public string ToString(
12021235
/// </summary>
12031236
public static Acceleration MinValue => new Acceleration(double.MinValue, BaseUnit);
12041237

1205-
/// <summary>
1206-
/// Converts the current value + unit to the base unit.
1207-
/// This is typically the first step in converting from one unit to another.
1208-
/// </summary>
1209-
/// <returns>The value in the base unit representation.</returns>
1210-
private double AsBaseUnitMetersPerSecondSquared()
1211-
{
1212-
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return _value; }
1213-
1214-
switch (Unit)
1215-
{
1216-
case AccelerationUnit.CentimeterPerSecondSquared: return (_value) * 1e-2d;
1217-
case AccelerationUnit.DecimeterPerSecondSquared: return (_value) * 1e-1d;
1218-
case AccelerationUnit.FootPerSecondSquared: return _value*0.304800;
1219-
case AccelerationUnit.InchPerSecondSquared: return _value*0.0254;
1220-
case AccelerationUnit.KilometerPerSecondSquared: return (_value) * 1e3d;
1221-
case AccelerationUnit.KnotPerHour: return _value*0.5144444444444/3600;
1222-
case AccelerationUnit.KnotPerMinute: return _value*0.5144444444444/60;
1223-
case AccelerationUnit.KnotPerSecond: return _value*0.5144444444444;
1224-
case AccelerationUnit.MeterPerSecondSquared: return _value;
1225-
case AccelerationUnit.MicrometerPerSecondSquared: return (_value) * 1e-6d;
1226-
case AccelerationUnit.MillimeterPerSecondSquared: return (_value) * 1e-3d;
1227-
case AccelerationUnit.NanometerPerSecondSquared: return (_value) * 1e-9d;
1228-
case AccelerationUnit.StandardGravity: return _value*9.80665;
1229-
default:
1230-
throw new NotImplementedException("Unit not implemented: " + Unit);
1231-
}
1232-
}
1233-
1234-
/// <summary>Convenience method for working with internal numeric type.</summary>
1235-
private double AsBaseNumericType(AccelerationUnit unit) => Convert.ToDouble(As(unit));
1236-
12371238
/// <summary>
12381239
/// The <see cref="BaseDimensions" /> of this quantity.
12391240
/// </summary>

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public AmountOfSubstance(double moles)
113113
#else
114114
public
115115
#endif
116-
AmountOfSubstance(double numericValue, AmountOfSubstanceUnit unit)
116+
AmountOfSubstance(double numericValue, AmountOfSubstanceUnit unit)
117117
{
118118
_value = numericValue;
119119
_unit = unit;
120-
}
120+
}
121121

122122
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
123123
/// <summary>
@@ -782,7 +782,7 @@ public int CompareTo(object obj)
782782
#endif
783783
int CompareTo(AmountOfSubstance other)
784784
{
785-
return AsBaseUnitMoles().CompareTo(other.AsBaseUnitMoles());
785+
return AsBaseUnit().CompareTo(other.AsBaseUnit());
786786
}
787787

788788
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -830,7 +830,7 @@ public override bool Equals(object obj)
830830
return false;
831831
}
832832

833-
return AsBaseUnitMoles().Equals(((AmountOfSubstance) obj).AsBaseUnitMoles());
833+
return AsBaseUnit().Equals(((AmountOfSubstance) obj).AsBaseUnit());
834834
}
835835

836836
/// <summary>
@@ -843,7 +843,7 @@ public override bool Equals(object obj)
843843
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
844844
public bool Equals(AmountOfSubstance other, AmountOfSubstance maxError)
845845
{
846-
return Math.Abs(AsBaseUnitMoles() - other.AsBaseUnitMoles()) <= maxError.AsBaseUnitMoles();
846+
return Math.Abs(AsBaseUnit() - other.AsBaseUnit()) <= maxError.AsBaseUnit();
847847
}
848848

849849
public override int GetHashCode()
@@ -861,14 +861,49 @@ public override int GetHashCode()
861861
/// <returns>Value converted to the specified unit.</returns>
862862
public double As(AmountOfSubstanceUnit unit)
863863
{
864-
if (Unit == unit)
864+
if(Unit == unit)
865+
return Convert.ToDouble(Value);
866+
867+
var converted = AsBaseNumericType(unit);
868+
return Convert.ToDouble(converted);
869+
}
870+
871+
/// <summary>
872+
/// Converts the current value + unit to the base unit.
873+
/// This is typically the first step in converting from one unit to another.
874+
/// </summary>
875+
/// <returns>The value in the base unit representation.</returns>
876+
private double AsBaseUnit()
877+
{
878+
switch(Unit)
865879
{
866-
return (double)Value;
880+
case AmountOfSubstanceUnit.Centimole: return (_value) * 1e-2d;
881+
case AmountOfSubstanceUnit.CentipoundMole: return (_value*453.59237) * 1e-2d;
882+
case AmountOfSubstanceUnit.Decimole: return (_value) * 1e-1d;
883+
case AmountOfSubstanceUnit.DecipoundMole: return (_value*453.59237) * 1e-1d;
884+
case AmountOfSubstanceUnit.Kilomole: return (_value) * 1e3d;
885+
case AmountOfSubstanceUnit.KilopoundMole: return (_value*453.59237) * 1e3d;
886+
case AmountOfSubstanceUnit.Micromole: return (_value) * 1e-6d;
887+
case AmountOfSubstanceUnit.MicropoundMole: return (_value*453.59237) * 1e-6d;
888+
case AmountOfSubstanceUnit.Millimole: return (_value) * 1e-3d;
889+
case AmountOfSubstanceUnit.MillipoundMole: return (_value*453.59237) * 1e-3d;
890+
case AmountOfSubstanceUnit.Mole: return _value;
891+
case AmountOfSubstanceUnit.Nanomole: return (_value) * 1e-9d;
892+
case AmountOfSubstanceUnit.NanopoundMole: return (_value*453.59237) * 1e-9d;
893+
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
894+
default:
895+
throw new NotImplementedException($"Can not convert {Unit} to base units.");
867896
}
897+
}
898+
899+
private double AsBaseNumericType(AmountOfSubstanceUnit unit)
900+
{
901+
if(Unit == unit)
902+
return _value;
868903

869-
double baseUnitValue = AsBaseUnitMoles();
904+
var baseUnitValue = AsBaseUnit();
870905

871-
switch (unit)
906+
switch(unit)
872907
{
873908
case AmountOfSubstanceUnit.Centimole: return (baseUnitValue) / 1e-2d;
874909
case AmountOfSubstanceUnit.CentipoundMole: return (baseUnitValue/453.59237) / 1e-2d;
@@ -884,9 +919,8 @@ public double As(AmountOfSubstanceUnit unit)
884919
case AmountOfSubstanceUnit.Nanomole: return (baseUnitValue) / 1e-9d;
885920
case AmountOfSubstanceUnit.NanopoundMole: return (baseUnitValue/453.59237) / 1e-9d;
886921
case AmountOfSubstanceUnit.PoundMole: return baseUnitValue/453.59237;
887-
888922
default:
889-
throw new NotImplementedException("unit: " + unit);
923+
throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
890924
}
891925
}
892926

@@ -1236,39 +1270,6 @@ public string ToString(
12361270
/// </summary>
12371271
public static AmountOfSubstance MinValue => new AmountOfSubstance(double.MinValue, BaseUnit);
12381272

1239-
/// <summary>
1240-
/// Converts the current value + unit to the base unit.
1241-
/// This is typically the first step in converting from one unit to another.
1242-
/// </summary>
1243-
/// <returns>The value in the base unit representation.</returns>
1244-
private double AsBaseUnitMoles()
1245-
{
1246-
if (Unit == AmountOfSubstanceUnit.Mole) { return _value; }
1247-
1248-
switch (Unit)
1249-
{
1250-
case AmountOfSubstanceUnit.Centimole: return (_value) * 1e-2d;
1251-
case AmountOfSubstanceUnit.CentipoundMole: return (_value*453.59237) * 1e-2d;
1252-
case AmountOfSubstanceUnit.Decimole: return (_value) * 1e-1d;
1253-
case AmountOfSubstanceUnit.DecipoundMole: return (_value*453.59237) * 1e-1d;
1254-
case AmountOfSubstanceUnit.Kilomole: return (_value) * 1e3d;
1255-
case AmountOfSubstanceUnit.KilopoundMole: return (_value*453.59237) * 1e3d;
1256-
case AmountOfSubstanceUnit.Micromole: return (_value) * 1e-6d;
1257-
case AmountOfSubstanceUnit.MicropoundMole: return (_value*453.59237) * 1e-6d;
1258-
case AmountOfSubstanceUnit.Millimole: return (_value) * 1e-3d;
1259-
case AmountOfSubstanceUnit.MillipoundMole: return (_value*453.59237) * 1e-3d;
1260-
case AmountOfSubstanceUnit.Mole: return _value;
1261-
case AmountOfSubstanceUnit.Nanomole: return (_value) * 1e-9d;
1262-
case AmountOfSubstanceUnit.NanopoundMole: return (_value*453.59237) * 1e-9d;
1263-
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
1264-
default:
1265-
throw new NotImplementedException("Unit not implemented: " + Unit);
1266-
}
1267-
}
1268-
1269-
/// <summary>Convenience method for working with internal numeric type.</summary>
1270-
private double AsBaseNumericType(AmountOfSubstanceUnit unit) => Convert.ToDouble(As(unit));
1271-
12721273
/// <summary>
12731274
/// The <see cref="BaseDimensions" /> of this quantity.
12741275
/// </summary>

0 commit comments

Comments
 (0)