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
Related
This question already has answers here:
Print pi to a number of decimal places
(8 answers)
Closed 2 years ago.
I am trying to get pi value in python. Here is my source code.
import math
pi_formatted_float = "{:.5000f}".format(math.pi)
print(pi_formatted_float)
Using the source code I only can get 48 decimal places. Others are only 00000000....
More simply, you can test with
>>> "{:.60f}".format(1/3)
'0.333333333333333314829616256247390992939472198486328125000000'
It's not just a problem of PI, but common in all float type. You may find more information from Limiting floats to two decimal points.
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
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 is the difference between '/' and '//' when used for division?
(16 answers)
Closed 5 years ago.
I have python2.7 installed on ubuntu 16.04. It's showing strange behavior. If I check for (5/4), it will just display 1 as output.
for (6/7) it is showing 0
Can anyone help me to fix this?
Not a strange error at all. Python 2.7 does Integer division, so you will always get an integers as the result of the expressions you have named. You can view it this way if you want:
5/4=int(5/4)=int(1.25) = 1
And this is how you can understand the expression you have shown:
float(5/4)= float(int(5/4)) = float(1) = 1.0
you need to use floats:
5.0/4.0