I used log to obtain the exponent base 2 to a very large number and I want to be able to retrieve the number back by reversing the function (2**x). However math.pow(2,x) is truncating the answer drastically. Note that x is a very large power in the range of 100000.00000001. The original number has to be retrieved in full.
Asked
Active
Viewed 490 times
0
-
Can you make this a running example with the loss of accuracy? – tdelaney Jan 06 '21 at 17:24
-
1`100000.00000001` is just an estimate for display. Printing with full precision `f"{100000.00000001:.53g}"` is `100000.000000009997165761888027191162109375`. `math.pow(2, 100000.00000001)` gives "OverflowError: math range error" on my machine. – tdelaney Jan 06 '21 at 17:40
-
1by, in the range of, i meant along those lines and not exactly that number. – Justin Farrugia Jan 06 '21 at 17:45
-
GMP (see [gmpy2](https://pypi.org/project/gmpy2/)) and [mpmath](https://mpmath.org/) are a couple of options for high precision math. Python uses the native double type for floats and it looks like your operations overflow those. These other libraries can help. – tdelaney Jan 06 '21 at 17:48
1 Answers
0
math.pow
uses floating point arithmetic.
Raising 2
to a power is equivalent to left-shifting 1
that many bits, so you can use the <<
operator.
x = 1000
result = 1 << x
print(result)

Barmar
- 741,623
- 53
- 500
- 612
-
In that case you have to use floating point, and you're stuck with its limitations. – Barmar Jan 06 '21 at 17:24
-
-
no i haven't yet. i refuse to accept that it's not possible. I mean I already had the large number loaded and I computed log of it to get the power, I just need to reverse the operation. It's like you're telling me 4 + 1 = 5 but 5 - 1 is not possible. I deleted the comment because I added some extra numbers in the integer part of my example. can you give me an example using decimal lib pls? – Justin Farrugia Jan 06 '21 at 17:31
-
1Here's an analogy: Suppose you're using 8-bit signed integers. 127 + 1 doesn't exist. The same thing happens with floating point: it has limited precision, so some numbers don't exist. – Barmar Jan 06 '21 at 17:34
-
ok I understand this but I know for a fact that my power is computable because I got it from the answer I want itself by doing log of it. Isn't there any way to compute it without the out of range exception by some other method that spits out the answer little by little instead? – Justin Farrugia Jan 06 '21 at 17:43
-
You almost certainly lost precision when you calculated the log in the first place. – Barmar Jan 06 '21 at 17:52
-
1Binary floating point can't even represent `1/10` exactly. It's like representing `1/3` or `1/7` in decimal, it's a repeating fraction. – Barmar Jan 06 '21 at 17:54
-
@JustinFarrugia The math analogy isn't really correct. A better analogy would be to scale or rotate an image. Scaling an image down to 1 pixel and then back up to 1024x1024 pixels will give you a completely blank image. As soon you do some operation that doesn't perfectly preserve all data, you'll never be able to fully reverse operations. Floating-point arithmetic is approximate (for the most part). You need a more deterministic data type, such as integers, fixed-points, strings or similar. – Ted Klein Bergman Jan 06 '21 at 18:34
-
is it not possible at all then or could libraries like decimal and fraction solve this? I just would like to be sure before going down the rabbit hole. thx – Justin Farrugia Jan 08 '21 at 00:02
-
I suspect not. Logarithms are inherently imprecise in any numeric system, since they're transcendental functions. – Barmar Jan 08 '21 at 00:10