Power operation in Python [duplicate] - python

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.

Related

How is 10%-3 -2 in python? Please explain [duplicate]

This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed 8 days ago.
I am not able to understand how is 10%-3 -2 in python? I am a beginner who just started learning python. Please explain
It is a question of math, the evaluation is like this, (10) % (-3), and if you input this into any calculator, it will give you -2. Try typing 10 modulo -3 on Google and see the result

Python floats giving strange responses [duplicate]

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

Why did python add 1 at the end? console.log(9.89+3.48) = 13.37000000000001 [duplicate]

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...

Large integers addition error in python 2.7.10 [duplicate]

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

Python is not showing floating decimals correctly [duplicate]

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

Categories