Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: strange results with decimal!

From: lmecir:mbox:vol:cz at: 13-Jan-2002 20:12

If I understood correctly, the original problem was, how to perform some simple operations (addition in this case, but it can be generalized to the multiplication or other operations) to get "accurate" results in the case where the results should be numbers with two decimal digits. If you perform enough normal additions for binary represented two-decimal digit numbers, the error can be of any size. There are two ways how to get "accurate" results in this case, i.e. not allow the error to grow. The first one is, that integer arithmetic is always accurate (no error). If you use integer numbers (the amounts are in cents rather than in dollars), the results will always be accurate. This may be a little bit inconvenient sometimes. The second possible solution is to use rounding. Instead of normal add use rounded add like: rounded-add: func [a [number!] b [number!]] [ round/places a + b 2 ] This way the error is not allowed to grow for any number of additions and if you compare two rounded numbers, they will be equal exactly when they should be. Cheers Ladislav