I’ve been experimenting with the standard python math module and have come across some subtle difficulties. For example, I’m noticing the following behavior concerning indeterminate forms:
0**0
>>> 1
def inf():
return 1e900
# Will return inf
inf()**inf()
>>> inf
And other anomalies of the sort. I’m writing a calculator, and I’d like to have it be mathematically accurate. Is there something I can do about this? Or, is there some way to circumvent this? Thanks in advance.
There’s nothing wrong with your first example.
0**0is often defined to be 1.The second example is all to do with precision of doubles.
1E900exceeds the maximum positive value of a (most likely 64-bit) double. If you want doubles outside of that range, you’ll have to look into libraries. Fortunately Python has one built-in: the decimal module.For example: