1
0
mirror of https://github.com/chylex/Query.git synced 2025-07-06 18:04:33 +02:00

Compare commits

...

2 Commits

Author SHA1 Message Date
2b9d5bba8f
Preserve rational number arithmetic for roots of perfect powers 2025-07-02 21:15:15 +02:00
8380aaa9f8
Reformat code 2025-07-02 21:01:32 +02:00
21 changed files with 356 additions and 342 deletions

View File

@ -36,14 +36,28 @@ public abstract record Number : IAdditionOperators<Number, Number, Number>,
public override Number Pow(Number exponent) { public override Number Pow(Number exponent) {
if (exponent is Rational { Value: {} rationalExponent }) { if (exponent is Rational { Value: {} rationalExponent }) {
Fraction fractionExponent = rationalExponent.GetImproperFraction(); Fraction fractionExponent = rationalExponent.GetImproperFraction();
if (fractionExponent.Denominator == 1 && fractionExponent.Numerator >= 0) {
if (fractionExponent.Numerator >= 0 && fractionExponent.Denominator == 1) {
try { try {
return new Rational(BigRational.Pow(Value, fractionExponent.Numerator)); return new Rational(BigRational.Pow(Value, fractionExponent.Numerator));
} catch (OverflowException) {} } catch (OverflowException) {}
} }
if (fractionExponent.Numerator == 1 && fractionExponent.Denominator > 1) {
Number result = PowAsDecimal(exponent);
BigRational assumedPerfectPowerRoot = new BigRational(decimal.Floor(result.AsDecimal));
BigRational assumedPerfectPower = BigRational.Pow(assumedPerfectPowerRoot, fractionExponent.Denominator);
return assumedPerfectPower == Value ? assumedPerfectPowerRoot : result;
}
} }
return new Decimal(AsDecimal).Pow(exponent); return PowAsDecimal(exponent);
Number PowAsDecimal(Number number) {
return new Decimal(AsDecimal).Pow(number);
}
} }
public override string ToString(IFormatProvider? formatProvider) { public override string ToString(IFormatProvider? formatProvider) {

View File

@ -109,7 +109,7 @@
JP8kJCT/JCQk/yQkJP8kJCT/JCQk/yQkJP8AAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA JP8kJCT/JCQk/yQkJP8kJCT/JCQk/yQkJP8AAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA
AEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA AEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA
AEE= AEE=
</value> </value>
</data> </data>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing.Common" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing.Common" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
@ -155,6 +155,6 @@
JP8kJCT/JCQk/yQkJP8kJCT/JCQk/yQkJP8AAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA JP8kJCT/JCQk/yQkJP8kJCT/JCQk/yQkJP8AAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA
AEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA AEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAAAEEAAABBAAAAQQAA
AEE= AEE=
</value> </value>
</data> </data>
</root> </root>