Releases: 144881-Studios/pymodule
Releases · 144881-Studios/pymodule
PyModule: decimal
Update information:
- bug fixes:
(1) Add decimals modulo ZeroDivisionError
(2) Catch a number not in “decimal” class - add sum function(
self.sum(var1,var2,*vars)
) - More accurate division. Uses
self / value
to keep 100 digits after the decimal point as a infinite decimal places. Usesdecimal(1) / decimal(7)
will return0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571423
. Or uses advanced functionself.placediv(othernum,places,last="round")
. Usesdecimal(19).placediv(decimal(16),2)
will return1.19
.
PyModule: decimal
bug fixes
PyModule: decimal
bug fixes
PyModule: numbmod
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIG update!
Verified. Release ID: d
PyModule: vector1d
Uses from vector1d import *
for best!
This module uses another module: numbmod. Please download it later than v0.0-beta 1.
Verified. Release ID: b
PyModule: decimal
A very accurate module!
You can use it to perform addition, subtraction, multiplication, division and remainder operations.
Use a sign or absolute value for it.
Now supports scientific number.
How to install and import this module:
- Download this module.
- Move these modules to sys.path.
- Type
from decimal import *
.
How to use:
>>> a = decimal(2)
>>> b = decimal(5.0)
>>> c = decimal("0.123456789")
>>> d = decimal(0)
>>> e = decimal("-2.5")
>>> a
2
>>> b
5
>>> c
0.123456789
>>> d
0
>>> e
-2.5
>>> a + b
7
>>> a - b
-3
>>> -a
-2
>>> -e
2.5
>>> abs(e)
2.5
>>> len(c)
1
>>> c.len()
[1, 9]
>>> a * b
10
>>> c * d
0
>>> c * c
0.015241578750190521
>>> c * c * c
0.001881676371789154860897069
>>> f = scidecimal("1.729271E10000")
Verified. Release ID: g
PyModule: char
Uses from char import *
for best.
Verified. Release ID: 8
PyModule: vector1d
Beta version. Uses from vector import *
for best!
This module uses another module: numbmod. Please download it later than v0.0-beta 1.
Verified. Release ID: a
PyModule: fraction
How to install and import this module:
- Download numbmod v0.1.2.
- Download this module.
- Move these modules to sys.path.
- Type
from fraction import *
.
Start to use
Improper fraction:
>>> a = fraction(1,2)
>>> b = fraction(2,3)
>>> c = fraction(9,4)
>>> d = fraction(2.6)
>>> e = fraction(-1,3)
>>> a
1/2
>>> b
2/3
>>> c
9/4
>>> d
13/5
>>> e
-1/3
>>> a + b
5/6
>>> a - b
1/6
>>> b - a
-1/6
>>> a * b
1/3
>>> a / b
3/4
>>> b / a
4/3
>>> b // a
1
>>> a ** 2
1/4
>>> b ** 3
8/27
>>> a.oppo()
2/1
>>> c ** b.oppo()
3.375
>>> float(a)
0.5
>>> -e
1/3
>>> a.write()
1
-
2
>>>
fraction.write(b ** 3)
8
--
27
fraction.write(b ** 5)
32
---
243
fraction.write(e ** 6)
1
---
729
>>> fraction(0)
0/1
>>> fraction(1,0)
Traceback(most recent call last):
File '<stdin>', line 1, in <module>
fraction(1,0)
ZeroDivisionError: division by zero
Mixed fraction:
>>> a = ifraction(0,1,3)
>>> a
0 1/3
>>> b = ifraction(2,6,9)
>>> b
2 2/3
>>> c = ifraction(1,1,18,neg=True)
>>> c
-1 1/18
>>> d = ifraction(0,9,10,neg=True)
>>> d
-0 9/10
source code:
from numbmod import math as _m
from numbmod import xor
class fraction(object) :
def __init__(self,d,c=1) :
if type(d) == fraction :
dd = d
cc = fraction(c)
d = (dd/cc).d
c = (dd/cc).c
elif type(d) == ifraction :
if d.neg :
dd = fraction(-(d.a*d.c+d.d),d.c)
else :
dd = fraction(d.a*d.c+d.d,d.c)
if type(c) == ifraction :
if c.neg :
cc = fraction(-(c.a*c.c+c.d),c.c)
else :
cc = fraction(c.a*c.c+c.d,c.c)
else :
cc = fraction(c)
d = (dd/cc).d
c = (dd/cc).c
else :
if type(c) == fraction :
dd = fraction(d)
cc = c
d = (dd/cc).d
c = (dd/cc).c
elif type(c) == ifraction :
if c.neg :
cc = fraction(-(c.a*c.c+c.d),c.c)
else :
cc = fraction(c.a*c.c+c.d,c.c)
if type(d) == ifraction :
if d.neg :
dd = fraction(-(d.a*d.c+d.d),d.c)
else :
dd = fraction(d.a*d.c+d.d,d.c)
else :
dd = fraction(d)
d = (dd/cc).d
c = (dd/cc).c
elif type(d) == float or type(c) == float :
p = 10**max(_m.decimallen(d),_m.decimallen(c))
d *= p
c *= p
d = round(d)
c = round(c)
if d < 0 and c < 0 : # -|
d = -d # |
c = -c # |
elif d < 0 or c < 0 : # -- Set negative fraction.
d = -abs(d)
c = abs(c)
d / c # Check
gcd = _m.prime.gcd(d,c) # Get greatest common divisor.
d //= gcd # -|
c //= gcd # --- Reduce fraction.
self.d = d
self.c = c
def __str__(self) :
return str(self.d) + "/" + str(self.c)
def __repr__(self) :
return str(self.d) + "/" + str(self.c)
def __float__(self) :
return self.d / self.c
def __eq__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d == other.d
def __gt__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d > other.d
def __lt__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d < other.d
def __ge__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d >= other.d
def __le__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d <= other.d
def __ne__(self,other) :
lcm = _m.prime.lcm(self.c,other.c) # Get least common mutiple.
self.d *= (lcm//self.c) # --|
other.d *= (lcm//other.c) # --
return self.d != other.d
def __abs__(self) :
return fraction(abs(self.d),abs(self.c))
def __add__(self,other) :
lcm = _m.prime.lcm(self.c,other.c)
return fraction(self.d*(lcm//self.c)+other.d*(lcm//other.c),lcm)
def __sub__(self,other) :
lcm = _m.prime.lcm(self.c,other.c)
return fraction(self.d*(lcm//self.c)-other.d*(lcm//other.c),lcm)
def __mul__(self,other) :
return fraction(self.d*other.d,self.c*other.c)
def __truediv__(self,other) :
return fraction(self.d*other.c,self.c*other.d)
def __floordiv__(self,other) :
return round(float((self/other)-((self/other)%fraction(1))))
def __pos__(self) :
return self * fraction(1)
def __neg__(self) :
return self * fraction(-1)
def __mod__(self,other) :
while self >= fraction(0) :
self = self - other
return self + other
def __pow__(self,other,mod=None) :
if type(mod) in [int,float,complex,fraction] :
mod = fraction(mod)
if type(other) == int :
res = fraction(1)
if _m.isnature(other) :
for i in range(other) :
res *= self
else :
for i in range(other) :
res /= self
res %= mod
return res
else :
return pow(float(self),float(other),float(mod))
else :
if type(other) == int :
res = fraction(1)
if _m.isnature(other) :
for i in range(other) :
res *= self
else :
for i in range(other) :
res /= self
return res
else :
return pow(float(self),float(other))
def oppo(self) :
return fraction(self.c,self.d)
def write(self) :
return str(self.d).center(max(len(str(self.d)),len(str(self.c)))) + "\n" + ("-"*max(len(str(self.d)),len(str(self.c)))) + "\n" + str(self.c).center(max(len(str(self.d)),len(str(self.c))))
class ifraction(fraction) :
def __init__(self,a,d=0,c=1,neg=False) :
frac = fraction(a) + fraction(d,c)
if frac < fraction(0) :
frac = -frac
neg = not neg
d = frac.d
c = frac.c
a = d // c
d = abs(d) % c
self.a = a
self.d = d
self.c = c
self.neg = neg
def __str__(self) :
return "-" * self.neg + str(self.a) + " " + str(self.d) + "/" + str(self.c)
def __repr__(self) :
return "-" * self.neg + str(self.a) + " " + str(self.d) + "/" + str(self.c)
def __float__(self) :
if self.neg :
return -(self.a + (self.d / self.c))
else :
return self.a + (self.d / self.c)
def __eq__(self,other) :
return fraction(self) == fraction(other)
def __gt__(self,other) :
return fraction(self) > fraction(other)
def __lt__(self,other) :
return fraction(self) < fraction(other)
def __ge__(self,other) :
return fraction(self) >= fraction(other)
def __le__(self,other) :
return fraction(self) <= fraction(other)
def __ne__(self,other) :
return fraction(self) != fraction(other)
def __abs__(self) :
return ifraction(self.a,self.d,self.c)
def __add__(self,other) :
return ifraction(0,(fraction(self)+fraction(other)).d,(fraction(self)+fraction(other)).c)
def __sub__(self,other) :
return ifraction(0,(fraction(self)-fraction(other)).d,(fraction(self)-fraction(other)).c)
def __mul__(self,other) :
return ifraction(0,(fraction(self)*fraction(other)).d,(fraction(self)*fraction(other)).c)
def __truediv__(self,other) :
return ifraction(0,(fraction(self)/fraction(other)).d,(fraction(self)/fraction(other)).c)
def __floordiv__(self,other) :
return fraction(self.a*self.c+self.d,self.c) // fraction(other.a*other.c+other.d,other.c)
def __pos__(self) :
return self
def __neg__(self) :
self.neg = not self.neg
return self
def __mod__(self,other) :
return ifraction(0,(fraction(self)%fraction(other)).d,(fraction(self)%fraction(other)).c)
def __pow__(self,other,mod=None) :
if type(mod) in [int,float,complex,fraction,ifraction] :
if type(other) == int :
return ifraction(0,pow(fraction(self),other,mod).d,pow(fraction(self),other,mod).c)
else :
return pow(float(self),float(other),float(mod))
else :
if type(other) == int :
return ifraction(0,pow(fraction(self),other).d,pow(fraction(self),other).c)
else :
return pow(float(self),float(other))
def oppo(self) :
return ifraction(fraction(self).oppo())
def write(self) :
return (" "*len(str(self.a))) + str(self.d).center(ma...
PyModule: decimal
A very accurate module!
You can use it to perform addition, subtraction, multiplication, division and remainder operations.
Use a sign or absolute value for it.
How to install and import this module:
- Download this module.
- Move these modules to sys.path.
- Type
from decimal import *
.
How to use:
>>> a = decimal(2)
>>> b = decimal(5.0)
>>> c = decimal("0.123456789")
>>> d = decimal(0)
>>> e = decimal("-2.5")
>>> a
2
>>> b
5
>>> c
0.123456789
>>> d
0
>>> e
-2.5
>>> a + b
7
>>> a - b
-3
>>> -a
-2
>>> -e
2.5
>>> abs(e)
2.5
>>> len(c)
1
>>> c.len()
[1, 9]
>>> a * b
10
>>> c * d
0
>>> c * c
0.015241578750190521
>>> c * c * c
0.001881676371789154860897069
Verified. Release ID: f