11-28-2004, 06:22 AM
At issue is the definition of "rounding". The Quickbasic 4.0 manual for CINT says "Converts a numeric expression to an interger by rounding the fractional part of the expression."
Up until two years ago, I only was aware of one method of rounding. Then I discovered that there were at least eight recognized methods. The CINT function incorporates a variation of the IEEE 754 standard for rounding. The CINT does the following:
2.5 is rounded to 2 instead of 3.
-124.5 is rounded to -124 instead of -125.
Many years ago, this strange behavior was considered by me to be an error in the CINT, so that's why I never use CINT.
As a result, I then wrote a little rounding algorithm, as follows:
ZSIGN = SGN(x)
RESULT = INT(x +.5*ZSIGN)
I was happy with it, and used it for years. Recently I found that certain combinations of numbers give erroneous results, like -123.9 gives -125, and -15.9 gives -17. I have a feeling (totally unfounded) that this is a single precision floating point problem with negative numbers, since the input to the algorithm is single.
Then I took a look at an algorithm by Oracle, which avoids doing arithmetic on negative numbers, as follows:
result = SGN(x) * INT(ABS(x) + .5)
I have tested this little honey every which way from Sunday, and it consistently gives the rounded result which I consider "normal".
If you guys want to see Microsoft's document on the different rounding methods, with a table of methods by programming language, please go to the following link:
http://support.microsoft.com/default.asp...us;Q196652
It'll blow your mind!
*****
Up until two years ago, I only was aware of one method of rounding. Then I discovered that there were at least eight recognized methods. The CINT function incorporates a variation of the IEEE 754 standard for rounding. The CINT does the following:
2.5 is rounded to 2 instead of 3.
-124.5 is rounded to -124 instead of -125.
Many years ago, this strange behavior was considered by me to be an error in the CINT, so that's why I never use CINT.
As a result, I then wrote a little rounding algorithm, as follows:
ZSIGN = SGN(x)
RESULT = INT(x +.5*ZSIGN)
I was happy with it, and used it for years. Recently I found that certain combinations of numbers give erroneous results, like -123.9 gives -125, and -15.9 gives -17. I have a feeling (totally unfounded) that this is a single precision floating point problem with negative numbers, since the input to the algorithm is single.
Then I took a look at an algorithm by Oracle, which avoids doing arithmetic on negative numbers, as follows:
result = SGN(x) * INT(ABS(x) + .5)
I have tested this little honey every which way from Sunday, and it consistently gives the rounded result which I consider "normal".
If you guys want to see Microsoft's document on the different rounding methods, with a table of methods by programming language, please go to the following link:
http://support.microsoft.com/default.asp...us;Q196652
It'll blow your mind!
*****