Python math is wrong
You have reached a new level in computer science, and you are coming of age.
You therefore are now ready for the next step. I have been authorized by the BDFL himself to reveal the following Super Secret document to you. The ancients understood it and deciphered it first, and now, so will you!
The Floating Point Guide
Treat this document with care! Only share this with people you know have reached the same baffling conclusions!
Moderator's Note
This answer is not representative of the expected quality standards on Stack Overflow. However, it has unexpectedly developed a life of its own and is solely preserved for historical significance now.
Wrong math with Python?
Because of octal arithmetic, 013 is actually the integer 11.
>>> 013
11
With a leading zero, 013
is interpreted as a base-8 number and 1*81 + 3*80 = 11.
Note: this behaviour was changed in python 3. Here is a particularly appropriate quote from PEP 3127
The default octal representation of integers is silently confusing to
people unfamiliar with C-like languages. It is extremely easy to
inadvertently create an integer object with the wrong value, because
'013' means 'decimal 11', not 'decimal 13', to the Python language
itself, which is not the meaning that most humans would assign to this
literal.
floating point in python gives a wrong answer
Floating point arithmetic has built-in problems as it's based on a binary approximation of numbers.
There is a good explanation of this in the Python docs.
You can check out the decimal
module if you need more exact answers.
Why is math.sqrt() incorrect for large numbers?
Because math.sqrt(..)
first casts the number to a floating point and floating points have a limited mantissa: it can only represent part of the number correctly. So float(A**2)
is not equal to A**2
. Next it calculates the math.sqrt
which is also approximately correct.
Most functions working with floating points will never be fully correct to their integer counterparts. Floating point calculations are almost inherently approximative.
If one calculates A**2
one gets:
>>> 12345678917**2
152415787921658292889L
Now if one converts it to a float(..)
, one gets:
>>> float(12345678917**2)
1.5241578792165828e+20
But if you now ask whether the two are equal:
>>> float(12345678917**2) == 12345678917**2
False
So information has been lost while converting it to a float.
You can read more about how floats work and why these are approximative in the Wikipedia article about IEEE-754, the formal definition on how floating points work.
Wrong answer from math.log(python 3)
- Yes, the documentation says so explicitly.
Another solution is to use the Decimal class, from the "decimal" library:
import math
from decimal import Decimal, getcontext
getcontext().prec = 6
Decimal(math.log(4913))/Decimal(math.log(17))
Python math module shows completely wrong results?
You should do the following:
sin(radians(90))
radians
convert the given angle from degrees to radians, and since sin
expects radians
, now it should print 1 as you expected.
Related Topics
How Accurate Is Python's Time.Sleep()
Capturing Repeating Subpatterns in Python Regex
Checking If a String Can Be Converted to Float in Python
Filter Dict to Contain Only Certain Keys
Accessing Pandas Column Using Squared Brackets VS Using a Dot (Like an Attribute)
Determine Whether Integer Is Between Two Other Integers
What Are "Named Tuples" in Python
Convert Pyspark String to Date Format
Having Django Serve Downloadable Files
Create an Empty List with Certain Size in Python
How Does Zip(*[Iter(S)]*N) Work in Python
What's the Difference Between Globals(), Locals(), and Vars()
I Can't Install Pyaudio on Windows? How to Solve "Error: Microsoft Visual C++ 14.0 Is Required."
How to Groupby Consecutive Values in Pandas Dataframe
Executable Python Program with All Dependencies for Linux