Skip to content

Commit 7a2da03

Browse files
committed
codegen: Update to Value/Unit props and proper casting
* Store Value/Unit upon construction with From() methods or ctor() * Move conversion functions into As() and AsBaseUnit() methods * Add QuantityValueDecimal to avoid precision-loss going via double
1 parent 995bda4 commit 7a2da03

File tree

4 files changed

+128
-135
lines changed

4 files changed

+128
-135
lines changed

UnitsNet/QuantityValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ private QuantityValue(double val)
6060

6161
#region To double
6262

63-
public static explicit operator double(QuantityValue number) => number._value;
63+
public static explicit operator double(QuantityValue number) => Convert.ToDouble(number._value);
6464

6565
#endregion
6666
}
6767
}
68-
#endif
68+
#endif

UnitsNet/Scripts/GenerateUnits.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ function Set-ConversionFunctions
135135
foreach ($u in $quantity.Units) {
136136

137137
# Use decimal for internal calculations if base type is not double, such as for long or int.
138-
if ($quantity.BaseType -ne "double") {
139-
$u.FromUnitToBaseFunc = $u.FromUnitToBaseFunc -replace "m", "d"
138+
if ($quantity.BaseType -eq "decimal") {
139+
$u.FromUnitToBaseFunc = $u.FromUnitToBaseFunc -replace "d", "m"
140140
$u.FromBaseToUnitFunc = $u.FromBaseToUnitFunc -replace "d", "m"
141141
}
142142

@@ -274,8 +274,8 @@ $pad = 25
274274
$quantities = Get-ChildItem -Path $templatesDir -filter "*.json" `
275275
| %{(Get-Content $_.FullName -Encoding "UTF8" | Out-String)} `
276276
| ConvertFrom-Json `
277-
| Add-PrefixUnits `
278277
| Set-DefaultValues `
278+
| Add-PrefixUnits `
279279
| Set-ConversionFunctions `
280280
| Set-UnitsOrderedByName
281281

UnitsNet/Scripts/Include-GenerateLogarithmicCode.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,45 @@ function GenerateLogarithmicArithmeticOperators([string]$quantityName, [string]$
2828
#if !WINDOWS_UWP
2929
public static $quantityName operator -($quantityName right)
3030
{
31-
return new $quantityName(-right.$baseUnitFieldName);
31+
return new $quantityName(-right.Value, right.Unit);
3232
}
3333
3434
public static $quantityName operator +($quantityName left, $quantityName right)
3535
{
3636
// Logarithmic addition
3737
// Formula: $x*log10(10^(x/$x) + 10^(y/$x))
38-
return new $quantityName($x*Math.Log10(Math.Pow(10, left.$baseUnitFieldName/$x) + Math.Pow(10, right.$baseUnitFieldName/$x)));
38+
return new $quantityName($x*Math.Log10(Math.Pow(10, left.Value/$x) + Math.Pow(10, right.AsBaseNumericType(left.Unit)/$x)), left.Unit);
3939
}
4040
4141
public static $quantityName operator -($quantityName left, $quantityName right)
4242
{
4343
// Logarithmic subtraction
4444
// Formula: $x*log10(10^(x/$x) - 10^(y/$x))
45-
return new $quantityName($x*Math.Log10(Math.Pow(10, left.$baseUnitFieldName/$x) - Math.Pow(10, right.$baseUnitFieldName/$x)));
45+
return new $quantityName($x*Math.Log10(Math.Pow(10, left.Value/$x) - Math.Pow(10, right.AsBaseNumericType(left.Unit)/$x)), left.Unit);
4646
}
4747
4848
public static $quantityName operator *($baseType left, $quantityName right)
4949
{
5050
// Logarithmic multiplication = addition
51-
return new $quantityName(left + right.$baseUnitFieldName);
51+
return new $quantityName(left + right.Value, right.Unit);
5252
}
5353
5454
public static $quantityName operator *($quantityName left, double right)
5555
{
5656
// Logarithmic multiplication = addition
57-
return new $quantityName(left.$baseUnitFieldName + ($baseType)right);
57+
return new $quantityName(left.Value + ($baseType)right, left.Unit);
5858
}
5959
6060
public static $quantityName operator /($quantityName left, double right)
6161
{
6262
// Logarithmic division = subtraction
63-
return new $quantityName(left.$baseUnitFieldName - ($baseType)right);
63+
return new $quantityName(left.Value - ($baseType)right, left.Unit);
6464
}
6565
6666
public static double operator /($quantityName left, $quantityName right)
6767
{
6868
// Logarithmic division = subtraction
69-
return Convert.ToDouble(left.$baseUnitFieldName - right.$baseUnitFieldName);
69+
return Convert.ToDouble(left.Value - right.AsBaseNumericType(left.Unit));
7070
}
7171
#endif
7272

0 commit comments

Comments
 (0)