Skip to content

Commit 08abf98

Browse files
committed
Fix compile errors for Windows Runtime Component
Avoid precision issues in CompareTo() Use internal numeric type (decimal) and convert to base unit.
1 parent 310f995 commit 08abf98

File tree

89 files changed

+2344
-1415
lines changed

Some content is hidden

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

89 files changed

+2344
-1415
lines changed

UnitsNet/CustomCode/Quantities/AmplitudeRatio.extra.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public partial struct AmplitudeRatio
5656
"The base-10 logarithm of a number ≤ 0 is undefined. Voltage must be greater than 0 V.");
5757

5858
// E(dBV) = 20*log10(value(V)/reference(V))
59-
Value = 20 * Math.Log10(voltage.Volts / 1);
59+
_value = 20 * Math.Log10(voltage.Volts / 1);
6060
Unit = AmplitudeRatioUnit.DecibelVolt;
6161
}
6262

@@ -92,4 +92,4 @@ public static PowerRatio ToPowerRatio(AmplitudeRatio amplitudeRatio, ElectricRes
9292
return PowerRatio.FromDecibelWatts(amplitudeRatio.DecibelVolts - 10 * Math.Log10(impedance.Ohms / 1));
9393
}
9494
}
95-
}
95+
}

UnitsNet/CustomCode/Quantities/Level.extra.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ namespace UnitsNet
2727
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
2828
// Public structures can't have any members other than public fields, and those fields must be value types or strings.
2929
// Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic.
30-
#if WINDOWS_UWP
31-
public sealed partial class Level
32-
#else
30+
// Cannot have methods with same name and same number of parameters.
31+
#if !WINDOWS_UWP
3332
public partial struct Level
34-
#endif
3533
{
3634
/// <summary>
3735
/// Initializes a new instance of the logarithmic <see cref="Level" /> struct which is the ratio of a quantity Q to a
@@ -50,8 +48,9 @@ public Level(double quantity, double reference)
5048
if ((reference == 0) || ((quantity > 0) && (reference < 0)))
5149
throw new ArgumentOutOfRangeException(nameof(reference), errorMessage);
5250

53-
Value = 10*Math.Log10(quantity/reference);
51+
_value = 10*Math.Log10(quantity/reference);
5452
Unit = LevelUnit.Decibel;
5553
}
5654
}
57-
}
55+
#endif
56+
}

UnitsNet/CustomCode/Quantities/Molarity.extra.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial struct Molarity
2121
Molarity(Density density, Mass molecularWeight)
2222
: this()
2323
{
24-
Value = density.KilogramsPerCubicMeter / molecularWeight.Kilograms;
24+
_value = density.KilogramsPerCubicMeter / molecularWeight.Kilograms;
2525
Unit = MolarityUnit.MolesPerCubicMeter;
2626
}
2727

UnitsNet/CustomCode/Quantities/PowerRatio.extra.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public partial struct PowerRatio
5252
nameof(power), "The base-10 logarithm of a number ≤ 0 is undefined. Power must be greater than 0 W.");
5353

5454
// P(dBW) = 10*log10(value(W)/reference(W))
55-
Value = 10 * Math.Log10(power.Watts / 1);
55+
_value = 10 * Math.Log10(power.Watts / 1);
5656
Unit = PowerRatioUnit.DecibelWatt;
5757
}
5858

@@ -87,4 +87,4 @@ public static AmplitudeRatio ToAmplitudeRatio(PowerRatio powerRatio, ElectricRes
8787
return AmplitudeRatio.FromDecibelVolts(10 * Math.Log10(impedance.Ohms / 1) + powerRatio.DecibelWatts);
8888
}
8989
}
90-
}
90+
}

UnitsNet/CustomCode/UnitSystem.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ public UnitSystem() : this(DefaultCulture)
139139
/// Defaults to <see cref="CultureInfo.CurrentUICulture" /> when creating an instance with no culture provided.
140140
/// Can be overridden, but note that this is static and will affect all subsequent usages.
141141
/// </summary>
142-
public static IFormatProvider DefaultCulture { get; set; } = CultureInfo.CurrentUICulture;
142+
#if WINDOWS_UWP
143+
// Windows Runtime Component does not support exposing the IFormatProvider type in public API
144+
private
145+
#else
146+
public
147+
#endif
148+
static IFormatProvider DefaultCulture { get; set; } = CultureInfo.CurrentUICulture;
143149

144150
public bool IsFallbackCulture => Culture.Equals(FallbackCulture);
145151

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ public sealed partial class Acceleration
6969
public partial struct Acceleration : IComparable, IComparable<Acceleration>
7070
#endif
7171
{
72+
private readonly double _value;
73+
7274
/// <summary>
7375
/// The numeric value this quantity was constructed with.
7476
/// </summary>
75-
public double Value { get; }
77+
#if WINDOWS_UWP
78+
public double Value => Convert.ToDouble(_value);
79+
#else
80+
public double Value => _value;
81+
#endif
7682

7783
/// <summary>
7884
/// The unit this quantity was constructed with.
@@ -89,7 +95,7 @@ public Acceleration() : this(0, BaseUnit)
8995
[Obsolete("Use the constructor that takes a unit parameter. This constructor will be removed in a future version.")]
9096
public Acceleration(double meterpersecondsquared)
9197
{
92-
Value = Convert.ToDouble(meterpersecondsquared);
98+
_value = Convert.ToDouble(meterpersecondsquared);
9399
Unit = BaseUnit;
94100
}
95101

@@ -99,9 +105,14 @@ public Acceleration(double meterpersecondsquared)
99105
/// <param name="numericValue">Numeric value.</param>
100106
/// <param name="unit">Unit representation.</param>
101107
/// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
102-
public Acceleration(double numericValue, AccelerationUnit unit)
108+
#if WINDOWS_UWP
109+
private
110+
#else
111+
public
112+
#endif
113+
Acceleration(double numericValue, AccelerationUnit unit)
103114
{
104-
Value = numericValue;
115+
_value = numericValue;
105116
Unit = unit;
106117
}
107118

@@ -700,7 +711,7 @@ public int CompareTo(object obj)
700711
#endif
701712
int CompareTo(Acceleration other)
702713
{
703-
return Value.CompareTo(other.AsBaseNumericType(Unit));
714+
return AsBaseUnitMeterPerSecondSquared().CompareTo(other.AsBaseUnitMeterPerSecondSquared());
704715
}
705716

706717
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -748,7 +759,7 @@ public override bool Equals(object obj)
748759
return false;
749760
}
750761

751-
return Value.Equals(((Acceleration) obj).AsBaseNumericType(Unit));
762+
return AsBaseUnitMeterPerSecondSquared().Equals(((Acceleration) obj).AsBaseUnitMeterPerSecondSquared());
752763
}
753764

754765
/// <summary>
@@ -761,7 +772,7 @@ public override bool Equals(object obj)
761772
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
762773
public bool Equals(Acceleration other, Acceleration maxError)
763774
{
764-
return Math.Abs(Value - other.AsBaseNumericType(Unit)) <= maxError.AsBaseNumericType(Unit);
775+
return Math.Abs(AsBaseUnitMeterPerSecondSquared() - other.AsBaseUnitMeterPerSecondSquared()) <= maxError.AsBaseUnitMeterPerSecondSquared();
765776
}
766777

767778
public override int GetHashCode()
@@ -1071,23 +1082,23 @@ public string ToString(AccelerationUnit unit, [CanBeNull] Culture culture, [NotN
10711082
/// <returns>The value in the base unit representation.</returns>
10721083
private double AsBaseUnitMeterPerSecondSquared()
10731084
{
1074-
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return Value; }
1085+
if (Unit == AccelerationUnit.MeterPerSecondSquared) { return _value; }
10751086

10761087
switch (Unit)
10771088
{
1078-
case AccelerationUnit.CentimeterPerSecondSquared: return (Value) * 1e-2d;
1079-
case AccelerationUnit.DecimeterPerSecondSquared: return (Value) * 1e-1d;
1080-
case AccelerationUnit.FootPerSecondSquared: return Value*0.304800;
1081-
case AccelerationUnit.InchPerSecondSquared: return Value*0.0254;
1082-
case AccelerationUnit.KilometerPerSecondSquared: return (Value) * 1e3d;
1083-
case AccelerationUnit.KnotPerHour: return Value*0.5144444444444/3600;
1084-
case AccelerationUnit.KnotPerMinute: return Value*0.5144444444444/60;
1085-
case AccelerationUnit.KnotPerSecond: return Value*0.5144444444444;
1086-
case AccelerationUnit.MeterPerSecondSquared: return Value;
1087-
case AccelerationUnit.MicrometerPerSecondSquared: return (Value) * 1e-6d;
1088-
case AccelerationUnit.MillimeterPerSecondSquared: return (Value) * 1e-3d;
1089-
case AccelerationUnit.NanometerPerSecondSquared: return (Value) * 1e-9d;
1090-
case AccelerationUnit.StandardGravity: return Value*9.80665;
1089+
case AccelerationUnit.CentimeterPerSecondSquared: return (_value) * 1e-2d;
1090+
case AccelerationUnit.DecimeterPerSecondSquared: return (_value) * 1e-1d;
1091+
case AccelerationUnit.FootPerSecondSquared: return _value*0.304800;
1092+
case AccelerationUnit.InchPerSecondSquared: return _value*0.0254;
1093+
case AccelerationUnit.KilometerPerSecondSquared: return (_value) * 1e3d;
1094+
case AccelerationUnit.KnotPerHour: return _value*0.5144444444444/3600;
1095+
case AccelerationUnit.KnotPerMinute: return _value*0.5144444444444/60;
1096+
case AccelerationUnit.KnotPerSecond: return _value*0.5144444444444;
1097+
case AccelerationUnit.MeterPerSecondSquared: return _value;
1098+
case AccelerationUnit.MicrometerPerSecondSquared: return (_value) * 1e-6d;
1099+
case AccelerationUnit.MillimeterPerSecondSquared: return (_value) * 1e-3d;
1100+
case AccelerationUnit.NanometerPerSecondSquared: return (_value) * 1e-9d;
1101+
case AccelerationUnit.StandardGravity: return _value*9.80665;
10911102
default:
10921103
throw new NotImplementedException("Unit not implemented: " + Unit);
10931104
}

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ public sealed partial class AmountOfSubstance
6969
public partial struct AmountOfSubstance : IComparable, IComparable<AmountOfSubstance>
7070
#endif
7171
{
72+
private readonly double _value;
73+
7274
/// <summary>
7375
/// The numeric value this quantity was constructed with.
7476
/// </summary>
75-
public double Value { get; }
77+
#if WINDOWS_UWP
78+
public double Value => Convert.ToDouble(_value);
79+
#else
80+
public double Value => _value;
81+
#endif
7682

7783
/// <summary>
7884
/// The unit this quantity was constructed with.
@@ -89,7 +95,7 @@ public AmountOfSubstance() : this(0, BaseUnit)
8995
[Obsolete("Use the constructor that takes a unit parameter. This constructor will be removed in a future version.")]
9096
public AmountOfSubstance(double moles)
9197
{
92-
Value = Convert.ToDouble(moles);
98+
_value = Convert.ToDouble(moles);
9399
Unit = BaseUnit;
94100
}
95101

@@ -99,9 +105,14 @@ public AmountOfSubstance(double moles)
99105
/// <param name="numericValue">Numeric value.</param>
100106
/// <param name="unit">Unit representation.</param>
101107
/// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
102-
public AmountOfSubstance(double numericValue, AmountOfSubstanceUnit unit)
108+
#if WINDOWS_UWP
109+
private
110+
#else
111+
public
112+
#endif
113+
AmountOfSubstance(double numericValue, AmountOfSubstanceUnit unit)
103114
{
104-
Value = numericValue;
115+
_value = numericValue;
105116
Unit = unit;
106117
}
107118

@@ -733,7 +744,7 @@ public int CompareTo(object obj)
733744
#endif
734745
int CompareTo(AmountOfSubstance other)
735746
{
736-
return Value.CompareTo(other.AsBaseNumericType(Unit));
747+
return AsBaseUnitMoles().CompareTo(other.AsBaseUnitMoles());
737748
}
738749

739750
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -781,7 +792,7 @@ public override bool Equals(object obj)
781792
return false;
782793
}
783794

784-
return Value.Equals(((AmountOfSubstance) obj).AsBaseNumericType(Unit));
795+
return AsBaseUnitMoles().Equals(((AmountOfSubstance) obj).AsBaseUnitMoles());
785796
}
786797

787798
/// <summary>
@@ -794,7 +805,7 @@ public override bool Equals(object obj)
794805
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
795806
public bool Equals(AmountOfSubstance other, AmountOfSubstance maxError)
796807
{
797-
return Math.Abs(Value - other.AsBaseNumericType(Unit)) <= maxError.AsBaseNumericType(Unit);
808+
return Math.Abs(AsBaseUnitMoles() - other.AsBaseUnitMoles()) <= maxError.AsBaseUnitMoles();
798809
}
799810

800811
public override int GetHashCode()
@@ -1105,24 +1116,24 @@ public string ToString(AmountOfSubstanceUnit unit, [CanBeNull] Culture culture,
11051116
/// <returns>The value in the base unit representation.</returns>
11061117
private double AsBaseUnitMoles()
11071118
{
1108-
if (Unit == AmountOfSubstanceUnit.Mole) { return Value; }
1119+
if (Unit == AmountOfSubstanceUnit.Mole) { return _value; }
11091120

11101121
switch (Unit)
11111122
{
1112-
case AmountOfSubstanceUnit.Centimole: return (Value) * 1e-2d;
1113-
case AmountOfSubstanceUnit.CentipoundMole: return (Value*453.59237) * 1e-2d;
1114-
case AmountOfSubstanceUnit.Decimole: return (Value) * 1e-1d;
1115-
case AmountOfSubstanceUnit.DecipoundMole: return (Value*453.59237) * 1e-1d;
1116-
case AmountOfSubstanceUnit.Kilomole: return (Value) * 1e3d;
1117-
case AmountOfSubstanceUnit.KilopoundMole: return (Value*453.59237) * 1e3d;
1118-
case AmountOfSubstanceUnit.Micromole: return (Value) * 1e-6d;
1119-
case AmountOfSubstanceUnit.MicropoundMole: return (Value*453.59237) * 1e-6d;
1120-
case AmountOfSubstanceUnit.Millimole: return (Value) * 1e-3d;
1121-
case AmountOfSubstanceUnit.MillipoundMole: return (Value*453.59237) * 1e-3d;
1122-
case AmountOfSubstanceUnit.Mole: return Value;
1123-
case AmountOfSubstanceUnit.Nanomole: return (Value) * 1e-9d;
1124-
case AmountOfSubstanceUnit.NanopoundMole: return (Value*453.59237) * 1e-9d;
1125-
case AmountOfSubstanceUnit.PoundMole: return Value*453.59237;
1123+
case AmountOfSubstanceUnit.Centimole: return (_value) * 1e-2d;
1124+
case AmountOfSubstanceUnit.CentipoundMole: return (_value*453.59237) * 1e-2d;
1125+
case AmountOfSubstanceUnit.Decimole: return (_value) * 1e-1d;
1126+
case AmountOfSubstanceUnit.DecipoundMole: return (_value*453.59237) * 1e-1d;
1127+
case AmountOfSubstanceUnit.Kilomole: return (_value) * 1e3d;
1128+
case AmountOfSubstanceUnit.KilopoundMole: return (_value*453.59237) * 1e3d;
1129+
case AmountOfSubstanceUnit.Micromole: return (_value) * 1e-6d;
1130+
case AmountOfSubstanceUnit.MicropoundMole: return (_value*453.59237) * 1e-6d;
1131+
case AmountOfSubstanceUnit.Millimole: return (_value) * 1e-3d;
1132+
case AmountOfSubstanceUnit.MillipoundMole: return (_value*453.59237) * 1e-3d;
1133+
case AmountOfSubstanceUnit.Mole: return _value;
1134+
case AmountOfSubstanceUnit.Nanomole: return (_value) * 1e-9d;
1135+
case AmountOfSubstanceUnit.NanopoundMole: return (_value*453.59237) * 1e-9d;
1136+
case AmountOfSubstanceUnit.PoundMole: return _value*453.59237;
11261137
default:
11271138
throw new NotImplementedException("Unit not implemented: " + Unit);
11281139
}

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ public sealed partial class AmplitudeRatio
6969
public partial struct AmplitudeRatio : IComparable, IComparable<AmplitudeRatio>
7070
#endif
7171
{
72+
private readonly double _value;
73+
7274
/// <summary>
7375
/// The numeric value this quantity was constructed with.
7476
/// </summary>
75-
public double Value { get; }
77+
#if WINDOWS_UWP
78+
public double Value => Convert.ToDouble(_value);
79+
#else
80+
public double Value => _value;
81+
#endif
7682

7783
/// <summary>
7884
/// The unit this quantity was constructed with.
@@ -89,7 +95,7 @@ public AmplitudeRatio() : this(0, BaseUnit)
8995
[Obsolete("Use the constructor that takes a unit parameter. This constructor will be removed in a future version.")]
9096
public AmplitudeRatio(double decibelvolts)
9197
{
92-
Value = Convert.ToDouble(decibelvolts);
98+
_value = Convert.ToDouble(decibelvolts);
9399
Unit = BaseUnit;
94100
}
95101

@@ -99,9 +105,14 @@ public AmplitudeRatio(double decibelvolts)
99105
/// <param name="numericValue">Numeric value.</param>
100106
/// <param name="unit">Unit representation.</param>
101107
/// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
102-
public AmplitudeRatio(double numericValue, AmplitudeRatioUnit unit)
108+
#if WINDOWS_UWP
109+
private
110+
#else
111+
public
112+
#endif
113+
AmplitudeRatio(double numericValue, AmplitudeRatioUnit unit)
103114
{
104-
Value = numericValue;
115+
_value = numericValue;
105116
Unit = unit;
106117
}
107118

@@ -411,7 +422,7 @@ public int CompareTo(object obj)
411422
#endif
412423
int CompareTo(AmplitudeRatio other)
413424
{
414-
return Value.CompareTo(other.AsBaseNumericType(Unit));
425+
return AsBaseUnitDecibelVolts().CompareTo(other.AsBaseUnitDecibelVolts());
415426
}
416427

417428
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -459,7 +470,7 @@ public override bool Equals(object obj)
459470
return false;
460471
}
461472

462-
return Value.Equals(((AmplitudeRatio) obj).AsBaseNumericType(Unit));
473+
return AsBaseUnitDecibelVolts().Equals(((AmplitudeRatio) obj).AsBaseUnitDecibelVolts());
463474
}
464475

465476
/// <summary>
@@ -472,7 +483,7 @@ public override bool Equals(object obj)
472483
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
473484
public bool Equals(AmplitudeRatio other, AmplitudeRatio maxError)
474485
{
475-
return Math.Abs(Value - other.AsBaseNumericType(Unit)) <= maxError.AsBaseNumericType(Unit);
486+
return Math.Abs(AsBaseUnitDecibelVolts() - other.AsBaseUnitDecibelVolts()) <= maxError.AsBaseUnitDecibelVolts();
476487
}
477488

478489
public override int GetHashCode()
@@ -773,14 +784,14 @@ public string ToString(AmplitudeRatioUnit unit, [CanBeNull] Culture culture, [No
773784
/// <returns>The value in the base unit representation.</returns>
774785
private double AsBaseUnitDecibelVolts()
775786
{
776-
if (Unit == AmplitudeRatioUnit.DecibelVolt) { return Value; }
787+
if (Unit == AmplitudeRatioUnit.DecibelVolt) { return _value; }
777788

778789
switch (Unit)
779790
{
780-
case AmplitudeRatioUnit.DecibelMicrovolt: return Value - 120;
781-
case AmplitudeRatioUnit.DecibelMillivolt: return Value - 60;
782-
case AmplitudeRatioUnit.DecibelUnloaded: return Value - 2.218487499;
783-
case AmplitudeRatioUnit.DecibelVolt: return Value;
791+
case AmplitudeRatioUnit.DecibelMicrovolt: return _value - 120;
792+
case AmplitudeRatioUnit.DecibelMillivolt: return _value - 60;
793+
case AmplitudeRatioUnit.DecibelUnloaded: return _value - 2.218487499;
794+
case AmplitudeRatioUnit.DecibelVolt: return _value;
784795
default:
785796
throw new NotImplementedException("Unit not implemented: " + Unit);
786797
}

0 commit comments

Comments
 (0)