QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 Return to Index  

BEWARE! Earthborn's function looked nice until I tested it >>>

November 7 2007 at 6:21 PM
Moneo  (no login)


Response to Very nice! But could be shortened though...

The following is Earthborn's original function:

FUNCTION Round (Num, Decimals)
n = Num * (10 ^ Decimals)
n = SGN(n) * (ABS(INT(n + .5)))
Round = n / (10 ^ Decimals)
END FUNCTION

The error lies in this statement:
n = SGN(n) * (ABS(INT(n + .5)))

The intent of the ABS is to feed an absolute value to the INT, because INT works differently for negative numbers.

The corrected function follows:

FUNCTION Round (Num, Decimals)
n = Num * (10 ^ Decimals)
R = SGN(N)*INT(ABS(N)+.5) '*** FIXED ***
Round = n / (10 ^ Decimals)
END FUNCTION

NOTE: Earthborn's function did not declare the rounding method used. It is Symmetric Arithmetic Rounding, the most commonly used method.

Regards..... Moneo







 
 Respond to this message   
Responses

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums