Rounding numbers was one of my pet peeves over the years, especially
when transferring a program to new processors or systems
(360-->UNIVAC-->PET-->APPLE II-->Mac Plus-->...OS-X, etc.) This was
especially troublesome with two results that were calculated by
different methods but should be the same answer mathematically. Most
realize that there often are small differences in the answers due to
internal binary representations so these answers turn out 'not' equal.
Re rounding problems and its effects...
My approach was to have my own rounding function to be used on the
results of ALL calculations. It's an old method - just add 1/2 of the
next decimal position to the value and then chop (truncate) the
unneeded decimal places. Then use PRINT USING to display the results as
required. (or use LET x$ - USING...) if desired.
In my gymnastics work, the official rule is to keep just 3 decimal
places and IGNORE any extra ones. So I just change my rounding function
and the rest of the program logic remains the same.
The routine works something like this (from memory)...
LOCAL FN Round#( num#, places% ) ' ex. 18.5627, 3
DIM result#, incr#, pwr#
pwr# = 10 ^ places ' <--- 1000
incr# = .5 / pwr# ' <--- .0005
result# = num# + incr# ' <--- 18563.2
result# = result# * pwr# ' <--- 18563
result# = FIX( result# ) ' <--- 18563
result# = result# / pwr ' <--- 18.563
END FN = result#
Most will realize the above instructions could easily be combined or
simplified.
Here's the rest of a short test program and its sample results.
(FB4.2,console mode, OSX 10.3.9).
def tab = 12
print 18.5627, fn round#( 18.5627, 3 )
print 18.5621, fn round#( 18.5621, 3 )
print 18.5625, fn round#( 18.5625, 3 )
print 18.562499, fn round#( 18.562499, 3 )
(Results...)
18.5627 18.563
18.5621 18.562
18.5625 18.563
18.562499 18.562
And now a word from the sponsor...
Just a bit of my philosophy - FB (and other languages) are just tools
to help achieve a goal. If the built-in functions don't do exactly what
you want, then create your own.
This problem was aggravated 3-4 years ago when FB3 changed the way its
INT() function worked. It used to chop off the decimal places in FBI
and FBII (and in MS BASIC, FORTRAN, and ...) but now it rounds to the
nearest integer instead. That caused a bit of updating in some of my
program once the problem was discovered. MORAL - read the manuals
(twice even) when new versions of a program come out. Hopefully changes
will be clearly marked but ... Just need more time.. ;-)
My $.02 worth
- Stu