Home > Software engineering >  VB code overflow problem, novice, please help a look
VB code overflow problem, novice, please help a look

Time:09-17

Option Base 1
Private Sub Command1_Click ()

Dim arr () As an Integer, m As an Integer, I, As an Integer, a As Integer
For a=0 To Op1. Count - 1
If Op1 (a). The Value=https://bbs.csdn.net/topics/ture Then
M=Val (Op1) (a). The Caption)
End the If
Next a
ReDim arr (m)
Arr (1)=1
Arr (2)=1
For I=3 To m
Arr (I)=arr (I - 1) + arr (I - 2) 'here always when I=24 overflow
Next
Text1. Text=arr (m)
End Sub

CodePudding user response:

VB6.0 by use of Integer range for - between 32768 and 32767
You this value the Fibonacci sequence of 46368 (24)
Beyond the scope of the
 Dim arr () As string, m As string, 

And the true spelt wrong

CodePudding user response:

"Value" with integer, don't use String,


With Long, best if calculation number many, even "overflow", use type Double,
Dim arr () As Long or Dim arr () As a Double
A few other changes, with Long, or an Integer can be a huge difference,

CodePudding user response:

Agree with the first half of the upstairs, arr () with long, number theory aspect, is an integer, type double precision is not enough, the numerical everbright useless,

CodePudding user response:

If you want to show off in an ostentatious manner in front of your classmates can be arr () is defined as the variant and the back of the operation will be converted to a decimal data, thus, can calculate m to 139

 
Option Base 1
Private Sub Command1_Click ()
Dim arr () As the Variant, m As Currency, I, As Long As a Long
'For a=0 To Op1. Count - 1
'If Op1 (a). The Value=https://bbs.csdn.net/topics/ture Then
'm=Val (Op1) (a). The Caption)
'End If
'the Next a
M=139 'm maximum values
ReDim arr (m)
Arr (1)=1
Arr (2)=1
For I=3 To m
Arr (I)=CDec (arr (I - 1)) + CDec (arr (I - 2))
Next
Text1. Text=arr (m)
End Sub



Finally the execution result is 50095301248058391139327916261

CodePudding user response:

Chewinggum
reference 3 floor response:
agree with the first half of the upstairs, arr () with long, number theory aspect, is an integer, type double precision is not enough, the numerical everbright useless,

No specific calculated, just have a "feel", use Long also not how many items to overflow,
It's close to 2 ^ n kind, began to don't feel well, how to back is very terror,

In fact in general, the main control algorithm, to calculate "a lot of items and don't have much meaning,"
So I didn't want to so many,

CodePudding user response:

Depends on how big is your m, choosing the appropriate arr () data type,

Long: I=47 when overflow,
Currency: overflow when I=74,
Single type: overflow when I=187,
Double type: when I=1744 overflow, the Variant type is the same,


CodePudding user response:

If the number is larger, and required accuracy, can be used to calculate the custom types, such as type LongLong, there are also many large computing algorithms,

For an easy to understand chestnuts,
You can use two longs to represent your Numbers, said the number of millions of high word, low word means millions of remainder,

Two additive of high words with high word together, lower and lower words together, when low word and more than million, minus the million, the high word plus 1 (carry),
The chestnut has the advantage of easily converted into a string of 10 bytes, high generated string in front, low word before filling with 0 to 6 from the back,

Many methods,

CodePudding user response:

Fyi:
//value calculated at the following number level e of myopia, make e have 1000 valid number decimal places, 
//e=1 + 1/1! + 1/2! + a third! +... 1/n!
#include
#include
using namespace std;
The inline int the compare (string str1, string str2) {//return equal 0, is greater than the return 1, less than the return 1
If (str1. The size () & gt; Str2. The size ()) return 1;//the length of the long integer greater than the length of the integer
Else if (str1. The size () & lt; Str2. The size ()) return 1;
The else pare said return str1.com (str2);//if equal length, head to tail according to the comparison of
}
String SUB_INT (string str1, string str2);
String ADD_INT (string str1, string str2) {//high precision addition
Int sign=1;//sign for the sign bit
String STR.
If (str1 [0]=='-') {
If (str2 [0]=='-') {
Sign=1;
STR=ADD_INT (str1. Erase (0, 1), str2. Erase (0, 1));
} else {
STR=SUB_INT (str2, str1. Erase (0, 1));
}
} else {
If (str2 [0]=='-') {
STR=SUB_INT (str1, str2. Erase (0, 1));
} else {//alignment, the two integers up short integer adding 0
String: : size_type L1, L2,
int i;
L1=str1. The size ();
L2=str2. The size ();
If (L1 & lt; L2) {
For (I=1; i<=L2 - L1. I++) str1="0" + str1;
} else {
For (I=1; i<=L1, L2, I++) str2="0" + str2;
}
Int int1=0, int2=0;//record int2 carry
For (I=str1. The size () - 1; I>=0; I -) {
Int1=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2) % 10;
Int2=(int (str1 [I]) - '0' + int (str2 [I]) - '0' + int2)/10;
STR=char (int1 + '0') + STR.
}
If (int2! STR==0) char (int2 + '0') + STR;
}
}
//operation post-processing the sign bit
If ((sign==1) & amp; & (STR [0]! STR='0'))="-" + STR;
return str;
}
String SUB_INT (string str1, string str2) {//high precision subtraction
Int sign=1;//sign for the sign bit
String STR.
int i,j;
If (str2 [0]=='-') {
STR=ADD_INT (str1, str2. Erase (0, 1));
} else {
Int res=compare (str1, str2);
If (res==0) return "0".
If (res<0 {
Sign=1;
String temp=str1;
Str1=str2;
Str2=temp;
}
String: : size_type tempint;
Tempint=str1. The size () - str2. The size ();
For (I=str2. The size () - 1; I>=0; I -) {
If (str1 + tempint [I] j=1;
While (1) {//zhao4zhong1 add
If (str1 [I + tempint - j]=='0') {
Str1 [I + tempint - j]='9';
j++;
} else {
Str1 [I + tempint - j]=char (int (str1 [I + tempint - j]) - 1);
break;
}
}
STR=char (str1 + tempint [I] - str2 [I] + ':') + STR.
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull