@@ -324,6 +324,10 @@ protected override Entity InnerSimplify(bool isExact)
324324 // A size that is not a number is compared as a size, never by the difference
325325 // of two expressions: card(ZZ) - card(QQ) is not zero, and the two are equal.
326326 : AngouriMath . Core . Sets . Cardinality . IsSize ( left ) || AngouriMath . Core . Sets . Cardinality . IsSize ( right ) ? AngouriMath . Core . Sets . Cardinality . Compare ( left , right , static sign => sign == 0 , isExact )
327+ // An exact power too large to build is not a constant, its value being left written, and is
328+ // told apart from a number or another such power where its sign, exponent or magnitude decides it.
329+ // https://github.com/asc-community/AngouriMath/issues/1448
330+ : OrderOfUnbuiltPowers ( left , right ) is { } order ? order == 0
327331 : left . IsConstant && right . IsConstant ? ( HasAnUnreadSize ( left ) || HasAnUnreadSize ( right ) ? null : ConstantsAreEqual ( left , right ) )
328332 : left is Set setLeft && right is Set setRight ? SetsAreEqual ( setLeft , setRight , isExact )
329333 : null ,
@@ -347,6 +351,80 @@ protected override Entity InnerSimplify(bool isExact)
347351 _ => null ,
348352 } ;
349353
354+ /// <summary>
355+ /// An exact power too large to build, which evaluation leaves written: an exact base that is not zero to a
356+ /// rational exponent. <see langword="null"/> for anything else.
357+ /// https://github.com/asc-community/AngouriMath/issues/1448
358+ /// </summary>
359+ private static ( Number . Rational Base , Number . Rational Exponent ) ? AsAnUnbuiltPower ( Entity expr )
360+ => expr is Powf ( Number . Rational { IsZero : false } @base , Number . Rational exponent ) && Powf . IsTooLargeToBuild ( @base , exponent )
361+ ? ( @base , exponent ) : null ;
362+
363+ /// <summary>
364+ /// The sign of a real power of an exact real base that is not zero, where its exponent decides it: positive
365+ /// for a positive base, and for a negative one by the parity of a whole exponent.
366+ /// </summary>
367+ private static int ? SignOfAnUnbuiltPower ( ( Number . Rational Base , Number . Rational Exponent ) power )
368+ => power . Base . ERational . Sign > 0 ? 1
369+ : power . Exponent is Number . Integer { EInteger : var whole } ? ( whole . IsEven ? 1 : - 1 )
370+ : null ;
371+
372+ /// <summary>
373+ /// The sign of <c>left - right</c> where either side is an exact power too large to build and the other is a
374+ /// real number or another such power, decided without building either: two powers of one positive base by
375+ /// their exponents, exactly, and otherwise by the signs and then by the logarithms of the magnitudes, where
376+ /// those differ by more than the error of computing them. <see langword="null"/> where nothing decides it,
377+ /// which leaves the comparison written; in particular, two magnitudes that may be equal are never told
378+ /// apart. As the decimals, these powers were <c>+oo</c> and <c>0</c>, and <c>2^(10^9) > 2^(10^9 - 1)</c>
379+ /// was False. https://github.com/asc-community/AngouriMath/issues/1448
380+ /// </summary>
381+ private static int ? OrderOfUnbuiltPowers ( Entity left , Entity right )
382+ {
383+ var ( powerLeft , powerRight ) = ( AsAnUnbuiltPower ( left ) , AsAnUnbuiltPower ( right ) ) ;
384+ if ( powerLeft is null && powerRight is null )
385+ return null ;
386+ if ( powerLeft is { } l && powerRight is { } r && l . Base . ERational . Equals ( r . Base . ERational ) && l . Base . ERational . Sign > 0 )
387+ {
388+ var byExponent = l . Exponent . ERational . CompareTo ( r . Exponent . ERational ) ;
389+ return l . Base . ERational . CompareTo ( ERational . One ) > 0 ? byExponent : - byExponent ;
390+ }
391+ if ( SignAndLogarithm ( left , powerLeft ) is not var ( signLeft , logLeft ) || SignAndLogarithm ( right , powerRight ) is not var ( signRight , logRight ) )
392+ return null ;
393+ // A power is not zero, so a side of the other sign, or zero, is decided by the signs alone.
394+ if ( signLeft != signRight || logLeft is null || logRight is null )
395+ return signLeft . CompareTo ( signRight ) ;
396+ // Each logarithm is within a relative 1e-99 of its value: a decimal side is rounded to a hundred digits,
397+ // and the rest is computed to OrderContext's. So a difference past 1e-90 of their size is not rounding.
398+ var difference = logLeft . Subtract ( logRight , OrderContext ) ;
399+ var bound = logLeft . Abs ( ) . Add ( logRight . Abs ( ) ) . Add ( EDecimal . One ) . Multiply ( OrderTolerance ) ;
400+ if ( difference . Abs ( ) . CompareTo ( bound ) <= 0 )
401+ return null ;
402+ return signLeft * difference . Sign ;
403+ }
404+
405+ /// <summary>
406+ /// The sign of an exact power too large to build, or of a finite real number, and the natural logarithm of
407+ /// its magnitude, <see langword="null"/> for zero. <see langword="null"/> for anything else, and for a power
408+ /// whose sign its exponent does not settle.
409+ /// </summary>
410+ private static ( int Sign , EDecimal ? Logarithm ) ? SignAndLogarithm ( Entity side , ( Number . Rational Base , Number . Rational Exponent ) ? power )
411+ {
412+ if ( power is { } p )
413+ return SignOfAnUnbuiltPower ( p ) is { } sign
414+ ? ( sign , p . Exponent . ERational . ToEDecimal ( OrderContext )
415+ . Multiply ( p . Base . ERational . Abs ( ) . ToEDecimal ( OrderContext ) . Log ( OrderContext ) , OrderContext ) )
416+ : null ;
417+ if ( side is Number number && AsAReal ( number ) is { EDecimal : { IsFinite : true } real } )
418+ return real . IsZero ? ( 0 , null ) : ( real . Sign , real . Abs ( ) . Log ( OrderContext ) ) ;
419+ return null ;
420+ }
421+
422+ /// <summary>The working precision of <see cref="SignAndLogarithm"/>: twenty digits past the decimals'.</summary>
423+ [ Core . ConstantField ] private static readonly EContext OrderContext = EContext . ForPrecision ( 120 ) ;
424+
425+ /// <summary>The relative difference of two logarithms below which <see cref="OrderOfUnbuiltPowers"/> leaves them undecided.</summary>
426+ [ Core . ConstantField ] private static readonly EDecimal OrderTolerance = EDecimal . FromString ( "1e-90" ) ;
427+
350428 partial record Greaterf
351429 {
352430 // Inequality comparisons are only defined for real numbers.
@@ -361,6 +439,7 @@ protected override Entity InnerSimplify(bool isExact)
361439 ( Number numLeft , Number numRight ) when AsAReal ( numLeft ) is { } reLeft && AsAReal ( numRight ) is { } reRight => reLeft > reRight ,
362440 ( Number numLeft , Number numRight ) => MathS . NaN ,
363441 _ when AngouriMath . Core . Sets . Cardinality . IsSize ( a ) || AngouriMath . Core . Sets . Cardinality . IsSize ( b ) => AngouriMath . Core . Sets . Cardinality . Compare ( a , b , static sign => sign > 0 , isExact ) ,
442+ _ when OrderOfUnbuiltPowers ( a , b ) is { } sign => sign > 0 ,
364443 _ => null
365444 } ,
366445 ( @this , a , b ) => ( ( Greaterf ) @this ) . New ( a , b ) , isExact ) ;
@@ -381,6 +460,7 @@ protected override Entity InnerSimplify(bool isExact)
381460 ( Number numLeft , Number numRight ) when AsAReal ( numLeft ) is { } reLeft && AsAReal ( numRight ) is { } reRight => reLeft >= reRight ,
382461 ( Number numLeft , Number numRight ) => MathS . NaN ,
383462 _ when AngouriMath . Core . Sets . Cardinality . IsSize ( a ) || AngouriMath . Core . Sets . Cardinality . IsSize ( b ) => AngouriMath . Core . Sets . Cardinality . Compare ( a , b , static sign => sign >= 0 , isExact ) ,
463+ _ when OrderOfUnbuiltPowers ( a , b ) is { } sign => sign >= 0 ,
384464 _ => null
385465 } ,
386466 ( @this , a , b ) => ( ( GreaterOrEqualf ) @this ) . New ( a , b ) , isExact ) ;
@@ -401,6 +481,7 @@ protected override Entity InnerSimplify(bool isExact)
401481 ( Number numLeft , Number numRight ) when AsAReal ( numLeft ) is { } reLeft && AsAReal ( numRight ) is { } reRight => reLeft < reRight ,
402482 ( Number numLeft , Number numRight ) => MathS . NaN ,
403483 _ when AngouriMath . Core . Sets . Cardinality . IsSize ( a ) || AngouriMath . Core . Sets . Cardinality . IsSize ( b ) => AngouriMath . Core . Sets . Cardinality . Compare ( a , b , static sign => sign < 0 , isExact ) ,
484+ _ when OrderOfUnbuiltPowers ( a , b ) is { } sign => sign < 0 ,
404485 _ => null
405486 } ,
406487 ( @this , a , b ) => ( ( Lessf ) @this ) . New ( a , b ) , isExact ) ;
@@ -420,6 +501,7 @@ protected override Entity InnerSimplify(bool isExact)
420501 ( Number numLeft , Number numRight ) when AsAReal ( numLeft ) is { } reLeft && AsAReal ( numRight ) is { } reRight => reLeft <= reRight ,
421502 ( Number numLeft , Number numRight ) => MathS . NaN ,
422503 _ when AngouriMath . Core . Sets . Cardinality . IsSize ( a ) || AngouriMath . Core . Sets . Cardinality . IsSize ( b ) => AngouriMath . Core . Sets . Cardinality . Compare ( a , b , static sign => sign <= 0 , isExact ) ,
504+ _ when OrderOfUnbuiltPowers ( a , b ) is { } sign => sign <= 0 ,
423505 _ => null
424506 } ,
425507 ( @this , a , b ) => ( ( LessOrEqualf ) @this ) . New ( a , b ) , isExact ) ;
0 commit comments