Python Negative Number modulo positive number [duplicate] - python

This question already has answers here:
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Closed 5 years ago.
How come -20 % 3 = 1?
Just confused with the formulae used for negative number % positive number.
(I have seen many question related in quora but still not clear with formula used)

I am not sure about the formula but you can add x to the negative number such that (x+ negative number)>=0 and x is a multiple of mod value . This is right because
x % k = (x+ y*k) % k

Related

Is there a way of typing a squared number? [duplicate]

This question already has answers here:
Python and Powers Math
(3 answers)
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed last year.
Is it possible to type a squared number in python?
Or how to type for example 5 to the power of 2
number**power
As Joran Beasley said you only type **2 next to the number to take it to the power of 2.
In python we multiply with * so ** is multiplying two times, which is square. however *** is an error and **3 is cubic.
however you can also define a method
def power(number,n):
x=1
for n in range(n):
x=number*x
return x
print(power(5,8))

How can a multiplication result of two small positive numbers equal 0 (python)? [duplicate]

This question already has answers here:
What is the smallest number which can be represented in python?
(2 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Why does the result of the next product yield the result 0?
print(3.944069389679206e-306*2.85043837e-46)
The product that results from that multiplication is too small for floats to hold.
Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power.

Difference between int(r/2) and r//2 outputs [duplicate]

This question already has answers here:
Is integer division always equal to the floor of regular division?
(4 answers)
Closed 3 years ago.
I was making an integer to Boolean program and was dealing with some large numbers
The test case was - 15921396743627894741911
When I used
r/2 the output was 7.960698371813948e+21
int(r/2) gave me 7960698371813947736064
and r//2 gave me 7960698371813947370955
Why is the value for the last two cases so vastly different. Thank you
In Python 3, / does "true division", which returns a float, but floats have limited precision.

Index number of decimals in float - python [duplicate]

This question already has answers here:
Print pi to a number of decimal places
(8 answers)
Closed 4 years ago.
I'm writing a program that estimates the value of pi. I want to implement that you in the command line can specify when the result have the specified number of correct decimals.
Example:
python est_pi.py 3
should end the script when the estimation is 3.141...
Is it possible to have a variable real_pi = 3.14159... and then index into the number of decimals or is there any other solution to this problem?
If You can round your result using:
round(0.333333, N)
Where N is the parameter in input of your script
all the details are there: Round float to x decimals?
When you reached the needed precision, you can format the display through:
format(66.66666666666, '.'+str(N)+'f')
That will display your 66.666... with N digits.
In python 3.6 you have the f-string:
value = 2.34558
precision = N
width = 4
print(f'result: {value:{width}.{precision}f}')
Everything is detailed there: Limiting floats to two decimal points

the most efficient way to check if an integer can be divided by a float number in Python 2.7 [duplicate]

This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'

Categories

Resources