This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am writing a program that deals with adding floats to each other and found something weird.
Examples
---INPUT---
print(1+0.1)
print(1-0.1)
---OUTPUT---
1.10000000000001
0.99999999999999
Why is this happening and how do I stop it?
To do this instead of using floats, I used a list where each value of the list represented by each part of the float that i need. This is not always the best soluton but for the wider application that I need it in this is the best solution!
e.g.
10 1 0.1 0.01
[5, 4, 0, 1]
=> 54.01
Related
This question already has answers here:
Why does changing the sum order returns a different result?
(7 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am working on some floating point addition in Python, i found this difference, changing the order of addition changes the value.
v1=2.7776548790102065
v2=2.932026860135167
v3=-2.5635999386901154
v4=-5.884153623433478
v5=0.16152830205880864
v6=2.614447767673556
v7=5.651999753771971
v8=-7.074990233473147
v9=12.624973219138516
print(v1+v2+v3+v4+v5+v6+v7+v8+v9) # 11.239886986191486
print(v1+v4+v7+v2+v5+v8+v3+v6+v9) # 11.239886986191484
can anyhow suggest me how to rectify this?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
https://i.imgur.com/YYNhvNv.png
Its adding in 1 at the end of that console log output. Why?
Answer is suppose to be only 13.37
Likely because it is adding to numbers of type double (I am not a huge Python expert though, so I could be wrong). The doubletype, much like single does not have perfect precission. Hence some decimal errors can occur...
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I was playing around with Python. I had a doubt about the power operation in Python. So, I tried this:
0726**13 = 54609997061205831773270000000000000L
726**13 = 15565965698792536237226936270158258176L
Why is there a difference between these two? I know it might be trivial. But, I could not figure it out. Could someone please explain? Thanks.
It's because an integer constant beginning with 0 is taken to be an octal value. In this case, 0726 is interpreted as 470:
>>> 0726
470
>>> 470**13
54609997061205831773270000000000000L
>>>
Numbers starting with 0 in Python are represented in Base 8 (octal numbers). That's why you're getting different results.
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 7 years ago.
I am trying to solve a problem using python. In which I have to deal with large integers (upto 500 digits). According to my current stage of understanding, python can handle any numbers in same traditional way. But I have problem in simple addition like this:
>>> p= 1001101111101011011100101100100110111011111011000100111100111110111101011011011100111001100011111010
>>> q= 0011111011111010111101111110101101111001111111100011111101101100100011010011111011111110110011111000
>>> p+q
1001101111105557844987142979708366943425581971579987152809865568761000527613931421735161949470823522L
Can anyone please explain why i got such an error.
Var q starts with a zero, making it an octal number, rather than decimal
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 7 years ago.
So I was surprised I didn't find anything regarding this.
I have a python script which is testing a C++ program. It needs to format a float in the same way std::setprecision does. That is a float like 1.265 should be rounded UP to 1.27 (2 dp).
Now I have the following code:
"{:.2f}".format(myFloat)
The issue is that numbers like 1.265 are rounded to 1.26 and my tests fail. setprecision rounds 1.265 to 1.27.
What is the best way to fix this issue?
You can use double rounding to overcome the inability of binary arithmetic to exactly represent a decimal value.
round(round(1.265, 3) + 0.0005, 2)