0

i'm trying to display the same original value (inverse of log )after taking the log based 10 as shown in the example, but i'm not able to display the original value, i have follow the answer there but its still not display it, i know its large value . is there away to display it? i'm using python 3.6

import math
a=2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960
x=math.log10(a)
print(x) # result there is 93.33696640660276
y=10**x
print(y) #  result there is 2.1725331233299482e+93
print(y*10**93) # result there is 2.1725331233299483e+186
hani
  • 19
  • 6
  • in python you don't have to use `;` at the end of line. But you have to use `#` instead of `//` for comments. – furas Sep 30 '19 at 07:10
  • i just use the // for show the answer there, the code is work good sir, its just the problem i need to display the original value of a after taking the inverse of log – hani Sep 30 '19 at 07:14
  • In Python, the result of a logarithm and exponentiation is always a float, even if your original value was an integer. There's no real way around this when using standard types. If you need to do exact math, you can try [sympy](https://www.sympy.org/en/index.html). – L3viathan Sep 30 '19 at 07:16
  • 1
    try print(int(y)) and print(int(y*10**93)) – TVK Sep 30 '19 at 07:20

4 Answers4

2

When using sympy, you can use arbitrary precision math:

>>> import sympy
>>> a = 2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960
>>> x = sympy.log(a, 10)
>>> x
1 + log(217253312332991762017286188910986740604997538732016932260875184446040622223162641636598480896)/log(10)
>>> y = 10**x
>>> y
10**(1 + log(217253312332991762017286188910986740604997538732016932260875184446040622223162641636598480896)/log(10))
>>> y*10**93
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10**(1 + log(217253312332991762017286188910986740604997538732016932260875184446040622223162641636598480896)/log(10))
>>> result = y*10**93
>>> result
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10**(1 + log(217253312332991762017286188910986740604997538732016932260875184446040622223162641636598480896)/log(10))
>>> result.simplify()
2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
L3viathan
  • 26,748
  • 2
  • 58
  • 81
1

You can use string formating - '{:f}'

print('{:f}'.format(y))
print('{:f}'.format(y*10**93))

Result:

2172533123329948244974667712184302503862484572004323002958790801469409104613465879037786193920.000000
2172533123329948250187989505303380345960860577775669342927459052809311382170807040229342819817270425458795653742290299639999303458637786346385752271584468621244918497826695717627392688128.000000

To remove zeros after dot - '{:.0f}'

print('{:.0f}'.format(y))
print('{:.0f}'.format(y*10**93))

Result:

2172533123329948244974667712184302503862484572004323002958790801469409104613465879037786193920
2172533123329948250187989505303380345960860577775669342927459052809311382170807040229342819817270425458795653742290299639999303458637786346385752271584468621244918497826695717627392688128

To display with e - '{:e}'

print('{:e}'.format(x))

Result:

9.333697e+01

More about string formating on pyformat.info


Code:

import math

a = 2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960
x = math.log10(a)
y = 10**x 

print('{:e}'.format(x))
print('{:.0f}'.format(y))
print('{:.0f}'.format(y*10**93))
furas
  • 134,197
  • 12
  • 106
  • 148
  • prefect sir, thank you for explain everything, but the value is not same as i compare with the variable a , you can take look into the results . thank you again – hani Sep 30 '19 at 07:22
  • because `float` is not ideal - it can't keep all real values and there is always problem like this `0.1 + 0.2 == 0.3` which gives `False` but we expect `True`. To get better result you should use method mentioned in other answers. – furas Sep 30 '19 at 07:28
0

As furas says, if you want to avoid the "engineering notation" when printing the result specify a precision in the format string.

If, however, you want a higher precision for the calculation you can use the decimal library:

from decimal import Decimal, getcontext

a = Decimal(2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960)

# Increase the precision
getcontext().prec = 100

x = a.log10()
y = 10 ** x
print(y)

Joe Halliwell
  • 1,155
  • 6
  • 21
  • thank you sir, your code is help me and work great after use print('{:.0f}'.format(y)) give me prefect answer ,from @ furas – hani Sep 30 '19 at 07:37
  • Thanks! @L3viathan's style of answer may also be useful, if you need `y` to be exactly equal to `a` for some reason. – Joe Halliwell Sep 30 '19 at 07:40
0

thank you everybody for perfect answers. the final code as shown below

from decimal import Decimal, getcontext

a = Decimal(2172533123329917620172861889109867406049975387320169322608751844460406222231626416365984808960)

# Increase the precision
getcontext().prec = 100

x = a.log10()
y = 10 ** x
print(y)
print('{:.0f}'.format(y))
hani
  • 19
  • 6
  • stackoverflow is not normal forum and we rather don't use place for answers/solutions to show final code but we append it in question - sometimes with text "EDIT:" – furas Sep 30 '19 at 07:48