9

Trying out a problem of finding the first k digits of a num^num I wrote the same program in C++ and Python

C++

long double intpart,num,f_digit,k; cin>>num>>k; f_digit= pow(10.0,modf(num*log10(num),&intpart)+k-1); cout<<f_digit; 

Python

(a,b) = modf(num*log10(num)) f_digits = pow(10,b+k-1) print f_digits 

Input

19423474 9 

Output

C++ > 163074912 Python > 163074908 

I checked the results the C++ solution is the accurate one. Checked it at http://www.wolframalpha.com/input/?i=19423474^19423474

Any idea how can I get the same precision in Python ???

EDIT : I know about the external library packages to obtain this precision but any NATIVE solution ???

5
  • 1
    It's curious that they're different at all, since you would think that they're both using the same basic math library (math.h), the same underlying co-processors, and the same underlying IEEE 754 standard. But perhaps python rewrote its own numeric tower. Commented Oct 2, 2010 at 17:19
  • Wolfram alpha says that the last few digits are 2826110976, which is the result of neither your C++ or Python code. Commented Oct 2, 2010 at 17:31
  • 1
    @sharth: he's listing the first few digits, not the last few. Commented Oct 2, 2010 at 17:47
  • 1
    @Will Hartung: they're different because the C code uses long double, and Python uses a normal C double. Commented Oct 2, 2010 at 17:58
  • No one seems to have noticed, but your Python code was subtly wrong; a is the fractional component, b is the integer component, and the code only makes sense with the fractional component (a), but you used b in the subsequent pow computation (which gets an OverflowError thanks to using floats, and if fixed to use pure integer math, would take some insane amount of time/memory to produce a result, if it ever finished). Commented Jan 26, 2023 at 18:03

3 Answers 3

11

Decimal is a built in python class that handles floating points correctly (as base 10, not as IEEE 7somethingsomething standard). I don't know if it supports logarithms and all that though.

Edit: It does indeed support logarithms "and all that".

You can set the precision of it as well. Default is 28 places, but it can be as large as you want. Think of it as a BigInt for decimals.

Sign up to request clarification or add additional context in comments.

Comments

2

Python floats are doubles under the hood, as you discovered. You will have to resort to C code, or an external library, to get better floating-point precision.

The GMP library is a good one, and it has a python wrapper called 'GMPY', available on PyPI

1 Comment

Any of the coding competition don't allow any external API's and i prefer coding in Python for obvious reasons. So i wanted a possible native solution !!!
0

In general, I would do it this way. However, it doesn't seem to perform anywhere near fast enough for your example numbers.

num = 453 k = 9 result = num ** num print str(result)[:k] # Prints: '163111849' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.