@@ -25,7 +25,7 @@ public QuantityGenerator(Quantity quantity)
25
25
throw new ArgumentException ( $ "No unit found with SingularName equal to BaseUnit [{ _quantity . BaseUnit } ]. This unit must be defined.",
26
26
nameof ( quantity ) ) ;
27
27
28
- _valueType = "QuantityValue" ; // quantity.ValueType;
28
+ _valueType = quantity . ValueType ;
29
29
_unitEnumName = $ "{ quantity . Name } Unit";
30
30
31
31
BaseDimensions baseDimensions = quantity . BaseDimensions ;
@@ -77,14 +77,14 @@ public partial struct {_quantity.Name} : IQuantity<{_unitEnumName}>, ");
77
77
Writer . W ( "IDecimalQuantity, " ) ;
78
78
}
79
79
80
- Writer . WL ( $ "IEquatable< { _quantity . Name } >, IComparable, IComparable<{ _quantity . Name } >, IConvertible, IFormattable") ;
80
+ Writer . WL ( $ "IComparable, IComparable<{ _quantity . Name } >, IConvertible, IFormattable") ;
81
81
Writer . WL ( $@ "
82
82
{{
83
83
/// <summary>
84
84
/// The numeric value this quantity was constructed with.
85
85
/// </summary>
86
86
[DataMember(Name = ""Value"", Order = 0)]
87
- private readonly { _valueType } _value;
87
+ private readonly { _quantity . ValueType } _value;
88
88
89
89
/// <summary>
90
90
/// The unit this quantity was constructed with.
@@ -176,9 +176,9 @@ private void GenerateInstanceConstructors()
176
176
/// <param name=""value"">The numeric value to construct this quantity with.</param>
177
177
/// <param name=""unit"">The unit representation to construct this quantity with.</param>
178
178
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>
179
- public { _quantity . Name } ({ _valueType } value, { _unitEnumName } unit)
179
+ public { _quantity . Name } ({ _quantity . ValueType } value, { _unitEnumName } unit)
180
180
{{" ) ;
181
- Writer . WL ( _valueType == "double"
181
+ Writer . WL ( _quantity . ValueType == "double"
182
182
? @"
183
183
_value = Guard.EnsureValidNumber(value, nameof(value));"
184
184
: @"
@@ -203,7 +203,7 @@ private void GenerateInstanceConstructors()
203
203
var firstUnitInfo = unitInfos.FirstOrDefault();
204
204
" ) ;
205
205
206
- Writer . WL ( _valueType == "double"
206
+ Writer . WL ( _quantity . ValueType == "double"
207
207
? @"
208
208
_value = Guard.EnsureValidNumber(value, nameof(value));"
209
209
: @"
@@ -262,15 +262,15 @@ private void GenerateProperties()
262
262
public { _valueType } Value => _value;
263
263
" ) ;
264
264
265
- Writer . WL ( $@ "
266
- /// <inheritdoc />
267
- { _valueType } IQuantity.Value => _value;
268
- " ) ;
269
265
// Need to provide explicit interface implementation for decimal quantities like Information
266
+ if ( _quantity . ValueType != "double" )
267
+ Writer . WL ( @"
268
+ double IQuantity.Value => (double) _value;
269
+ " ) ;
270
270
if ( _quantity . ValueType == "decimal" )
271
271
Writer . WL ( @"
272
272
/// <inheritdoc cref=""IDecimalQuantity.Value""/>
273
- decimal IDecimalQuantity.Value => (decimal) _value;
273
+ decimal IDecimalQuantity.Value => _value;
274
274
" ) ;
275
275
276
276
Writer . WL ( $@ "
@@ -306,11 +306,11 @@ private void GenerateConversionProperties()
306
306
307
307
Writer . WL ( $@ "
308
308
/// <summary>
309
- /// Gets the numeric value of this quantity converted into <see cref=""{ _unitEnumName } .{ unit . SingularName } ""/>
309
+ /// Gets a <see cref=""double""/> value of this quantity converted into <see cref=""{ _unitEnumName } .{ unit . SingularName } ""/>
310
310
/// </summary>" ) ;
311
311
Writer . WLIfText ( 2 , GetObsoleteAttributeOrNull ( unit ) ) ;
312
312
Writer . WL ( $@ "
313
- public { _valueType } { unit . PluralName } => As({ _unitEnumName } .{ unit . SingularName } );
313
+ public double { unit . PluralName } => As({ _unitEnumName } .{ unit . SingularName } );
314
314
" ) ;
315
315
}
316
316
@@ -426,7 +426,7 @@ private void GenerateStaticFactoryMethods()
426
426
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>" ) ;
427
427
Writer. WLIfText ( 2 , GetObsoleteAttributeOrNull ( unit ) ) ;
428
428
Writer. WL ( $@ "
429
- public static { _quantity . Name } From{ unit . PluralName } ({ _valueType } { valueParamName } )
429
+ public static { _quantity . Name } From{ unit . PluralName } (QuantityValue { valueParamName } )
430
430
{{
431
431
{ _valueType } value = ({ _valueType } ) { valueParamName } ;
432
432
return new { _quantity . Name } (value, { _unitEnumName } .{ unit . SingularName } );
@@ -651,7 +651,7 @@ private void GenerateArithmeticOperators()
651
651
}}
652
652
653
653
/// <summary>Get ratio value from dividing <see cref=""{ _quantity . Name } ""/> by <see cref=""{ _quantity . Name } ""/>.</summary>
654
- public static { _valueType } operator /({ _quantity . Name } left, { _quantity . Name } right)
654
+ public static double operator /({ _quantity . Name } left, { _quantity . Name } right)
655
655
{{
656
656
return left.{ _baseUnit . PluralName } / right.{ _baseUnit . PluralName } ;
657
657
}}
@@ -679,15 +679,15 @@ private void GenerateLogarithmicArithmeticOperators()
679
679
{{
680
680
// Logarithmic addition
681
681
// Formula: { x } * log10(10^(x/{ x } ) + 10^(y/{ x } ))
682
- return new { _quantity . Name } ({ x } * Math.Log10(Math.Pow(10, (double) left.Value/{ x } ) + Math.Pow(10, (double) right.GetValueAs(left.Unit)/{ x } )), left.Unit);
682
+ return new { _quantity . Name } ({ x } * Math.Log10(Math.Pow(10, left.Value/{ x } ) + Math.Pow(10, right.GetValueAs(left.Unit)/{ x } )), left.Unit);
683
683
}}
684
684
685
685
/// <summary>Get <see cref=""{ _quantity . Name } ""/> from logarithmic subtraction of two <see cref=""{ _quantity . Name } ""/>.</summary>
686
686
public static { _quantity . Name } operator -({ _quantity . Name } left, { _quantity . Name } right)
687
687
{{
688
688
// Logarithmic subtraction
689
689
// Formula: { x } * log10(10^(x/{ x } ) - 10^(y/{ x } ))
690
- return new { _quantity . Name } ({ x } * Math.Log10(Math.Pow(10, (double) left.Value/{ x } ) - Math.Pow(10, (double) right.GetValueAs(left.Unit)/{ x } )), left.Unit);
690
+ return new { _quantity . Name } ({ x } * Math.Log10(Math.Pow(10, left.Value/{ x } ) - Math.Pow(10, right.GetValueAs(left.Unit)/{ x } )), left.Unit);
691
691
}}
692
692
693
693
/// <summary>Get <see cref=""{ _quantity . Name } ""/> from logarithmic multiplication of value and <see cref=""{ _quantity . Name } ""/>.</summary>
@@ -751,19 +751,6 @@ private void GenerateEqualityAndComparison()
751
751
return left.Value > right.GetValueAs(left.Unit);
752
752
}}
753
753
754
- /// <summary>Returns true if exactly equal.</summary>
755
- /// <remarks>Consider using <see cref=""Equals({ _quantity . Name } , { _valueType } , ComparisonType)""/> for safely comparing floating point values.</remarks>
756
- public static bool operator ==({ _quantity . Name } left, { _quantity . Name } right)
757
- {{
758
- return left.Equals(right);
759
- }}
760
- /// <summary>Returns true if not exactly equal.</summary>
761
- /// <remarks>Consider using <see cref=""Equals({ _quantity . Name } , { _valueType } , ComparisonType)""/> for safely comparing floating point values.</remarks>
762
- public static bool operator !=({ _quantity . Name } left, { _quantity . Name } right)
763
- {{
764
- return !(left == right);
765
- }}
766
-
767
754
/// <inheritdoc />
768
755
public int CompareTo(object obj)
769
756
{{
@@ -776,29 +763,7 @@ public int CompareTo(object obj)
776
763
/// <inheritdoc />
777
764
public int CompareTo({ _quantity . Name } other)
778
765
{{
779
- var asFirstUnit = other.GetValueAs(this.Unit);
780
- var asSecondUnit = GetValueAs(other.Unit);
781
- return (_value.CompareTo(asFirstUnit) - other.Value.CompareTo(asSecondUnit)) / 2;
782
- }}
783
-
784
- /// <inheritdoc />
785
- /// <remarks>Consider using <see cref=""Equals({ _quantity . Name } , { _valueType } , ComparisonType)""/> for safely comparing floating point values.</remarks>
786
- public override bool Equals(object obj)
787
- {{
788
- if (obj is null || !(obj is { _quantity . Name } obj{ _quantity . Name } ))
789
- return false;
790
- return Equals(obj{ _quantity . Name } );
791
- }}
792
-
793
- /// <inheritdoc />
794
- /// <remarks>Consider using <see cref=""Equals({ _quantity . Name } , { _valueType } , ComparisonType)""/> for safely comparing floating point values.</remarks>
795
- public bool Equals({ _quantity . Name } other)
796
- {{
797
- if (Value.IsDecimal)
798
- return other.Value.Equals(this.GetValueAs(other.Unit));
799
- if (other.Value.IsDecimal)
800
- return Value.Equals(other.GetValueAs(this.Unit));
801
- return this.Unit == other.Unit && this.Value.Equals(other.Value);
766
+ return _value.CompareTo(other.GetValueAs(this.Unit));
802
767
}}
803
768
804
769
/// <summary>
@@ -841,13 +806,13 @@ public bool Equals({_quantity.Name} other)
841
806
/// <param name=""tolerance"">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
842
807
/// <param name=""comparisonType"">The comparison type: either relative or absolute.</param>
843
808
/// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
844
- public bool Equals({ _quantity . Name } other, { _valueType } tolerance, ComparisonType comparisonType)
809
+ public bool Equals({ _quantity . Name } other, double tolerance, ComparisonType comparisonType)
845
810
{{
846
811
if (tolerance < 0)
847
812
throw new ArgumentOutOfRangeException(""tolerance"", ""Tolerance must be greater than or equal to 0."");
848
813
849
- { _valueType } thisValue = this.Value;
850
- { _valueType } otherValueInThisUnits = other.As(this.Unit);
814
+ double thisValue = (double) this.Value;
815
+ double otherValueInThisUnits = other.As(this.Unit);
851
816
852
817
return UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType);
853
818
}}
@@ -858,7 +823,7 @@ public bool Equals({_quantity.Name} other, {_valueType} tolerance, ComparisonTyp
858
823
/// <returns>A hash code for the current { _quantity . Name } .</returns>
859
824
public override int GetHashCode()
860
825
{{
861
- return Info.Name.GetHashCode();
826
+ return new {{ Info.Name, Value, Unit }} .GetHashCode();
862
827
}}
863
828
864
829
#endregion
@@ -874,30 +839,17 @@ private void GenerateConversionMethods()
874
839
/// Convert to the unit representation <paramref name=""unit"" />.
875
840
/// </summary>
876
841
/// <returns>Value converted to the specified unit.</returns>
877
- public { _valueType } As({ _unitEnumName } unit)
842
+ public double As({ _unitEnumName } unit)
878
843
{{
879
- if(Unit == unit)
880
- return Value;
881
-
882
- return GetValueAs(unit);
883
- }}
884
- " ) ;
885
-
886
- if ( _valueType == "decimal" )
887
- {
888
- Writer. WL ( $@ "
844
+ if (Unit == unit)
845
+ return Convert.ToDouble(Value);
889
846
890
- { _valueType } IQuantity<{ _unitEnumName } >.As({ _unitEnumName } unit)
891
- {{
892
- return ({ _valueType } )As(unit);
847
+ var converted = GetValueAs(unit);
848
+ return Convert.ToDouble(converted);
893
849
}}
894
- " ) ;
895
- }
896
-
897
- Writer. WL ( $@ "
898
850
899
851
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
900
- public { _valueType } As(UnitSystem unitSystem)
852
+ public double As(UnitSystem unitSystem)
901
853
{{
902
854
if (unitSystem is null)
903
855
throw new ArgumentNullException(nameof(unitSystem));
@@ -910,27 +862,14 @@ private void GenerateConversionMethods()
910
862
911
863
return As(firstUnitInfo.Value);
912
864
}}
913
- " ) ;
914
-
915
- if ( _valueType == "decimal" )
916
- {
917
- Writer. WL ( $@ "
918
- /// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
919
- { _valueType } IQuantity.As(UnitSystem unitSystem)
920
- {{
921
- return ({ _valueType } )As(unitSystem);
922
- }}
923
- " ) ;
924
- }
925
865
926
- Writer. WL ( $@ "
927
866
/// <inheritdoc />
928
- { _valueType } IQuantity.As(Enum unit)
867
+ double IQuantity.As(Enum unit)
929
868
{{
930
- if (!(unit is { _unitEnumName } typedUnit ))
869
+ if (!(unit is { _unitEnumName } unitAs { _unitEnumName } ))
931
870
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({ _unitEnumName } )}} is supported."", nameof(unit));
932
871
933
- return ( { _valueType } )As(typedUnit );
872
+ return As(unitAs { _unitEnumName } );
934
873
}}
935
874
936
875
/// <summary>
@@ -962,25 +901,25 @@ private void GenerateConversionMethods()
962
901
var converted = conversionFunction(this);
963
902
return ({ _quantity . Name } )converted;
964
903
}}
965
- else if (Enum.IsDefined(typeof( { _unitEnumName } ), unit) )
904
+ else if (Unit != BaseUnit )
966
905
{{
967
906
// Direct conversion to requested unit NOT found. Convert to BaseUnit, and then from BaseUnit to requested unit.
968
907
var inBaseUnits = ToUnit(BaseUnit);
969
908
return inBaseUnits.ToUnit(unit);
970
909
}}
971
910
else
972
911
{{
973
- throw new NotSupportedException ($""Can not convert {{Unit}} to {{unit}}."");
912
+ throw new NotImplementedException ($""Can not convert {{Unit}} to {{unit}}."");
974
913
}}
975
914
}}
976
915
977
916
/// <inheritdoc />
978
917
IQuantity IQuantity.ToUnit(Enum unit)
979
918
{{
980
- if (!(unit is { _unitEnumName } typedUnit ))
919
+ if (!(unit is { _unitEnumName } unitAs { _unitEnumName } ))
981
920
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({ _unitEnumName } )}} is supported."", nameof(unit));
982
921
983
- return ToUnit(typedUnit , DefaultConversionFunctions);
922
+ return ToUnit(unitAs { _unitEnumName } , DefaultConversionFunctions);
984
923
}}
985
924
986
925
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
0 commit comments