There are lots of problems about taking remainders of number couples. With integers, there is no problem. But when it comes to floating-point numbers( I mean, float-float or float-int couples), shell returns a nonsense answer.There is an example below:
>>> 5.8%2 1.7999999999999998 #Correct Answer: 1.8 >>> 25.34%5 0.33999999999999986 #Correct Answer:0.34
I recommend you to check numbers before taking them into the process. I will try to explain the “checker code” draft which is in my mind:
-If both numbers are integers, code directly sends these to the main process.
return (x,y) #You can do anything which directly returns x and y.(I preferred to return them as a tuple.)
-If there is a float between these to variables(x and y), code firstly does this(to make sure that both numbers are float):
Then, our code must calculate the lengths of the fractional(after-dot) pieces. We will use these lengths to make integers.
After that, we must change our floats into integers. (Because modulo returns a correct respond, if both numbers are integers.) To change a float into an integer -we want to use the same characters(12,34->1234)- we must multiply it with 10^^length_of_the_fractional_part.There is an example below:
ourIntegerNumber=34.45 * 10**2 #and this is equivalent to 3445
But, we have two numbers. To save their ratio, we must multiply them with the same number. And this number will be equivalent to the biggest fractional part’s length. So, at this point, our code must do something like this:
if lenFractx>lenFracty: x*=10**lenFractx y*=10**lenFractx floatMultiplier=lenFractx else: x*=10**lenFracty y*=10**lenFracty floatMultiplier=lenFracty
Right now, our variables are ready to go. But you mustn’t forget to divide the remainder by 10^^biggest_fractional_length. You may add a new parameter to your remainder function called floatMultiplier(which will also has default value of None). If this parameter is None, your code won’t do anything extra. If it has an integer value passed into it, then your remainder function will divide the result by 10**floatMultiplier and return it.
I bringed together all this stuff here(I suppose your modulo function is called remainder and requires only two args):
if ("." not in str(x) and "." not in str(y)): remainder(x,y) else: x=float(x) y=float(y) lenFractx=len(str(x).split(sep=".")) lenFracty=len(str(y).split(sep=".")) if lenFractx>lenFracty: x*=10**lenFractx y*=10**lenFractx floatMultiplier=lenFractx else: x*=10**lenFracty y*=10**lenFracty floatMultiplier=lenFracty remainder(x,y,floatMultiplier)
Stuff about your remainder function:
def remainder(x,y,floatMultiplier=None): #bla bla #result=something if floatMultiplier==None: return result else: return result/(10**floatMultiplier)
If you can take this into account, I will be really happy.
Note:I am sorry if there are any mistakes about phrases or language usage. English is not my mother language.Thank you for reading this.