@@ -567,14 +567,12 @@ def isExpReprEqualToStandardRepr(dBase, dExp10, originalD):
567567
568568 # returns log10 representation of a number or the empty string if the input is not outside the defined log10 representation thresholds.
569569 @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 ):
574572 # 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 ()
576574 d = Decimal (str (digits [0 ]))
577- equalOrApprox = "=" if _BirthdayProblemNumberFormatter .isExpReprEqualToStandardRepr (d , exp , inputD ) else "≈"
575+ equalOrApprox = "=" if _BirthdayProblemNumberFormatter .isExpReprEqualToStandardRepr (d , exp , argD ) else "≈"
578576 return equalOrApprox + (str (d ) + "*" if _DecimalFns .isNotOne (d ) else "" ) + "10^" + str (exp )
579577 else :
580578 return "" # no base 10 representation needed
@@ -634,6 +632,7 @@ def methodToShortDescription(method):
634632 _BirthdayProblemSolver .CalcPrecision .EXACT : "Exact method" ,
635633 _BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX : "Taylor approximation" ,
636634 _BirthdayProblemSolver .CalcPrecision .STIRLING_APPROX : "Exact method with Stirling's approximation" ,
635+ _BirthdayProblemSolver .CalcPrecision .TRIVIAL : "Trivial method"
637636 }
638637 return texts .get (method , "Unknown method" )
639638
@@ -958,12 +957,13 @@ def solveText(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
958957 (n , methodUsed ) = _BirthdayProblemSolverChecked .birthdayProblemInv (d , dLog , p , isBinary )
959958 except BaseException as e :
960959 methodText = _BirthdayProblemTextFormatter .parenthesize (_BirthdayProblemTextFormatter .methodToShortDescription (_BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX ))
960+ errorText = "N/A (Calculation failed: " + str (e ).lower () + methodText + ")"
961961 if isinstance (e , KeyboardInterrupt ):
962962 outputter (_BirthdayProblemTextFormatter .indented ("N/A (Interrupted by user" + methodText + ")" ))
963963 elif isinstance (e , SolverException ):
964- outputter (_BirthdayProblemTextFormatter .indented ("N/A (Calculation failed: " + str ( e ) + methodText + ")" ))
964+ outputter (_BirthdayProblemTextFormatter .indented (errorText ))
965965 else :
966- outputter (_BirthdayProblemTextFormatter .indented ("N/A (Calculation failed: " + str ( e ) + methodText + ")" ))
966+ outputter (_BirthdayProblemTextFormatter .indented (errorText ))
967967 else :
968968 outputter (_BirthdayProblemTextFormatter .indented (_BirthdayProblemTextFormatter .resultTextBirthdayProblemInv (n , isBinary , methodUsed , prec )))
969969 else :
@@ -978,12 +978,13 @@ def solveText(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
978978 pPercent = _DecimalFns .toPercent (p )
979979 except BaseException as e :
980980 methodText = _BirthdayProblemTextFormatter .parenthesize (_BirthdayProblemTextFormatter .methodToShortDescription (method ))
981+ errorText = " (Calculation failed: " + str (e ).lower () + methodText + ")"
981982 if isinstance (e , KeyboardInterrupt ):
982983 results += [("N/A" , "" , " (Interrupted by user" + methodText + ")" )]
983984 elif isinstance (e , SolverException ):
984- results += [("N/A" , "" , " (Calculation failed: " + str ( e ) + methodText + ")" )]
985+ results += [("N/A" , "" , errorText )]
985986 else :
986- results += [("N/A" , "" , " (Calculation failed: " + str ( e ) + methodText + ")" )]
987+ results += [("N/A" , "" , errorText )]
987988 else :
988989 results += [_BirthdayProblemTextFormatter .resultTextBirthdayProblem (p , pPercent , methodUsed , prec )]
989990 # 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
10071008 (n , methodUsed ) = _BirthdayProblemSolverChecked .birthdayProblemInv (d , dLog , p , isBinary )
10081009 except BaseException as e :
10091010 methodKey = _BirthdayProblemTextFormatter .methodToText (_BirthdayProblemSolver .CalcPrecision .TAYLOR_APPROX ).lower ()
1010- res [ 'results' ][ methodKey ] = {}
1011+ errorMessage = str ( e ). lower ()
10111012 if isinstance (e , KeyboardInterrupt ):
1012- res ['results' ][methodKey ][ 'error' ] = 'interrupted'
1013+ res ['results' ][methodKey ] = { 'error' : 'interrupted' }
10131014 elif isinstance (e , SolverException ):
1014- res ['results' ][methodKey ][ 'error' ] = str ( e )
1015+ res ['results' ][methodKey ] = { 'error' : errorMessage }
10151016 else :
1016- res ['results' ][methodKey ][ 'error' ] = str ( e )
1017+ res ['results' ][methodKey ] = { 'error' : errorMessage }
10171018 else :
10181019 methodKey = _BirthdayProblemTextFormatter .methodToText (methodUsed ).lower ()
1019- res ['results' ][methodKey ] = {}
10201020 n = _BirthdayProblemTextFormatter .resultTextBirthdayProblemInvNumbers (n , isBinary , prec )
1021- res ['results' ][methodKey ][ 'result' ] = n
1021+ res ['results' ][methodKey ] = { 'result' : n }
10221022 else :
10231023 dText , nText = _BirthdayProblemTextFormatter .headerTextBirthdayProblemNumbers (dLog if isBinary else d , nLog if isBinary else n , isBinary , prec )
10241024 res ['d' ] = dText
@@ -1032,18 +1032,17 @@ def solveJson(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
10321032 pPercent = _DecimalFns .toPercent (p )
10331033 except BaseException as e :
10341034 methodKey = _BirthdayProblemTextFormatter .methodToText (method ).lower ()
1035- res [ 'results' ][ methodKey ] = {}
1035+ errorMessage = str ( e ). lower ()
10361036 if isinstance (e , KeyboardInterrupt ):
1037- res ['results' ][methodKey ][ 'error' ] = 'interrupted'
1037+ res ['results' ][methodKey ] = { 'error' : 'interrupted' }
10381038 elif isinstance (e , SolverException ):
1039- res ['results' ][methodKey ][ 'error' ] = str ( e )
1039+ res ['results' ][methodKey ] = { 'error' : errorMessage }
10401040 else :
1041- res ['results' ][methodKey ][ 'error' ] = str ( e )
1041+ res ['results' ][methodKey ] = { 'error' : errorMessage }
10421042 else :
10431043 methodKey = _BirthdayProblemTextFormatter .methodToText (methodUsed ).lower ()
1044- res ['results' ][methodKey ] = {}
10451044 p = "" .join (_BirthdayProblemTextFormatter .resultTextBirthdayProblemNumbers (p , pPercent , prec ))
1046- res ['results' ][methodKey ][ 'result' ] = p
1045+ res ['results' ][methodKey ] = { 'result' : p }
10471046 res = json .dumps (res )
10481047 if (isMainProgram ):
10491048 print (res )
0 commit comments