Skip to content

Commit de1cb9b

Browse files
committed
🐛Pow fix
1 parent 89101a3 commit de1cb9b

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
global using TedToolkit.Quantities;
2-
32
using TedToolkit.QuantExtensions;
43
using UnitsNet;
54
using Length = UnitsNet.Length;
@@ -8,4 +7,6 @@
87
[assembly: NumberExtension<int, Angle>]
98
[assembly: NumberExtension<double, Angle>]
109

11-
[assembly:Quantities<double>(QuantitySystems.ALL, "AbsorbedDose", "Dimensionless", "DimensionlessRatio")]
10+
[assembly:
11+
Quantities<double>(QuantitySystems.ALL, "Area", "AbsorbedDose", "Dimensionless", "DimensionlessRatio",
12+
Length = LengthUnit.Millimetre)]

Tests/TedToolkit.Console/Program.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
using UnitsNet;
1515
using UnitsNet.Units;
1616

17-
var pi = ERational.FromEDecimal(EDecimal.FromString("3.1415926535897932384626433832795028841971693993751058209749445923"));
18-
var result = pi * ERational.FromInt32(2);
19-
var ctx = EContext.ForPrecision(50) // 精度50位
20-
.WithExponentRange(-1000, 1000) // 指数在 [-1000, 1000]
21-
.WithRounding(ERounding.HalfEven);
22-
Console.WriteLine(result.ToEDecimal(ctx));
17+
// var pi = ERational.FromEDecimal(EDecimal.FromString("3.1415926535897932384626433832795028841971693993751058209749445923"));
18+
// var result = pi * ERational.FromInt32(2);
19+
// var ctx = EContext.ForPrecision(50) // 精度50位
20+
// .WithExponentRange(-1000, 1000) // 指数在 [-1000, 1000]
21+
// .WithRounding(ERounding.HalfEven);
22+
// Console.WriteLine(result.ToEDecimal(ctx));
23+
24+
var rational = ERational.Create(EInteger.FromInt16(1), EInteger.FromInt16(10));
25+
Console.WriteLine(rational);
26+
var powered = Pow(rational, 4);
27+
Console.WriteLine(powered);
2328

2429
return;
2530
var length = (TedToolkit.Quantities.Length)6.0;
@@ -38,7 +43,18 @@
3843
var b = ratio.DecibelMicrovolts;
3944
return;
4045

46+
static ERational Pow(ERational rational, int exponent)
47+
{
48+
if (exponent == 0) return ERational.One;
49+
var one = rational;
50+
for (var i = 1; i < Math.Abs(exponent); i++)
51+
{
52+
rational *= one;
53+
}
4154

55+
if (exponent < 0) rational = ERational.One / rational;
56+
return rational;
57+
}
4258
// unsafe
4359
// {
4460
// double a = 15;

src/analyzers/TedToolkit.Quantities.Analyzer/UnitSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Unit GetUnit(string key)
1717

1818
var data = collection;
1919
var allUnits = data.Units.Values.ToArray();
20-
var quantityUnits =quantity.Units
20+
var quantityUnits = quantity.Units
2121
.Select(u => data.Units[u]);
2222
if (unitDictionary.TryGetValue(key, out var unitKey))
2323
{

src/libraries/TedToolkit.Quantities.Data/Conversion.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public readonly record struct Conversion(ERational Multiplier, ERational Offset)
4242
private static ERational Pow(ERational rational, int exponent)
4343
{
4444
if (exponent == 0) return ERational.One;
45-
for (var i = 0; i < Math.Abs(exponent); i++)
45+
var one = rational;
46+
for (var i = 1; i < Math.Abs(exponent); i++)
4647
{
47-
rational *= rational;
48+
rational *= one;
4849
}
4950

5051
if (exponent < 0) rational = ERational.One / rational;

0 commit comments

Comments
 (0)