Das Blöde: sysinfo.version sagt 1.5.2... Irgendwie glaube ich, dass die auf meinem PC laufende 1.5.2 nicht identisch sein wird mit dem, was auf dem Chip läuft...

Vielen Dank für Deine Hilfe, wie auch immer.
Code: Alles auswählen
class numerix:
def __init__(self, floatstr):
intpart, fraction = floatstr.split(".")
self.d = long(10**len(fraction))
self.n = long(intpart) * self.d + long(fraction)
self.n, self.d = self.reduce()
....
Code: Alles auswählen
# -*- coding: iso-8859-15 -*-
def euclidian(a, b):
if b == 0:
return a
else:
return euclidian(b, a % b)
class Numerix:
def __init__(self, floatstr):
intpart, fraction = floatstr.split(".")
self.d = long(10**len(fraction))
self.n = long(intpart) * self.d + long(fraction)
self.reduce()
def reduce(self):
gcd = euclidian(self.n, self.d)
if gcd != 0:
self.n = self.n / gcd
self.d = self.d / gcd
def __add__(self, o):
self.n = self.n * o.d + o.n * self.d
self.d = self.d * o.d
self.reduce()
return self.clone()
def __sub__(self, o):
self.n = self.n * o.d - o.n * self.d
self.d = self.d * o.d
self.reduce()
return self.clone()
def __mul__(self, o):
self.n, self.d = self.n * o.n, self.d * o.d
self.reduce()
return self.clone()
def __div__(self, o):
self.n, self.d = self.n * o.d, self.d * o.n
self.reduce()
return self.clone()
def __str__(self):
return "%ld/%ld" % (self.n, self.d)
def get(self):
return self.n, self.d
def clone(self):
no = Numerix("0.0")
no.n = self.n
no.d = self.d
return no
spongebob.squarepants hat geschrieben:@Dill: Nun wollen wir mal nicht kleinlicher sein, als der Papst. Ich hatte die angemailt - keine Antwort.
Code: Alles auswählen
# -*- coding: iso-8859-15 -*-
def euclidian(a, b):
while b:
a, b = b, a % b
return a
def from_string(floatstr):
intpart, fraction = floatstr.split('.')
denominator = long(10**len(fraction))
nominator = long(intpart) * denominator + long(fraction)
return Fraction(nominator, denominator)
class Fraction:
def __init__(self, nominator=0, denominator=1):
self.nominator = nominator
self.denominator = denominator
self.reduce()
# def from_string(cls, floatstr):
# intpart, fraction = floatstr.split('.')
# denominator = long(10**len(fraction))
# nominator = long(intpart) * denominator + long(fraction)
# return cls(nominator, denominator)
# from_string = staticmethod(from_string)
def reduce(self):
gcd = euclidian(self.nominator, self.denominator)
if gcd:
self.nominator = self.nominator / gcd
self.denominator = self.nominator /gcd
def __add__(self, other):
return Fraction(self.nominator * other.nominator
+ other.nominator * self.denominator,
self.denominator * other.denominator)
def __sub__(self, other):
return Fraction(self.nominator * other.nominator
- other.nominator * self.denominator,
self.denominator * other.denominator)
def __mul__(self, other):
return Fraction(self.nominator * other.nominator,
self.denominator * other.denominator)
def __div__(self, other):
return Fraction(self.nominator * other.denominator,
self.denominator * other.nominator)
def __str__(self):
return '%ld/%ld' % (self.nominator, self.denominator)
def __float__(self):
return float(self.nominator) / float(self.denominator)
a = from_string('65.125004')
b = from_string('2.561')
c = a * b
print c, float(c)
# 41696283811/250000000 166.785135244
d = a / b
print d, float(d)
# 16281251/640250 25.4295212807
Code: Alles auswählen
def reduce(self):
gcd = euclidian(self.nominator, self.denominator)
if gcd:
self.nominator = self.nominator / gcd
self.denominator = self.nominator /gcd
Code: Alles auswählen
def reduce(self):
gcd = euclidian(self.n, self.d)
if gcd != 0:
self.n = self.n / gcd
self.d = self.d / gcd