On Jun 9, 3:07 pm, Mel <[EMAIL PROTECTED]> wrote: > Frank Millman wrote: > > > class Number(object): > > def __init__(self,value,scale): > > self.factor = 10.0**scale > > if isinstance(value,Number): > > value = value.value / value.factor > > I think this could lead to trouble. One complaint against binary floating > point is that it messes up low-order decimal digits, and this ensures that > all calculations are effectively done in binary floating point. Better, I > think would be > > if isinstance (value, Number): > self.value = value.value > self.scale = scale + value.scale > > and be done with it. >
Thanks for the reply, Mel. I don't quite understand what you mean. Bear in mind my next line, which you did not quote - if isinstance(value,Number): value = value.value / value.factor --> self.value = long(round(value * self.factor)) I do understand that binary floating point does not always give the expected results when trying to do decimal arithmetic. However, given a float f1 and a scaling factor s, I thought that if I did the following - i1 = long(round(f1 * s)) # scale up to integer f2 = i1 / s # reduce back to float i2 = long(round(f2 * s)) # scale up again then i2 would always be equal to i1. If you are saying that there could be situations where this is not guaranteed, then I agree with you that what I have written is dangerous. I will do some more testing to see if this could happen. I suppose that if the scaling factor is very high it could cause a problem, but I cannot envisage it exceeding 6 in my application. Thanks Frank -- http://mail.python.org/mailman/listinfo/python-list