Find the ln ( X ) and log10X with the help of expansion | Function to calculate ln x using expansion ; terminating value of the loop can be increased to improve the precision ; Function to calculate log10 x ; Driver Code ; setprecision ( 3 ) is used to display the output up to 3 decimal places
from math import pow NEW_LINE def calculateLnx ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE num = ( n - 1 ) / ( n + 1 ) NEW_LINE for i in range ( 1 , 1001 , 1 ) : NEW_LINE INDENT mul = ( 2 * i ) - 1 NEW_LINE cal = pow ( num , mul ) NEW_LINE cal = cal / mul NEW_LINE sum = sum + cal NEW_LINE DEDENT sum = 2 * sum NEW_LINE return sum NEW_LINE DEDENT def calculateLogx ( lnx ) : NEW_LINE INDENT return ( lnx / 2.303 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE lnx = calculateLnx ( n ) NEW_LINE logx = calculateLogx ( lnx ) NEW_LINE print ( " ln " , " { 0 : . 3f } " . format ( n ) , " = " , " { 0 : . 3f } " . format ( lnx ) ) NEW_LINE print ( " log10" , " { 0 : . 3f } " . format ( n ) , " = " , " { 0 : . 3f } " . format ( logx ) ) NEW_LINE DEDENT