Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Round() Bug
Message
From
05/01/2001 13:30:47
 
 
To
04/01/2001 18:56:13
Mike Mattos
Nationwide Computers
Mississauga, Ontario, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00459503
Message ID:
00459929
Views:
27
>BUT, if you multiply by 10^ 15, add 0.5 , INT() , and then divide by 10^15, I think it works OK!

It works in some cases, but not in all. Take a look at the code below where I've compared results from some of the solutions suggested in this thread. If I've used your idea incorrectly please let me know. There are some interesting variations but none of the solutions gives the desired result in all cases. (Color syntax courtesy of mhHTMLCode by Mike Helland).
CLEAR
SET DECIMALS TO 18
LOCAL lnRound
lnRound = 15
FOR i = 1 TO 3 STEP .1
   *  Use these debugout's to see the 'pure' results.
   *!*   debugout i
   *!*   debugout round( i, lnRound)
   *!*   debugout PaulRound( i, lnRound)
   *!*   debugout RickRound(i, lnRound)
   *!*   debugout MikeRound(i, lnRound)
   *  Use these debugout's to format the results
   *  for easier comparison.
   DEBUGOUT "i =         " + LTRIM( STR( i, 20, 18))
   DEBUGOUT "VFP Round = " + LTRIM( STR( ROUND( i, lnRound), 20, 18))
   DEBUGOUT "PaulRound = " + LTRIM( STR( PaulRound( i, lnRound), 20, 18))
   DEBUGOUT "RickRound = " + LTRIM( STR( RickRound(i, lnRound), 20, 18))
   DEBUGOUT "MikeRound = " + LTRIM( STR( MikeRound(i, lnRound), 20, 18))
   DEBUGOUT " "
ENDFOR
SET DECIMALS TO 2
RETURN

FUNCTION PaulRound( tnNumber, tnDecimals)
RETURN ROUND(VAL(ALLTRIM(STR(tnNumber,20,tnDecimals+1))),tnDecimals)

FUNCTION RickRound( tnNumber, tnDecimals)
LOCAL lnX
lnX = ( ( ( tnNumber * 10^tnDecimals) + .5) / 10^tnDecimals)
RETURN lnX - ( lnX % (1 / 10^tnDecimals))

FUNCTION MikeRound(tnNumber, tnDecimals)
RETURN ROUND( ( INT( tnNumber*(10^15)) + .5 ) / (10^15), tnDecimals)
The results are as follows. Note the error in the value of 'i' beginning with the sixth iteration.
i =         1.000000000000000000
VFP Round = 1.000000000000000000
PaulRound = 1.000000000000000000
RickRound = 1.000000000000000000
MikeRound = 1.000000000000001000
 
i =         1.100000000000000000
VFP Round = 1.100000000000000000
PaulRound = 1.100000000000000000
RickRound = 1.100000000000000000
MikeRound = 1.100000000000001000
 
i =         1.200000000000000000
VFP Round = 1.200000000000000000
PaulRound = 1.200000000000000000
RickRound = 1.200000000000000000
MikeRound = 1.200000000000001000
 
i =         1.300000000000000000
VFP Round = 1.300000000000000000
PaulRound = 1.300000000000000000
RickRound = 1.300000000000000000
MikeRound = 1.300000000000001000
 
i =         1.400000000000000000
VFP Round = 1.400000000000000000
PaulRound = 1.400000000000000000
RickRound = 1.400000000000000000
MikeRound = 1.400000000000001000
 
i =         1.500000000000000000
VFP Round = 1.500000000000001000
PaulRound = 1.500000000000000000
RickRound = 1.500000000000001000
MikeRound = 1.500000000000001000
 
i =         1.600000000000001000
VFP Round = 1.600000000000001000
PaulRound = 1.600000000000001000
RickRound = 1.600000000000000000
MikeRound = 1.600000000000001000
 
i =         1.700000000000001000
VFP Round = 1.700000000000001000
PaulRound = 1.700000000000001000
RickRound = 1.700000000000001000
MikeRound = 1.700000000000001000
 
i =         1.800000000000001000
VFP Round = 1.800000000000001000
PaulRound = 1.800000000000001000
RickRound = 1.800000000000001000
MikeRound = 1.800000000000001000
 
i =         1.900000000000001000
VFP Round = 1.900000000000001000
PaulRound = 1.900000000000001000
RickRound = 1.900000000000001000
MikeRound = 1.900000000000001000
 
i =         2.000000000000001000
VFP Round = 2.000000000000001000
PaulRound = 2.000000000000001000
RickRound = 2.000000000000001000
MikeRound = 2.000000000000001000
 
i =         2.100000000000001000
VFP Round = 2.100000000000001000
PaulRound = 2.100000000000001000
RickRound = 2.100000000000001000
MikeRound = 2.100000000000002000
 
i =         2.200000000000001000
VFP Round = 2.200000000000001000
PaulRound = 2.200000000000001000
RickRound = 2.200000000000001000
MikeRound = 2.200000000000002000
 
i =         2.300000000000001000
VFP Round = 2.300000000000001000
PaulRound = 2.300000000000001000
RickRound = 2.300000000000001000
MikeRound = 2.300000000000002000
 
i =         2.400000000000001000
VFP Round = 2.400000000000001000
PaulRound = 2.400000000000001000
RickRound = 2.400000000000001000
MikeRound = 2.400000000000002000
 
i =         2.500000000000001000
VFP Round = 2.500000000000002000
PaulRound = 2.500000000000001000
RickRound = 2.500000000000002000
MikeRound = 2.500000000000002000
 
i =         2.600000000000001000
VFP Round = 2.600000000000002000
PaulRound = 2.600000000000001000
RickRound = 2.600000000000001000
MikeRound = 2.600000000000002000
 
i =         2.700000000000002000
VFP Round = 2.700000000000002000
PaulRound = 2.700000000000002000
RickRound = 2.700000000000001000
MikeRound = 2.700000000000002000
 
i =         2.800000000000002000
VFP Round = 2.800000000000002000
PaulRound = 2.800000000000002000
RickRound = 2.800000000000002000
MikeRound = 2.800000000000002000
 
i =         2.900000000000002000
VFP Round = 2.900000000000002000
PaulRound = 2.900000000000002000
RickRound = 2.900000000000002000
MikeRound = 2.900000000000002000
Rick Borup, MCSD

recursion (rE-kur'-shun) n.
  see recursion.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform