 Is there a better way to simulate CINT?October 22 2004 at 11:48 AM  Mac (Login Mac36) 
Response to Information about arithmetic rounding vs. banker's rounding 

We know that CINT(x) will fail unless x is integer size.
Here is a function that will handle larger values. Can you find a better way?
(Just replace the code of CDBLx# with your better way and post)
Mac
DECLARE FUNCTION CDBLx# (k#)
CLS
PRINT "Proof the function works"
k$ = "34.4": GOSUB Testit
k$ = "34.5": GOSUB Testit
k$ = "34.6": GOSUB Testit
k$ = "35.4": GOSUB Testit
k$ = "35.5": GOSUB Testit
k$ = "35.6": GOSUB Testit
SYSTEM
Testit:
PRINT k$; " "; CDBLx#(VAL(k$)), CDBLx#(VAL("888888" + k$))
PRINT k$; " "; CDBLx#(VAL("" + k$)), CDBLx#(VAL("888888" + k$))
RETURN
FUNCTION CDBLx# (k#)
Max% = 32767
IF ABS(k#) < Max% THEN
r# = CINT(k#)
ELSE
'Need to simulate CINT
k$ = STR$(k#)
y = INSTR(k$, ".")
IF y = 0 THEN
r# = k#
ELSE
k1$ = LEFT$(k$, y  1): k2$ = RIGHT$(k$, LEN(k$)  y + 1)
r# = VAL(k1$)
IF r# < 0 THEN
IF k2$ = ".5" THEN
IF INSTR("02468", RIGHT$(k1$, 1)) = 0 THEN r# = r#  1
ELSE
IF VAL(k2$) > .5 THEN r# = r#  1
END IF
ELSE
IF k2$ = ".5" THEN
IF INSTR("02468", RIGHT$(k1$, 1)) = 0 THEN r# = r# + 1
ELSE
IF VAL(k2$) > .5 THEN r# = r# + 1
END IF
END IF
END IF
END IF
CDBLx# = r#
END FUNCTION
 
 Responses 

