Int64 The value ranges from −263 to −263 −1-2^{63} to 2^{63}-1−263 to 263−1, that is, -9223372036854775808 to 9223372036854775807 (19 digits). If the value exceeds the value, the system overruns
fmt.Println(9223372036854775808) The execution result is constant9223372036854775808 overflows int
Copy the code
However, when there are relevant requirements, such as assuming that the value of π multiplied by 105010^{50}1050 is A1, and the value of E multiplied by 103010^{30}1030 is A2, the value of A1 +a2 and the value of A1-A2 need to be calculated.
The value of str1+str2 is computed from two string integers, str1 and str2
1. When both numbers are positive integers
Such as a1 + a2
Some of the necessary operations, such as removing all zeros on the far left, do not show in the code,
By default, strings.TrimLeft(STR,”0″) is processed as a positive integer that does not start with 0
func main(a){
str1 := "314159265358979323846264338327950288419716939937510"
str2 := "2718281828459045235360287471352"
rs := ""
if len(str1) > len(str2) {
rs = AddStr(str1,str2)
} else{
rs = AddStr(str2,str1)
}
fmt.Println(rs)
}
func AddStr(n1,n2 string)(res string) {
var num []int
x,y := len(n1),len(n2)
num = make([]int,x+1) // Since there may be cases where the highest bits of the two numbers add up to more than 10, the number of bits of the highest number of the two numbers is +1
for i :=0; i<y; { num[x-i] =int(n1[x- 1-i]-'0') + int(n2[y- 1-i]-'0')
i = i + 1
}
for i:=0; i<x-y; { num[x-y-i] =int(n1[x-y-i- 1] -'0')
i = i + 1
}
// Handle carry
for i := len(num); i>1; {if num[i- 1] /10= =1 {
num[i- 1] - =10
num[i2 -]++
}
i = i - 1
}
// Summarize the string results
for _,i := range num{
s:=strconv.Itoa(i)
res+=s
}
// Remove any possible 0 at the beginning
if len(res) > 1 {
res = strings.TrimLeft(res,"0")}return
}
Copy the code
Running result:
314159265358979323848982620156409333655077227408862
Copy the code
2. When two numbers may be negative integers
So a1 minus A2, or A2 minus A1, or minus A1 minus a2
When both numbers are negative, you take the negative sign out first, so it’s the same as adding two positive integers,
And then you get the result plus the minus sign;
When two numbers, one plus one minus,
The following code has been processed by default: a1 is positive,a2 is negative, and the absolute value of A1 is greater than the absolute value of A2
func main(){ str1 := "314159265358979323846264338327950288419716939937510" str2 := "-2718281828459045235360287471352" str2 = strings.TrimLeft(str2,"-") res := subtractStr(str1,str2) fmt.Println(res) } func subtractStr(n1,n2 string)(res string) { var num []int x,y := len(n1),len(n2) num= make([]int,x+1) for i := 0; i < y; { num[x-i] = int(n1[x-1-i]-'0') - int(n2[y-1-i]-'0') i = i + 1 } for i:=0; i<x-y; {num [x - y - I] = int (n1 [x - y - I - 1) - '0') I = I + 1} / / processing carry the for I: = len (num); i>1; If num[i-1] < 0 {num[i-1]+=10 num[i-2]--} I = i-1} for _, I := range num{s:=strconv.Itoa(I) res+=s} If len(res) > 1 {res = strings.TrimLeft(res,"0")} return}Copy the code
Running result:
314159265358979323843546056499491243184356652466158
Copy the code
Consider 1: What happens when str1 and str2 are not necessarily integer strings, but may be decimal ones?
Consideration 2: How to deal with the multiplication and division problem of large string integers