主题:求助moz
你的高精度乘法函数:
FUNCTION gjdmult$ (s$, m)
l = 10 - LEN(STR$(m))
IF l < 1 THEN l = 1
p = 10 ^ l: v# = 0
FOR x = LEN(s$) - l + 1 TO 1 STEP -l
v# = VAL(MID$(s$, x, l)) * m + v#
ss$ = RIGHT$(STR$(v#), l) + ss$
v# = INT(v# / p)
NEXT x
IF x > 1 - l THEN v# = VAL(LEFT$(s$, x + l - 1)) * m + v#
IF v# > 0 THEN ss$ = LTRIM$(STR$(v#)) + ss$
gjdmult$ = ss$
END FUNCTION
速度虽然快,但是其中的一个数必须是长整型的,如果遇到两个数都是高精度的就不行了(比如求999999999999999999!)所以我编了一个能计算两个高精度整数乘法的程序:
DECLARE FUNCTION gjdmult$ (s$, m&)
DECLARE FUNCTION gjdadd$ (aa$, bb$)
DEFLNG A-Z
CLS
INPUT a$, b$
IF LEN(a$) < LEN(b$) THEN SWAP a$, b$
lb = LEN(b$)
IF lb <= 9 THEN
PRINT gjdmult$(a$, VAL(b$))
ELSE
x = 0: n$ = "0"
FOR i = lb - 8 TO 1 STEP -9
kk = VAL(MID$(b$, i, 9))
xx$ = gjdmult$((a$), (kk)) + STRING$(x * 9, "0")
n$ = gjdadd$((n$), (xx$))
x = x + 1
NEXT i
IF lb MOD 9 <> 0 THEN
kk = VAL(LEFT$(b$, lb MOD 9))
xx$ = gjdmult$((a$), (kk)) + STRING$(x * 9, "0")
n$ = gjdadd$((n$), (xx$))
END IF
PRINT n$
END IF
END
FUNCTION gjdadd$ (x$, y$)
lx = LEN(x$): ly = LEN(y$)
IF lx < ly THEN
SWAP lx, ly: xx$ = y$: yy$ = x$
ELSE
xx$ = x$: yy$ = y$
END IF
yy$ = STRING$(lx - ly, "0") + yy$: c = 0: s$ = ""
FOR i = lx TO 1 STEP -1
ss = VAL(MID$(xx$, i, 1)) + VAL(MID$(yy$, i, 1)) + c
IF ss >= 10 THEN ss = ss - 10: c = 1 ELSE c = 0
s$ = CHR$(48 + ss) + s$
NEXT i
IF c = 1 THEN s$ = s$ + "1"
gjdadd$ = s$
END FUNCTION
FUNCTION gjdmult$ (s$, m)
l = 10 - LEN(STR$(m))
IF l < 1 THEN l = 1
p = 10 ^ l: v# = 0
FOR x = LEN(s$) - l + 1 TO 1 STEP -l
v# = VAL(MID$(s$, x, l)) * m + v#
ss$ = RIGHT$(STR$(v#), l) + ss$
v# = INT(v# / p)
NEXT x
IF x > 1 - l THEN v# = VAL(LEFT$(s$, x + l - 1)) * m + v#
IF v# > 0 THEN ss$ = LTRIM$(STR$(v#)) + ss$
gjdmult$ = ss$
END FUNCTION
用了你的函数,可是有的时候结果不对,怎么回事?
(如输入9999999999999999999999,30000000000000000000000000000000000000)
FUNCTION gjdmult$ (s$, m)
l = 10 - LEN(STR$(m))
IF l < 1 THEN l = 1
p = 10 ^ l: v# = 0
FOR x = LEN(s$) - l + 1 TO 1 STEP -l
v# = VAL(MID$(s$, x, l)) * m + v#
ss$ = RIGHT$(STR$(v#), l) + ss$
v# = INT(v# / p)
NEXT x
IF x > 1 - l THEN v# = VAL(LEFT$(s$, x + l - 1)) * m + v#
IF v# > 0 THEN ss$ = LTRIM$(STR$(v#)) + ss$
gjdmult$ = ss$
END FUNCTION
速度虽然快,但是其中的一个数必须是长整型的,如果遇到两个数都是高精度的就不行了(比如求999999999999999999!)所以我编了一个能计算两个高精度整数乘法的程序:
DECLARE FUNCTION gjdmult$ (s$, m&)
DECLARE FUNCTION gjdadd$ (aa$, bb$)
DEFLNG A-Z
CLS
INPUT a$, b$
IF LEN(a$) < LEN(b$) THEN SWAP a$, b$
lb = LEN(b$)
IF lb <= 9 THEN
PRINT gjdmult$(a$, VAL(b$))
ELSE
x = 0: n$ = "0"
FOR i = lb - 8 TO 1 STEP -9
kk = VAL(MID$(b$, i, 9))
xx$ = gjdmult$((a$), (kk)) + STRING$(x * 9, "0")
n$ = gjdadd$((n$), (xx$))
x = x + 1
NEXT i
IF lb MOD 9 <> 0 THEN
kk = VAL(LEFT$(b$, lb MOD 9))
xx$ = gjdmult$((a$), (kk)) + STRING$(x * 9, "0")
n$ = gjdadd$((n$), (xx$))
END IF
PRINT n$
END IF
END
FUNCTION gjdadd$ (x$, y$)
lx = LEN(x$): ly = LEN(y$)
IF lx < ly THEN
SWAP lx, ly: xx$ = y$: yy$ = x$
ELSE
xx$ = x$: yy$ = y$
END IF
yy$ = STRING$(lx - ly, "0") + yy$: c = 0: s$ = ""
FOR i = lx TO 1 STEP -1
ss = VAL(MID$(xx$, i, 1)) + VAL(MID$(yy$, i, 1)) + c
IF ss >= 10 THEN ss = ss - 10: c = 1 ELSE c = 0
s$ = CHR$(48 + ss) + s$
NEXT i
IF c = 1 THEN s$ = s$ + "1"
gjdadd$ = s$
END FUNCTION
FUNCTION gjdmult$ (s$, m)
l = 10 - LEN(STR$(m))
IF l < 1 THEN l = 1
p = 10 ^ l: v# = 0
FOR x = LEN(s$) - l + 1 TO 1 STEP -l
v# = VAL(MID$(s$, x, l)) * m + v#
ss$ = RIGHT$(STR$(v#), l) + ss$
v# = INT(v# / p)
NEXT x
IF x > 1 - l THEN v# = VAL(LEFT$(s$, x + l - 1)) * m + v#
IF v# > 0 THEN ss$ = LTRIM$(STR$(v#)) + ss$
gjdmult$ = ss$
END FUNCTION
用了你的函数,可是有的时候结果不对,怎么回事?
(如输入9999999999999999999999,30000000000000000000000000000000000000)