# Python math is wrong [duplicate]

Possible Duplicate:
Python rounding error with float numbers

``````Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> 4.2 - 1.8
2.4000000000000004
>>> 1.20 - 1.18
0.020000000000000018
>>> 5.1 - 4
1.0999999999999996
>>> 5 - 4
1
>>> 5.0 - 4.0
1.0
``````

Why is Python getting its maths wrong?

43

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.

Tuesday, June 1, 2021

55

Possibly you mean you want to plot these numbers on a graph and find a straight line through them where the overall distance between the line and the numbers is minimized? This is called a linear regression

``````def linreg(X, Y):
"""
return a,b in solution to y = ax + b such that root mean square distance between trend line and original points is minimized
"""
N = len(X)
Sx = Sy = Sxx = Syy = Sxy = 0.0
for x, y in zip(X, Y):
Sx = Sx + x
Sy = Sy + y
Sxx = Sxx + x*x
Syy = Syy + y*y
Sxy = Sxy + x*y
det = Sxx * N - Sx * Sx
return (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det

x = [12, 34, 29, 38, 34, 51, 29, 34, 47, 34, 55, 94, 68, 81]
a,b = linreg(range(len(x)),x)  //your x,y are switched from standard notation
``````

The trend line is unlikely to pass through your original points, but it will be as close as possible to the original points that a straight line can get. Using the gradient and intercept values of this trend line (a,b) you will be able to extrapolate the line past the end of the array:

``````extrapolatedtrendline=[a*index + b for index in range(20)] //replace 20 with desired trend length
``````
Saturday, July 31, 2021

27

Copy and pasted from Math.cs in the .NET 4.0 Reference Source:

``````  public const double PI = 3.14159265358979323846;
public const double E  = 2.7182818284590452354;
``````

No idea what you are looking at.

It was reverse-engineered from a follow-up question that you looked at the auto-generated text that was created from the assembly metadata when you use the Go To Definition context menu item. Yes, the code that generates this text appears to use the default %f formatting on public double constant values. Quite rare btw, there are not a lot of public constants that are double in the .NET framework. You can file a feedback report at connect.microsoft.com

Wednesday, September 1, 2021

66

The expression `(percentage / 100)` is of type integer and as such will be 0 whenever the result rounds to 0.

If you change one of the numbers in the expression to a type that handles decimals. The expression will evaluate to that type instead.

``````(percentage / 100.0)
``````

and then when you multiply it with `bar` that expression will also be of the same type rather than `int`.

Alternatively you can change the order of execution

``````int foo = (bar * percentage) / 100;
``````

There will be some loss of precision but you shouldn't get 0 unless `bar * percentage` is less than 100 as well.

Friday, September 3, 2021

38

The python numeric limitations, such as there are any, are available on the `sys` module:

• `sys.float_info` is a named tuple with floating point limitations for your platform. Floating point numbers consist of a exponent and a precision; you'd have to be more precise about what you mean by the largest number here; the number with the largest exponent and the full precision in use is `sys.float_info.max`.

• `sys.int_info`; not so much limitations as the implementation detail; you should be able to estimate the largest integer possible from this. Python integers are only limited by your available memory.

• `sys.maxsize`; the platform word size and limit to lists and tuples and the likes.

So for integers, there basically is a soft limit to the maximum and minimum values. It depends on how much memory your process can use, and how much memory your process is already using for other things.

In Python 3, there no longer is a separate `long` type, but in Python 2, `sys.maxsize + 1` would have to be a `long`, as would `-sys.maxsize - 2`. Between those two extremes lies the range of possible 'short' integers.

For complex numbers, ordering is a little more.... complex anyway. Complex numbers have a real and imaginary component, both are floats. Guess what? These are python floats and you already have their limit info above:

``````>>> type(1j)
<type 'complex'>
>>> type(1j.real)
<type 'float'>
>>> type(1j.imag)
<type 'float'>
``````
Thursday, September 30, 2021