Is python % operator broken [duplicate] - python

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
I've written some script to help me out finding if something is divisible by other thing, and it fails with larger numbers:
print(x, d)
90744169766518547761620274468374441720233236940 10
print(x/d)
9.074416976651854e+45
print(x / (x/d))
10.0
print(x % (x/d))
2.535301200456459e+30
Since 10.0 is clearly lacking decimal part I don't undertand why % is giving me this trash output?

Does this do what you expect?
>>> print(x//d)
9074416976651854776162027446837444172023323694
>>> print(x // (x//d))
10
>>> print(x % (x//d))
0
The difference is that / in Python 3 always produces a floating point result, even if the operands are integers. In this it differs from C. If you want integer division you need to use //.

Related

is there a function in python to round off three digits after decimal but show all three digits even if zero [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 3 months ago.
i want to round off number 7.00087 and output should be 7.000
I tried round() function but it eliminates zero.
This function gives the right output:
def my_round(x, decimals):
str_decimals = str(x % int(x))
return str(int(x)) + str_decimals[1 : 2 + decimals]
What does my_round() do:
1- It retrieves decimals in a string format as a variable named str_decimals
2- Concatenates rounded int(x) with desired decimals and return as output.
Let's apply it:
Input
x = 7.00087
y = my_round(x, 3)
print(y)
Ouput
>>> 7.000

Python3 does not truncate the decimals of automatically like python2 [duplicate]

This question already has answers here:
Integer division in Python 2 and Python 3
(4 answers)
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 2 years ago.
In python2, if you did
n = 12
n /= 10
n would become 1.
In python 2,
the above would cause n to be 1.2, even if it were passed in an integer parameter like
def foo(self, n: int) -> bool:
print (n / 10)
return True
The simple fix is to just cast it to an integer like so:
n = int(n/10)
But this is quite memory/time costly. Are there better alternatives in python3?
// is integer division, so it removes the decimal from the result.

Python: Function to determine if number is square, cube, etc [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Check if a number is a perfect square
(25 answers)
Closed 5 days ago.
My simple problem is to create a function that determines if a number N can be written as a^n for some given n, i.e. I need to check if N^(1/n) is a whole number. Somehow this function yields wrong results:
def is_power(N, n):
r = float(N) ** ( 1. / float(n) )
return r.is_integer()
For n=2 it works.
For n=3 and N=1,8,27 the function yields True, which is correct. But from then on False, e.g. for 4*4*4=64 or 5*5*5=125. How can I create a working function that finds numbers that are squares/cubes/etc.?
Floating point arithmetic is not exact--see Is floating point math broken?.
So check your answer using exact-integer math. Round r to the nearest integer then see if the power works. This Python 3 code removes some of your redundant type-casting. For Python 2, wrap the calculation of r into an int() typecast (which is not needed for Python 3).
def is_power(N, n):
r = round(N ** (1.0 / n))
return r**n == N

Learning Python, Why isn't this working? [duplicate]

This question already has answers here:
floating point equality in Python and in general
(8 answers)
Closed 9 years ago.
I'm learning python and have been trying various things... for some reason this isn't working!
x = (-2.1)
if ( (0.4*(x)) - (0.02*(x)) + (1.396) ) == 0.598:
print "TRUE!"
else:
print "FALSE!"
print ( (0.4*(x)) - (0.02*(x)) + (1.396) )
It prints FALSE! followed by 0.598 obviously the answer is 0.598 so why does'nt the "if" statement work? Thanks!
If you try to print out
(0.38 * x) + 1.396 # note simplified maths and syntax
You will quickly see why:
0.5979999999999999
This is not exactly equal to 0.598, because of the way floating point numbers (float) work. You are better testing these using tolerance:
a = (0.38 * x) + 1.396
if abs(a - 0.598) < 0.0001:
You're confused because python print is using float.__str__, which in turn trims up to 12 digits, so numbers are not what is printed:
>>> ( (0.4*(x)) - (0.02*(x)) + (1.396) ).__str__()
'0.598'
>>> ( (0.4*(x)) - (0.02*(x)) + (1.396) ).__repr__()
'0.5979999999999999'

float deviation in python list [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python float - str - float weirdness
I run the following code in python on codepad.org:
num = 1.6
print num
list = [num]
print list
num2 = list[0]
print num2
And I get the following output:
1.6
[1.6000000000000001]
1.6
Why the tiny deviation in the list?
list.__str__ calls repr on its elements, where as print calls str:
>>> str(1.6)
'1.6'
>>> repr(1.6)
'1.6000000000000001'
Since floating point numbers are not guaranteed to be exact (and cannot be exact for values that can't be expressed as a * 2b for integers a,b), both representations are correct, or, in other words:
>>> 1.6 == 1.6000000000000001
True

Categories