Skip to content

Commit 54be37d

Browse files
committed
- re-introduced the QuantityValue struct with all the implict constructors and numeric operators using the Fraction struct (hidden private field)
- replaced all occurrences of the Fraction with the QuantityValue (making it compatible with v5) - all tests (but for the DataContractSerializers and those regarding NaN/Infinity) are now passing with the original tolerance (almost no changes were required)
1 parent 37ca329 commit 54be37d

File tree

443 files changed

+12582
-9262
lines changed

Some content is hidden

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

443 files changed

+12582
-9262
lines changed

CodeGen/CodeGen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Fractions" Version="7.3.0" />
13+
<PackageReference Include="Fractions" Version="7.4.1" />
1414
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
1515
<PackageReference Include="NuGet.Protocol" Version="6.2.4" />
1616
<PackageReference Include="Serilog" Version="2.11.0" />

CodeGen/Generators/UnitsNetGen/NumberExtensionsGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static class NumberTo{_quantityName}Extensions
4545
continue;
4646

4747
Writer.WL(2, $@"
48-
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(double)"" />");
48+
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(QuantityValue)"" />");
4949

5050
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
5151

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public string Generate()
4343
using UnitsNet.InternalHelpers;
4444
using UnitsNet.Units;
4545
using System.Numerics;
46-
using Fractions;
4746
4847
#nullable enable
4948
@@ -89,7 +88,7 @@ namespace UnitsNet
8988
default:
9089
continue;
9190
}
92-
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name.Replace("double", "Fraction")}>,");
91+
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name.Replace("double", "QuantityValue")}>,");
9392
}
9493
}
9594

@@ -110,7 +109,7 @@ namespace UnitsNet
110109
/// The numeric value this quantity was constructed with.
111110
/// </summary>
112111
[DataMember(Name = ""Value"", Order = 1)]
113-
private readonly Fraction _value;
112+
private readonly QuantityValue _value;
114113
115114
/// <summary>
116115
/// The unit this quantity was constructed with.
@@ -202,7 +201,7 @@ private void GenerateInstanceConstructors()
202201
/// </summary>
203202
/// <param name=""value"">The numeric value to construct this quantity with.</param>
204203
/// <param name=""unit"">The unit representation to construct this quantity with.</param>
205-
public {_quantity.Name}(Fraction value, {_unitEnumName} unit)
204+
public {_quantity.Name}(QuantityValue value, {_unitEnumName} unit)
206205
{{");
207206
Writer.WL(@"
208207
_value = value;");
@@ -218,7 +217,7 @@ private void GenerateInstanceConstructors()
218217
/// <param name=""unitSystem"">The unit system to create the quantity with.</param>
219218
/// <exception cref=""ArgumentNullException"">The given <see cref=""UnitSystem""/> is null.</exception>
220219
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
221-
public {_quantity.Name}(Fraction value, UnitSystem unitSystem)
220+
public {_quantity.Name}(QuantityValue value, UnitSystem unitSystem)
222221
{{
223222
if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));
224223
@@ -289,10 +288,10 @@ private void GenerateProperties()
289288
/// <summary>
290289
/// The numeric value this quantity was constructed with.
291290
/// </summary>
292-
public Fraction Value => _value;
291+
public QuantityValue Value => _value;
293292
294293
/// <inheritdoc />
295-
Fraction IQuantity.Value => _value;
294+
QuantityValue IQuantity.Value => _value;
296295
297296
Enum IQuantity.Unit => Unit;
298297
@@ -329,7 +328,7 @@ private void GenerateConversionProperties()
329328
/// </summary>");
330329
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
331330
Writer.WL($@"
332-
public Fraction {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
331+
public QuantityValue {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
333332
");
334333
}
335334

@@ -420,7 +419,7 @@ private void GenerateStaticFactoryMethods()
420419
/// </summary>");
421420
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
422421
Writer.WL($@"
423-
public static {_quantity.Name} From{unit.PluralName}(Fraction value)
422+
public static {_quantity.Name} From{unit.PluralName}(QuantityValue value)
424423
{{
425424
return new {_quantity.Name}(value, {_unitEnumName}.{unit.SingularName});
426425
}}
@@ -434,7 +433,7 @@ private void GenerateStaticFactoryMethods()
434433
/// <param name=""value"">Value to convert from.</param>
435434
/// <param name=""fromUnit"">Unit to convert from.</param>
436435
/// <returns>{_quantity.Name} unit value.</returns>
437-
public static {_quantity.Name} From(Fraction value, {_unitEnumName} fromUnit)
436+
public static {_quantity.Name} From(QuantityValue value, {_unitEnumName} fromUnit)
438437
{{
439438
return new {_quantity.Name}(value, fromUnit);
440439
}}
@@ -609,7 +608,7 @@ private void GenerateArithmeticOperators()
609608
/// <summary>Negate the value.</summary>
610609
public static {_quantity.Name} operator -({_quantity.Name} right)
611610
{{
612-
return new {_quantity.Name}(right.Value.Invert(), right.Unit);
611+
return new {_quantity.Name}(-right.Value, right.Unit);
613612
}}
614613
615614
/// <summary>Get <see cref=""{_quantity.Name}""/> from adding two <see cref=""{_quantity.Name}""/>.</summary>
@@ -625,25 +624,25 @@ private void GenerateArithmeticOperators()
625624
}}
626625
627626
/// <summary>Get <see cref=""{_quantity.Name}""/> from multiplying value and <see cref=""{_quantity.Name}""/>.</summary>
628-
public static {_quantity.Name} operator *(Fraction left, {_quantity.Name} right)
627+
public static {_quantity.Name} operator *(QuantityValue left, {_quantity.Name} right)
629628
{{
630629
return new {_quantity.Name}(left * right.Value, right.Unit);
631630
}}
632631
633632
/// <summary>Get <see cref=""{_quantity.Name}""/> from multiplying value and <see cref=""{_quantity.Name}""/>.</summary>
634-
public static {_quantity.Name} operator *({_quantity.Name} left, Fraction right)
633+
public static {_quantity.Name} operator *({_quantity.Name} left, QuantityValue right)
635634
{{
636635
return new {_quantity.Name}(left.Value * right, left.Unit);
637636
}}
638637
639638
/// <summary>Get <see cref=""{_quantity.Name}""/> from dividing <see cref=""{_quantity.Name}""/> by value.</summary>
640-
public static {_quantity.Name} operator /({_quantity.Name} left, Fraction right)
639+
public static {_quantity.Name} operator /({_quantity.Name} left, QuantityValue right)
641640
{{
642641
return new {_quantity.Name}(left.Value / right, left.Unit);
643642
}}
644643
645644
/// <summary>Get ratio value from dividing <see cref=""{_quantity.Name}""/> by <see cref=""{_quantity.Name}""/>.</summary>
646-
public static Fraction operator /({_quantity.Name} left, {_quantity.Name} right)
645+
public static QuantityValue operator /({_quantity.Name} left, {_quantity.Name} right)
647646
{{
648647
return left.{_baseUnit.PluralName} / right.{_baseUnit.PluralName};
649648
}}
@@ -663,7 +662,7 @@ private void GenerateLogarithmicArithmeticOperators()
663662
/// <summary>Negate the value.</summary>
664663
public static {_quantity.Name} operator -({_quantity.Name} right)
665664
{{
666-
return new {_quantity.Name}(right.Value.Invert(), right.Unit);
665+
return new {_quantity.Name}(-right.Value, right.Unit);
667666
}}
668667
669668
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic addition of two <see cref=""{_quantity.Name}""/>.</summary>
@@ -672,8 +671,8 @@ private void GenerateLogarithmicArithmeticOperators()
672671
// Logarithmic addition
673672
// Formula: {x} * log10(10^(x/{x}) + 10^(y/{x}))
674673
// TODO see if we can switch to operating in linear space: left + right.ToUnit(left.Unit)
675-
return new {_quantity.Name}(10 * Fraction.FromDoubleRounded(Math.Log10(
676-
Math.Pow(10, (left.Value / 10).ToDouble()) + Math.Pow(10, (right.ToUnit(left.Unit).Value / 10).ToDouble()))),
674+
return new {_quantity.Name}({x} * QuantityValue.FromDoubleRounded(Math.Log10(
675+
Math.Pow(10, (left.Value / {x}).ToDouble()) + Math.Pow(10, (right.ToUnit(left.Unit).Value / {x}).ToDouble()))),
677676
left.Unit);
678677
}}
679678
@@ -683,35 +682,35 @@ private void GenerateLogarithmicArithmeticOperators()
683682
// Logarithmic subtraction
684683
// Formula: {x} * log10(10^(x/{x}) - 10^(y/{x}))
685684
// TODO see if we can switch to operating in linear space: left - right.ToUnit(left.Unit)
686-
return new {_quantity.Name}(10 * Fraction.FromDoubleRounded(Math.Log10(
687-
Math.Pow(10, (left.Value / 10).ToDouble()) - Math.Pow(10, (right.ToUnit(left.Unit).Value / 10).ToDouble()))),
685+
return new {_quantity.Name}({x} * QuantityValue.FromDoubleRounded(Math.Log10(
686+
Math.Pow(10, (left.Value / {x}).ToDouble()) - Math.Pow(10, (right.ToUnit(left.Unit).Value / {x}).ToDouble()))),
688687
left.Unit);
689688
}}
690689
691690
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic multiplication of value and <see cref=""{_quantity.Name}""/>.</summary>
692-
public static {_quantity.Name} operator *(Fraction left, {_quantity.Name} right)
691+
public static {_quantity.Name} operator *(QuantityValue left, {_quantity.Name} right)
693692
{{
694693
// Logarithmic multiplication = addition
695694
// TODO see if we can switch to operating in linear space: left * right.ToUnit(left.Unit)
696695
return new {_quantity.Name}(left + right.Value, right.Unit);
697696
}}
698697
699698
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic multiplication of value and <see cref=""{_quantity.Name}""/>.</summary>
700-
public static {_quantity.Name} operator *({_quantity.Name} left, Fraction right)
699+
public static {_quantity.Name} operator *({_quantity.Name} left, QuantityValue right)
701700
{{
702701
// Logarithmic multiplication = addition
703702
return new {_quantity.Name}(left.Value + right, left.Unit);
704703
}}
705704
706705
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic division of <see cref=""{_quantity.Name}""/> by value.</summary>
707-
public static {_quantity.Name} operator /({_quantity.Name} left, Fraction right)
706+
public static {_quantity.Name} operator /({_quantity.Name} left, QuantityValue right)
708707
{{
709708
// Logarithmic division = subtraction
710709
return new {_quantity.Name}(left.Value - right, left.Unit);
711710
}}
712711
713712
/// <summary>Get ratio value from logarithmic division of <see cref=""{_quantity.Name}""/> by <see cref=""{_quantity.Name}""/>.</summary>
714-
public static Fraction operator /({_quantity.Name} left, {_quantity.Name} right)
713+
public static QuantityValue operator /({_quantity.Name} left, {_quantity.Name} right)
715714
{{
716715
// Logarithmic division = subtraction
717716
return left.Value - right.ToUnit(left.Unit).Value;
@@ -741,7 +740,7 @@ private void GenerateRelationalOperators()
741740
/// <returns>The corresponding inverse quantity, <see cref=""{relation.RightQuantity.Name}""/>.</returns>
742741
public {relation.RightQuantity.Name} Inverse()
743742
{{
744-
return {relation.RightQuantity.Name}.From{relation.RightUnit.PluralName}({relation.LeftUnit.PluralName}.Reciprocal());
743+
return {relation.RightQuantity.Name}.From{relation.RightUnit.PluralName}(QuantityValue.Inverse({relation.LeftUnit.PluralName}));
745744
}}
746745
");
747746
}
@@ -765,13 +764,13 @@ private void GenerateRelationalOperators()
765764

766765
if (leftParameterName is "double")
767766
{
768-
leftParameterType = "Fraction";
767+
leftParameterType = "QuantityValue";
769768
leftParameterName = leftPart = "value";
770769
}
771770

772771
if (rightParameterName is "double")
773772
{
774-
rightParameterType = "Fraction";
773+
rightParameterType = "QuantityValue";
775774
rightParameterName = rightPart = "value";
776775
}
777776

@@ -780,7 +779,7 @@ private void GenerateRelationalOperators()
780779
var resultType = relation.ResultQuantity.Name;
781780
if (resultType is "double")
782781
{
783-
resultType = "Fraction";
782+
resultType = "QuantityValue";
784783
}
785784
else
786785
{
@@ -858,7 +857,7 @@ public override bool Equals(object? obj)
858857
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities.</summary>
859858
public bool Equals({_quantity.Name} other)
860859
{{
861-
return _value.IsEquivalentTo(other.As(this.Unit));
860+
return _value.Equals(other.As(this.Unit));
862861
}}
863862
864863
/// <summary>Compares the current <see cref=""{_quantity.Name}""/> with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit.</summary>
@@ -943,10 +942,10 @@ public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comp
943942
if (tolerance < 0)
944943
throw new ArgumentOutOfRangeException(nameof(tolerance), ""Tolerance must be greater than or equal to 0."");
945944
946-
return UnitsNet.FractionComparison.Equals(
945+
return UnitsNet.QuantityValueComparison.Equals(
947946
referenceValue: this.Value,
948947
otherValue: other.As(this.Unit),
949-
tolerance: (Fraction)tolerance,
948+
tolerance: (QuantityValue)tolerance,
950949
comparisonType: ComparisonType.Absolute);
951950
}}
952951
@@ -963,7 +962,7 @@ public bool Equals(IQuantity? other, IQuantity tolerance)
963962
/// <inheritdoc />
964963
public bool Equals({_quantity.Name} other, {_quantity.Name} tolerance)
965964
{{
966-
return UnitsNet.FractionComparison.Equals(
965+
return UnitsNet.QuantityValueComparison.Equals(
967966
referenceValue: this.Value,
968967
otherValue: other.As(this.Unit),
969968
tolerance: tolerance.As(this.Unit),
@@ -977,7 +976,11 @@ public bool Equals({_quantity.Name} other, {_quantity.Name} tolerance)
977976
public override int GetHashCode()
978977
{{
979978
var valueInBaseUnit = As(BaseUnit);
979+
#if NET7_0_OR_GREATER
980+
return HashCode.Combine(Info.Name, valueInBaseUnit);
981+
#else
980982
return new {{ Info.Name, valueInBaseUnit }}.GetHashCode();
983+
#endif
981984
}}
982985
983986
#endregion
@@ -993,7 +996,7 @@ private void GenerateConversionMethods()
993996
/// Convert to the unit representation <paramref name=""unit"" />.
994997
/// </summary>
995998
/// <returns>Value converted to the specified unit.</returns>
996-
public Fraction As({_unitEnumName} unit)
999+
public QuantityValue As({_unitEnumName} unit)
9971000
{{
9981001
if (Unit == unit)
9991002
return Value;
@@ -1005,7 +1008,7 @@ public Fraction As({_unitEnumName} unit)
10051008
Writer.WL($@"
10061009
10071010
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
1008-
public Fraction As(UnitSystem unitSystem)
1011+
public QuantityValue As(UnitSystem unitSystem)
10091012
{{
10101013
if (unitSystem is null)
10111014
throw new ArgumentNullException(nameof(unitSystem));
@@ -1022,7 +1025,7 @@ public Fraction As(UnitSystem unitSystem)
10221025

10231026
Writer.WL($@"
10241027
/// <inheritdoc />
1025-
Fraction IQuantity.As(Enum unit)
1028+
QuantityValue IQuantity.As(Enum unit)
10261029
{{
10271030
if (!(unit is {_unitEnumName} typedUnit))
10281031
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public string Generate()
2222
using System.Collections.Generic;
2323
using System.Diagnostics.CodeAnalysis;
2424
using System.Linq;
25-
using Fractions;
2625
2726
#nullable enable
2827
@@ -50,7 +49,7 @@ public partial class Quantity
5049
/// <param name=""quantityInfo"">The <see cref=""QuantityInfo""/> of the quantity to create.</param>
5150
/// <param name=""value"">The value to construct the quantity with.</param>
5251
/// <returns>The created quantity.</returns>
53-
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, Fraction value)
52+
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
5453
{
5554
return quantityInfo.Name switch
5655
{");
@@ -73,7 +72,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, Fraction val
7372
/// <param name=""unit"">Unit enum value.</param>
7473
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
7574
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
76-
public static bool TryFrom(Fraction value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity)
75+
public static bool TryFrom(QuantityValue value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity)
7776
{
7877
quantity = unit switch
7978
{");

0 commit comments

Comments
 (0)