Skip to content

Commit 3af27fd

Browse files
authored
Do conversions with internal numeric type (#456)
Preserve accuracy for quantities using `decimal`.
2 parents 07c96ff + 5337332 commit 3af27fd

File tree

92 files changed

+3537
-3453
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

+3537
-3453
lines changed

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 42 additions & 41 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
@@ -879,14 +879,48 @@ public override int GetHashCode()
879879
/// <returns>Value converted to the specified unit.</returns>
880880
public double As(AccelerationUnit unit)
881881
{
882-
if (Unit == unit)
882+
if(Unit == unit)
883+
return Convert.ToDouble(Value);
884+
885+
var converted = AsBaseNumericType(unit);
886+
return Convert.ToDouble(converted);
887+
}
888+
889+
/// <summary>
890+
/// Converts the current value + unit to the base unit.
891+
/// This is typically the first step in converting from one unit to another.
892+
/// </summary>
893+
/// <returns>The value in the base unit representation.</returns>
894+
private double AsBaseUnit()
895+
{
896+
switch(Unit)
883897
{
884-
return (double)Value;
898+
case AccelerationUnit.CentimeterPerSecondSquared: return (_value) * 1e-2d;
899+
case AccelerationUnit.DecimeterPerSecondSquared: return (_value) * 1e-1d;
900+
case AccelerationUnit.FootPerSecondSquared: return _value*0.304800;
901+
case AccelerationUnit.InchPerSecondSquared: return _value*0.0254;
902+
case AccelerationUnit.KilometerPerSecondSquared: return (_value) * 1e3d;
903+
case AccelerationUnit.KnotPerHour: return _value*0.5144444444444/3600;
904+
case AccelerationUnit.KnotPerMinute: return _value*0.5144444444444/60;
905+
case AccelerationUnit.KnotPerSecond: return _value*0.5144444444444;
906+
case AccelerationUnit.MeterPerSecondSquared: return _value;
907+
case AccelerationUnit.MicrometerPerSecondSquared: return (_value) * 1e-6d;
908+
case AccelerationUnit.MillimeterPerSecondSquared: return (_value) * 1e-3d;
909+
case AccelerationUnit.NanometerPerSecondSquared: return (_value) * 1e-9d;
910+
case AccelerationUnit.StandardGravity: return _value*9.80665;
911+
default:
912+
throw new NotImplementedException($"Can not convert {Unit} to base units.");
885913
}
914+
}
915+
916+
private double AsBaseNumericType(AccelerationUnit unit)
917+
{
918+
if(Unit == unit)
919+
return _value;
886920

887-
double baseUnitValue = AsBaseUnitMetersPerSecondSquared();
921+
var baseUnitValue = AsBaseUnit();
888922

889-
switch (unit)
923+
switch(unit)
890924
{
891925
case AccelerationUnit.CentimeterPerSecondSquared: return (baseUnitValue) / 1e-2d;
892926
case AccelerationUnit.DecimeterPerSecondSquared: return (baseUnitValue) / 1e-1d;
@@ -901,9 +935,8 @@ public double As(AccelerationUnit unit)
901935
case AccelerationUnit.MillimeterPerSecondSquared: return (baseUnitValue) / 1e-3d;
902936
case AccelerationUnit.NanometerPerSecondSquared: return (baseUnitValue) / 1e-9d;
903937
case AccelerationUnit.StandardGravity: return baseUnitValue/9.80665;
904-
905938
default:
906-
throw new NotImplementedException("unit: " + unit);
939+
throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
907940
}
908941
}
909942

@@ -1253,38 +1286,6 @@ public string ToString(
12531286
/// </summary>
12541287
public static Acceleration MinValue => new Acceleration(double.MinValue, BaseUnit);
12551288

1256-
/// <summary>
1257-
/// Converts the current value + unit to the base unit.
1258-
/// This is typically the first step in converting from one unit to another.
1259-
/// </summary>
1260-
/// <returns>The value in the base unit representation.</returns>
1261-
private double AsBaseUnitMetersPerSecondSquared()
1262-
{
1263-
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return _value; }
1264-
1265-
switch (Unit)
1266-
{
1267-
case AccelerationUnit.CentimeterPerSecondSquared: return (_value) * 1e-2d;
1268-
case AccelerationUnit.DecimeterPerSecondSquared: return (_value) * 1e-1d;
1269-
case AccelerationUnit.FootPerSecondSquared: return _value*0.304800;
1270-
case AccelerationUnit.InchPerSecondSquared: return _value*0.0254;
1271-
case AccelerationUnit.KilometerPerSecondSquared: return (_value) * 1e3d;
1272-
case AccelerationUnit.KnotPerHour: return _value*0.5144444444444/3600;
1273-
case AccelerationUnit.KnotPerMinute: return _value*0.5144444444444/60;
1274-
case AccelerationUnit.KnotPerSecond: return _value*0.5144444444444;
1275-
case AccelerationUnit.MeterPerSecondSquared: return _value;
1276-
case AccelerationUnit.MicrometerPerSecondSquared: return (_value) * 1e-6d;
1277-
case AccelerationUnit.MillimeterPerSecondSquared: return (_value) * 1e-3d;
1278-
case AccelerationUnit.NanometerPerSecondSquared: return (_value) * 1e-9d;
1279-
case AccelerationUnit.StandardGravity: return _value*9.80665;
1280-
default:
1281-
throw new NotImplementedException("Unit not implemented: " + Unit);
1282-
}
1283-
}
1284-
1285-
/// <summary>Convenience method for working with internal numeric type.</summary>
1286-
private double AsBaseNumericType(AccelerationUnit unit) => Convert.ToDouble(As(unit));
1287-
12881289
/// <summary>
12891290
/// The <see cref="BaseDimensions" /> of this quantity.
12901291
/// </summary>

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 43 additions & 42 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
@@ -912,14 +912,49 @@ public override int GetHashCode()
912912
/// <returns>Value converted to the specified unit.</returns>
913913
public double As(AmountOfSubstanceUnit unit)
914914
{
915-
if (Unit == unit)
915+
if(Unit == unit)
916+
return Convert.ToDouble(Value);
917+
918+
var converted = AsBaseNumericType(unit);
919+
return Convert.ToDouble(converted);
920+
}
921+
922+
/// <summary>
923+
/// Converts the current value + unit to the base unit.
924+
/// This is typically the first step in converting from one unit to another.
925+
/// </summary>
926+
/// <returns>The value in the base unit representation.</returns>
927+
private double AsBaseUnit()
928+
{
929+
switch(Unit)
916930
{
917-
return (double)Value;
931+
case AmountOfSubstanceUnit.Centimole: return (_value) * 1e-2d;
932+
case AmountOfSubstanceUnit.CentipoundMole: return (_value*453.59237) * 1e-2d;
933+
case AmountOfSubstanceUnit.Decimole: return (_value) * 1e-1d;
934+
case AmountOfSubstanceUnit.DecipoundMole: return (_value*453.59237) * 1e-1d;
935+
case AmountOfSubstanceUnit.Kilomole: return (_value) * 1e3d;
936+
case AmountOfSubstanceUnit.KilopoundMole: return (_value*453.59237) * 1e3d;
937+
case AmountOfSubstanceUnit.Micromole: return (_value) * 1e-6d;
938+
case AmountOfSubstanceUnit.MicropoundMole: return (_value*453.59237) * 1e-6d;
939+
case AmountOfSubstanceUnit.Millimole: return (_value) * 1e-3d;
940+
case AmountOfSubstanceUnit.MillipoundMole: return (_value*453.59237) * 1e-3d;
941+
case AmountOfSubstanceUnit.Mole: return _value;
942+
case AmountOfSubstanceUnit.Nanomole: return (_value) * 1e-9d;
943+
case AmountOfSubstanceUnit.NanopoundMole: return (_value*453.59237) * 1e-9d;
944+
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
945+
default:
946+
throw new NotImplementedException($"Can not convert {Unit} to base units.");
918947
}
948+
}
949+
950+
private double AsBaseNumericType(AmountOfSubstanceUnit unit)
951+
{
952+
if(Unit == unit)
953+
return _value;
919954

920-
double baseUnitValue = AsBaseUnitMoles();
955+
var baseUnitValue = AsBaseUnit();
921956

922-
switch (unit)
957+
switch(unit)
923958
{
924959
case AmountOfSubstanceUnit.Centimole: return (baseUnitValue) / 1e-2d;
925960
case AmountOfSubstanceUnit.CentipoundMole: return (baseUnitValue/453.59237) / 1e-2d;
@@ -935,9 +970,8 @@ public double As(AmountOfSubstanceUnit unit)
935970
case AmountOfSubstanceUnit.Nanomole: return (baseUnitValue) / 1e-9d;
936971
case AmountOfSubstanceUnit.NanopoundMole: return (baseUnitValue/453.59237) / 1e-9d;
937972
case AmountOfSubstanceUnit.PoundMole: return baseUnitValue/453.59237;
938-
939973
default:
940-
throw new NotImplementedException("unit: " + unit);
974+
throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
941975
}
942976
}
943977

@@ -1287,39 +1321,6 @@ public string ToString(
12871321
/// </summary>
12881322
public static AmountOfSubstance MinValue => new AmountOfSubstance(double.MinValue, BaseUnit);
12891323

1290-
/// <summary>
1291-
/// Converts the current value + unit to the base unit.
1292-
/// This is typically the first step in converting from one unit to another.
1293-
/// </summary>
1294-
/// <returns>The value in the base unit representation.</returns>
1295-
private double AsBaseUnitMoles()
1296-
{
1297-
if (Unit == AmountOfSubstanceUnit.Mole) { return _value; }
1298-
1299-
switch (Unit)
1300-
{
1301-
case AmountOfSubstanceUnit.Centimole: return (_value) * 1e-2d;
1302-
case AmountOfSubstanceUnit.CentipoundMole: return (_value*453.59237) * 1e-2d;
1303-
case AmountOfSubstanceUnit.Decimole: return (_value) * 1e-1d;
1304-
case AmountOfSubstanceUnit.DecipoundMole: return (_value*453.59237) * 1e-1d;
1305-
case AmountOfSubstanceUnit.Kilomole: return (_value) * 1e3d;
1306-
case AmountOfSubstanceUnit.KilopoundMole: return (_value*453.59237) * 1e3d;
1307-
case AmountOfSubstanceUnit.Micromole: return (_value) * 1e-6d;
1308-
case AmountOfSubstanceUnit.MicropoundMole: return (_value*453.59237) * 1e-6d;
1309-
case AmountOfSubstanceUnit.Millimole: return (_value) * 1e-3d;
1310-
case AmountOfSubstanceUnit.MillipoundMole: return (_value*453.59237) * 1e-3d;
1311-
case AmountOfSubstanceUnit.Mole: return _value;
1312-
case AmountOfSubstanceUnit.Nanomole: return (_value) * 1e-9d;
1313-
case AmountOfSubstanceUnit.NanopoundMole: return (_value*453.59237) * 1e-9d;
1314-
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
1315-
default:
1316-
throw new NotImplementedException("Unit not implemented: " + Unit);
1317-
}
1318-
}
1319-
1320-
/// <summary>Convenience method for working with internal numeric type.</summary>
1321-
private double AsBaseNumericType(AmountOfSubstanceUnit unit) => Convert.ToDouble(As(unit));
1322-
13231324
/// <summary>
13241325
/// The <see cref="BaseDimensions" /> of this quantity.
13251326
/// </summary>

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public AmplitudeRatio(double decibelvolts)
113113
#else
114114
public
115115
#endif
116-
AmplitudeRatio(double numericValue, AmplitudeRatioUnit unit)
116+
AmplitudeRatio(double numericValue, AmplitudeRatioUnit 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>
@@ -450,7 +450,7 @@ public int CompareTo(object obj)
450450
#endif
451451
int CompareTo(AmplitudeRatio other)
452452
{
453-
return AsBaseUnitDecibelVolts().CompareTo(other.AsBaseUnitDecibelVolts());
453+
return AsBaseUnit().CompareTo(other.AsBaseUnit());
454454
}
455455

456456
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -580,22 +580,46 @@ public override int GetHashCode()
580580
/// <returns>Value converted to the specified unit.</returns>
581581
public double As(AmplitudeRatioUnit unit)
582582
{
583-
if (Unit == unit)
583+
if(Unit == unit)
584+
return Convert.ToDouble(Value);
585+
586+
var converted = AsBaseNumericType(unit);
587+
return Convert.ToDouble(converted);
588+
}
589+
590+
/// <summary>
591+
/// Converts the current value + unit to the base unit.
592+
/// This is typically the first step in converting from one unit to another.
593+
/// </summary>
594+
/// <returns>The value in the base unit representation.</returns>
595+
private double AsBaseUnit()
596+
{
597+
switch(Unit)
584598
{
585-
return (double)Value;
599+
case AmplitudeRatioUnit.DecibelMicrovolt: return _value - 120;
600+
case AmplitudeRatioUnit.DecibelMillivolt: return _value - 60;
601+
case AmplitudeRatioUnit.DecibelUnloaded: return _value - 2.218487499;
602+
case AmplitudeRatioUnit.DecibelVolt: return _value;
603+
default:
604+
throw new NotImplementedException($"Can not convert {Unit} to base units.");
586605
}
606+
}
607+
608+
private double AsBaseNumericType(AmplitudeRatioUnit unit)
609+
{
610+
if(Unit == unit)
611+
return _value;
587612

588-
double baseUnitValue = AsBaseUnitDecibelVolts();
613+
var baseUnitValue = AsBaseUnit();
589614

590-
switch (unit)
615+
switch(unit)
591616
{
592617
case AmplitudeRatioUnit.DecibelMicrovolt: return baseUnitValue + 120;
593618
case AmplitudeRatioUnit.DecibelMillivolt: return baseUnitValue + 60;
594619
case AmplitudeRatioUnit.DecibelUnloaded: return baseUnitValue + 2.218487499;
595620
case AmplitudeRatioUnit.DecibelVolt: return baseUnitValue;
596-
597621
default:
598-
throw new NotImplementedException("unit: " + unit);
622+
throw new NotImplementedException($"Can not convert {Unit} to {unit}.");
599623
}
600624
}
601625

@@ -945,28 +969,5 @@ public string ToString(
945969
/// </summary>
946970
public static AmplitudeRatio MinValue => new AmplitudeRatio(double.MinValue, BaseUnit);
947971

948-
/// <summary>
949-
/// Converts the current value + unit to the base unit.
950-
/// This is typically the first step in converting from one unit to another.
951-
/// </summary>
952-
/// <returns>The value in the base unit representation.</returns>
953-
private double AsBaseUnitDecibelVolts()
954-
{
955-
if (Unit == AmplitudeRatioUnit.DecibelVolt) { return _value; }
956-
957-
switch (Unit)
958-
{
959-
case AmplitudeRatioUnit.DecibelMicrovolt: return _value - 120;
960-
case AmplitudeRatioUnit.DecibelMillivolt: return _value - 60;
961-
case AmplitudeRatioUnit.DecibelUnloaded: return _value - 2.218487499;
962-
case AmplitudeRatioUnit.DecibelVolt: return _value;
963-
default:
964-
throw new NotImplementedException("Unit not implemented: " + Unit);
965-
}
966-
}
967-
968-
/// <summary>Convenience method for working with internal numeric type.</summary>
969-
private double AsBaseNumericType(AmplitudeRatioUnit unit) => Convert.ToDouble(As(unit));
970-
971972
}
972973
}

0 commit comments

Comments
 (0)