Decimal precision in python without decimal module

I am new to Python and wondered how I could control the decimal precision of any given number without using a decimal unit or floating points (for example: "% 4f"% n).

Examples (edit):

entrance (2/7)

+0.28571428571 ....

input (1/3)

+0.3333333333333333 ....

and I wanted them to be a thousand decimal points or any decimal point. I thought of using it for a while as a controlled loop, but I'm not quite sure how to do it. Thanks

edit: The reason I don't use the decimal module is because I can conceptualize the algorithm / logic underlying this type of thing. Just trying to understand the logic of things.

+4
source share
3 answers

Without a Decimal module (why?), Assuming Python 3:

def divide(num, den, prec): a = (num*10**prec) // den s = str(a).zfill(prec+1) return s[0:-prec] + "." + s[-prec:] 

Thanks to @nneonneo for the smart idea .zfill() !

 >>> divide(2,7,1000) '0.28571428571428571428571428571428571428571428571428571428571428571428571428571 42857142857142857142857142857142857142857142857142857142857142857142857142857142 85714285714285714285714285714285714285714285714285714285714285714285714285714285 71428571428571428571428571428571428571428571428571428571428571428571428571428571 42857142857142857142857142857142857142857142857142857142857142857142857142857142 85714285714285714285714285714285714285714285714285714285714285714285714285714285 71428571428571428571428571428571428571428571428571428571428571428571428571428571 42857142857142857142857142857142857142857142857142857142857142857142857142857142 85714285714285714285714285714285714285714285714285714285714285714285714285714285 71428571428571428571428571428571428571428571428571428571428571428571428571428571 42857142857142857142857142857142857142857142857142857142857142857142857142857142 85714285714285714285714285714285714285714285714285714285714285714285714285714285 7142857142857142857142857142857142857142857' 

Caveat: this uses gender separation, so divide(2,3,2) will give you 0.66 instead of 0.67 .

+5
source

We can use long to store the decimal fraction with high precision and perform arithmetic. Here's how you printed it:

 def print_decimal(val, prec): intp, fracp = divmod(val, 10**prec) print str(intp) + '.' + str(fracp).zfill(prec) 

Using:

 >>> prec = 1000 >>> a = 2 * 10**prec >>> b = a//7 >>> print_decimal(b, prec) 0. 
+7
source

While other answers use very large values ​​to handle accuracy, this implements a long separation.

 def divide(num, denom, prec=30, return_remainder=False): "long divison" remain=lim=0 digits=[] #whole part for i in str(num): d=0;remain*=10 remain+=int(i) while denom*d<=remain:d+=1 if denom*d>remain:d-=1 remain-=denom*d digits.append(d) #fractional part if remain:digits.append('.') while remain and lim<prec: d=0;remain*=10 while denom*d<=remain:d+=1 if denom*d>remain:d-=1 remain-=denom*d digits.append(d) lim+=1 #trim leading zeros while digits[0]==0 and digits[1]!='.': digits=digits[1:] quotient = ''.join(list(map(str,digits))) if return_remainder: return (quotient, remain) else: return quotient 

Because it is a division algorithm, each digit will be correct, and you can get the remainder (as opposed to dividing the sexes, which will have no remainder). I implemented accuracy here as the number of digits after the decimal place.

 >>> divide(2,7,70) '0.2857142857142857142857142857142857142857142857142857142857142857142857' >>> divide(2,7,70,True) ('0.2857142857142857142857142857142857142857142857142857142857142857142857', 1) 
0
source

Source: https://habr.com/ru/post/1441629/


All Articles