@@ -567,14 +567,12 @@ def isExpReprEqualToStandardRepr(dBase, dExp10, originalD):
567
567
568
568
# returns log10 representation of a number or the empty string if the input is not outside the defined log10 representation thresholds.
569
569
@staticmethod
570
- def toLog10ReprOrNone (d ):
571
- inputD = d
572
- exp = 0 # powers of 10 filtered out of d
573
- if (_DecimalFns .isGreaterThan (_BirthdayProblemNumberFormatter .LOG10_LOWER_THRESHOLD , d ) or _DecimalFns .isLessThan (_BirthdayProblemNumberFormatter .LOG10_UPPER_THRESHOLD , d )) and _DecimalFns .isGreaterThanZero (d ):
570
+ def toLog10ReprOrNone (argD ):
571
+ if (_DecimalFns .isGreaterThan (_BirthdayProblemNumberFormatter .LOG10_LOWER_THRESHOLD , argD ) or _DecimalFns .isLessThan (_BirthdayProblemNumberFormatter .LOG10_UPPER_THRESHOLD , argD )) and _DecimalFns .isGreaterThanZero (argD ):
574
572
# d is smaller than the lower log 10 repr threshold or larger than the upper log 10 repr threshold, and not 0, so a complementary log 10 representation is called for
575
- (sign , digits , exp ) = Context (prec = 1 , rounding = ROUND_HALF_UP ).create_decimal (d ).as_tuple ()
573
+ (sign , digits , exp ) = Context (prec = 1 , rounding = ROUND_HALF_UP ).create_decimal (argD ).as_tuple ()
576
574
d = Decimal (str (digits [0 ]))
577
- equalOrApprox = "=" if _BirthdayProblemNumberFormatter .isExpReprEqualToStandardRepr (d , exp , inputD ) else "≈"
575
+ equalOrApprox = "=" if _BirthdayProblemNumberFormatter .isExpReprEqualToStandardRepr (d , exp , argD ) else "≈"
578
576
return equalOrApprox + (str (d ) + "*" if _DecimalFns .isNotOne (d ) else "" ) + "10^" + str (exp )
579
577
else :
580
578
return "" # no base 10 representation needed
@@ -634,6 +632,7 @@ def methodToShortDescription(method):
634
632
_BirthdayProblemSolver .CalcPrecision .EXACT : "Exact method" ,
635
633
_BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX : "Taylor approximation" ,
636
634
_BirthdayProblemSolver .CalcPrecision .STIRLING_APPROX : "Exact method with Stirling's approximation" ,
635
+ _BirthdayProblemSolver .CalcPrecision .TRIVIAL : "Trivial method"
637
636
}
638
637
return texts .get (method , "Unknown method" )
639
638
@@ -958,12 +957,13 @@ def solveText(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
958
957
(n , methodUsed ) = _BirthdayProblemSolverChecked .birthdayProblemInv (d , dLog , p , isBinary )
959
958
except BaseException as e :
960
959
methodText = _BirthdayProblemTextFormatter .parenthesize (_BirthdayProblemTextFormatter .methodToShortDescription (_BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX ))
960
+ errorText = "N/A (Calculation failed: " + str (e ).lower () + methodText + ")"
961
961
if isinstance (e , KeyboardInterrupt ):
962
962
outputter (_BirthdayProblemTextFormatter .indented ("N/A (Interrupted by user" + methodText + ")" ))
963
963
elif isinstance (e , SolverException ):
964
- outputter (_BirthdayProblemTextFormatter .indented ("N/A (Calculation failed: " + str ( e ) + methodText + ")" ))
964
+ outputter (_BirthdayProblemTextFormatter .indented (errorText ))
965
965
else :
966
- outputter (_BirthdayProblemTextFormatter .indented ("N/A (Calculation failed: " + str ( e ) + methodText + ")" ))
966
+ outputter (_BirthdayProblemTextFormatter .indented (errorText ))
967
967
else :
968
968
outputter (_BirthdayProblemTextFormatter .indented (_BirthdayProblemTextFormatter .resultTextBirthdayProblemInv (n , isBinary , methodUsed , prec )))
969
969
else :
@@ -978,12 +978,13 @@ def solveText(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
978
978
pPercent = _DecimalFns .toPercent (p )
979
979
except BaseException as e :
980
980
methodText = _BirthdayProblemTextFormatter .parenthesize (_BirthdayProblemTextFormatter .methodToShortDescription (method ))
981
+ errorText = " (Calculation failed: " + str (e ).lower () + methodText + ")"
981
982
if isinstance (e , KeyboardInterrupt ):
982
983
results += [("N/A" , "" , " (Interrupted by user" + methodText + ")" )]
983
984
elif isinstance (e , SolverException ):
984
- results += [("N/A" , "" , " (Calculation failed: " + str ( e ) + methodText + ")" )]
985
+ results += [("N/A" , "" , errorText )]
985
986
else :
986
- results += [("N/A" , "" , " (Calculation failed: " + str ( e ) + methodText + ")" )]
987
+ results += [("N/A" , "" , errorText )]
987
988
else :
988
989
results += [_BirthdayProblemTextFormatter .resultTextBirthdayProblem (p , pPercent , methodUsed , prec )]
989
990
# map every value for results and log10 results to the length of the string (=> an array of tuples), then spred it with * so that we add tuples as vararg input to zip which will then create two
@@ -1007,18 +1008,17 @@ def solveJson(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
1007
1008
(n , methodUsed ) = _BirthdayProblemSolverChecked .birthdayProblemInv (d , dLog , p , isBinary )
1008
1009
except BaseException as e :
1009
1010
methodKey = _BirthdayProblemTextFormatter .methodToText (_BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX ).lower ()
1010
- res [ 'results' ][ methodKey ] = {}
1011
+ errorMessage = str ( e ). lower ()
1011
1012
if isinstance (e , KeyboardInterrupt ):
1012
- res ['results' ][methodKey ][ 'error' ] = 'interrupted'
1013
+ res ['results' ][methodKey ] = { 'error' : 'interrupted' }
1013
1014
elif isinstance (e , SolverException ):
1014
- res ['results' ][methodKey ][ 'error' ] = str ( e )
1015
+ res ['results' ][methodKey ] = { 'error' : errorMessage }
1015
1016
else :
1016
- res ['results' ][methodKey ][ 'error' ] = str ( e )
1017
+ res ['results' ][methodKey ] = { 'error' : errorMessage }
1017
1018
else :
1018
1019
methodKey = _BirthdayProblemTextFormatter .methodToText (methodUsed ).lower ()
1019
- res ['results' ][methodKey ] = {}
1020
1020
n = _BirthdayProblemTextFormatter .resultTextBirthdayProblemInvNumbers (n , isBinary , prec )
1021
- res ['results' ][methodKey ][ 'result' ] = n
1021
+ res ['results' ][methodKey ] = { 'result' : n }
1022
1022
else :
1023
1023
dText , nText = _BirthdayProblemTextFormatter .headerTextBirthdayProblemNumbers (dLog if isBinary else d , nLog if isBinary else n , isBinary , prec )
1024
1024
res ['d' ] = dText
@@ -1032,18 +1032,17 @@ def solveJson(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
1032
1032
pPercent = _DecimalFns .toPercent (p )
1033
1033
except BaseException as e :
1034
1034
methodKey = _BirthdayProblemTextFormatter .methodToText (method ).lower ()
1035
- res [ 'results' ][ methodKey ] = {}
1035
+ errorMessage = str ( e ). lower ()
1036
1036
if isinstance (e , KeyboardInterrupt ):
1037
- res ['results' ][methodKey ][ 'error' ] = 'interrupted'
1037
+ res ['results' ][methodKey ] = { 'error' : 'interrupted' }
1038
1038
elif isinstance (e , SolverException ):
1039
- res ['results' ][methodKey ][ 'error' ] = str ( e )
1039
+ res ['results' ][methodKey ] = { 'error' : errorMessage }
1040
1040
else :
1041
- res ['results' ][methodKey ][ 'error' ] = str ( e )
1041
+ res ['results' ][methodKey ] = { 'error' : errorMessage }
1042
1042
else :
1043
1043
methodKey = _BirthdayProblemTextFormatter .methodToText (methodUsed ).lower ()
1044
- res ['results' ][methodKey ] = {}
1045
1044
p = "" .join (_BirthdayProblemTextFormatter .resultTextBirthdayProblemNumbers (p , pPercent , prec ))
1046
- res ['results' ][methodKey ][ 'result' ] = p
1045
+ res ['results' ][methodKey ] = { 'result' : p }
1047
1046
res = json .dumps (res )
1048
1047
if (isMainProgram ):
1049
1048
print (res )
0 commit comments