加减法代码更新,可以处理科学指数形式的数值了。如 1.25E-17 = 0.0000000000000000125- Function PM(Na, Nb, Optional k = 0) '桁数多い文字表示数字の加減計算
- If Na = 0 Or Na = "" Then PM = Nb: Exit Function
- If Nb = 0 Or Nb = "" Then PM = Na: Exit Function
-
- ka = IIf(Left(Na, 1) = "-", "-", "+")
- If Left(Na, 1) = "+" Or Left(Na, 1) = "-" Then Na = Mid(Na, 2)
- If InStr(Na, "E") Then
- If Mid(Na, InStr(Na, "E") + 1, 1) = "-" Then
- If InStr(Na, ".") Then
- Na = "0." & String(Mid(Na, InStr(Na, "E") + 2) - 1, "0") & Left(Na, 1) & Mid(Na, 3, InStr(Na, "E") - 3)
- Else
- Na = "0." & String(Mid(Na, InStr(Na, "E") + 2) - 1, "0") & Left(Na, 1)
- End If
- Else
- If InStr(Na, ".") Then
- Na = Left(Na, 1) & Mid(Na, 3, InStr(Na, "E") - 3) & String(Mid(Na, InStr(Na, "E") + 2) - InStr(Na, "E") + 3, "0")
- Else
- Na = Left(Na, 1) & String(Mid(Na, InStr(Na, "E") + 2) - InStr(Na, "E") + 2, "0")
- End If
- End If
- End If
- Pa = IIf(InStr(Na, ".") = 0, 0, Len(Na) - InStr(Na, "."))
- If Left(Na, 2) = "0." Then Na = Replace(Na, "0.", "") Else Na = Replace(Na, ".", "")
-
-
- kb = IIf(Left(Nb, 1) = "-", "-", "+")
- If Left(Nb, 1) = "+" Or Left(Nb, 1) = "-" Then Nb = Mid(Nb, 2)
- If InStr(Nb, "E") Then
- If Mid(Nb, InStr(Nb, "E") + 1, 1) = "-" Then
- If InStr(Nb, ".") Then
- Nb = "0." & String(Mid(Nb, InStr(Nb, "E") + 2) - 1, "0") & Left(Nb, 1) & Mid(Nb, 3, InStr(Nb, "E") - 3)
- Else
- Nb = "0." & String(Mid(Nb, InStr(Nb, "E") + 2) - 1, "0") & Left(Nb, 1)
- End If
- Else
- If InStr(Nb, ".") Then
- Nb = Left(Nb, 1) & Mid(Nb, 3, InStr(Nb, "E") - 3) & String(Mid(Nb, InStr(Nb, "E") + 2) - InStr(Nb, "E") + 3, "0")
- Else
- Nb = Left(Nb, 1) & String(Mid(Nb, InStr(Nb, "E") + 2) - InStr(Nb, "E") + 2, "0")
- End If
- End If
- End If
- Pb = IIf(InStr(Nb, ".") = 0, 0, Len(Nb) - InStr(Nb, "."))
- If Left(Nb, 2) = "0." Then Nb = Replace(Nb, "0.", "") Else Nb = Replace(Nb, ".", "")
-
- If Pa > Pb Then p = Pa Else p = Pb
- Na = Na & String(p - Pa, "0")
- Nb = Nb & String(p - Pb, "0")
-
- l = IIf(Len(Na) > Len(Nb), Len(Na) + 1, Len(Nb) + 1) \ 12
- ReDim a(l + 2, 4)
- For i = 1 To l + 1
- If i * 12 <= Len(Na) Then a(i, 1) = Mid(Na, Len(Na) - i * 12 + 1, 12) Else If i = Len(Na) \ 12 + 1 Then a(i, 1) = Left(Na, Len(Na) - (i - 1) * 12)
- If i * 12 <= Len(Nb) Then a(i, 2) = Mid(Nb, Len(Nb) - i * 12 + 1, 12) Else If i = Len(Nb) \ 12 + 1 Then a(i, 2) = Left(Nb, Len(Nb) - (i - 1) * 12)
-
- If Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) < 10 ^ 12 Then a(i, 0) = Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) Else a(i + 1, 0) = 1: a(i, 0) = Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) - 10 ^ 12
- a(0, 0) = Right(String(12, "0") & a(i, 0), 12) & a(0, 0)
-
- If Val(a(i, 3)) + Val(a(i, 1)) < Val(a(i, 2)) Then a(i + 1, 3) = -1: a(i, 3) = 10 ^ 12 + Val(a(i, 3)) + Val(a(i, 1)) - Val(a(i, 2)) Else a(i, 3) = Val(a(i, 3)) + Val(a(i, 1)) - Val(a(i, 2))
- a(0, 3) = Right(String(12, "0") & a(i, 3), 12) & a(0, 3)
-
- If Val(a(i, 4)) + Val(a(i, 2)) < Val(a(i, 1)) Then a(i + 1, 4) = -1: a(i, 4) = 10 ^ 12 + Val(a(i, 4)) + Val(a(i, 2)) - Val(a(i, 1)) Else a(i, 4) = Val(a(i, 4)) + Val(a(i, 2)) - Val(a(i, 1))
- a(0, 4) = Right(String(12, "0") & a(i, 4), 12) & a(0, 4)
- Next i
-
- a(0, 0) = a(l + 2, 0) & a(0, 0)
- a(0, 3) = a(l + 2, 3) & a(0, 3)
- a(0, 4) = a(l + 2, 4) & a(0, 4)
-
- If p > 0 Then
- a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - p) & "." & Right(a(0, 0), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 0), 1) = "0" Then a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - 1) Else Exit For
- Next
- If Right(a(0, 0), 1) = "." Then a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - 1)
-
- a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - p) & "." & Right(a(0, 3), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 3), 1) = "0" Then a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - 1) Else Exit For
- Next
- If Right(a(0, 3), 1) = "." Then a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - 1)
-
- a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - p) & "." & Right(a(0, 4), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 4), 1) = "0" Then a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - 1) Else Exit For
- Next
- If Right(a(0, 4), 1) = "." Then a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - 1)
- End If
-
- For i = 1 To (l + 1) * 12
- If Left(a(0, 0), 1) = "0" And Left(a(0, 0), 2) <> "0." Then a(0, 0) = Right(a(0, 0), Len(a(0, 0)) - 1) Else Exit For
- Next
- For i = 1 To (l + 1) * 12
- If Left(a(0, 3), 1) = "0" And Left(a(0, 3), 2) <> "0." Then a(0, 3) = Right(a(0, 3), Len(a(0, 3)) - 1) Else Exit For
- Next
- For i = 1 To (l + 1) * 12
- If Left(a(0, 4), 1) = "0" And Left(a(0, 4), 2) <> "0." Then a(0, 4) = Right(a(0, 4), Len(a(0, 4)) - 1) Else Exit For
- Next
-
- If Left(a(0, 3), 1) = "-" Then a(0, 3) = "-" & a(0, 4)
- If Left(a(0, 4), 1) = "-" Then a(0, 4) = "-" & a(0, 3)
-
- If k = 0 Then
- If ka = "+" And kb = "+" Then
- PM = a(0, 0)
- ElseIf ka = "+" And kb = "-" Then
- PM = a(0, 3)
- ElseIf ka = "-" And kb = "+" Then
- PM = a(0, 4)
- ElseIf ka = "-" And kb = "-" Then
- PM = "-" & a(0, 0)
- End If
- ElseIf k = 1 Then 'a+b
- PM = a(0, 0)
- ElseIf k = 2 Then 'a-b
- PM = a(0, 3)
- ElseIf k = 3 Then 'b-a
- PM = a(0, 4)
- ElseIf k = 4 Then '-a-b
- PM = "-" & a(0, 0)
- End If
-
- End Function
复制代码 |