Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect exponent (off by 1) for "large" values when converting … #55

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/c/libqb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6947,9 +6947,58 @@ qbs *qbs_str(float value){
qbs *qbs_str(double value){
static qbs *tqbs;
tqbs=qbs_new(32,1);
static char ch;
static int32 l,i,i2,i3,digits,exponent;
static int32 mult,exp,index;

l=sprintf((char*)&qbs_str_buffer,"% .15E",value);

// the "new version"...

// scan the result (in reverse) to determine the exponent.
exponent = 0; mult = 1;
for (i = l; i > 0; i--) {
if (!(qbs_str_buffer[i] == 0 || qbs_str_buffer[i] == 32)) { // not null or space...
if (qbs_str_buffer[i] == 43) {
break; // if a +, exit for.
} else if (qbs_str_buffer[i] == 45) {
exponent = -exponent;
break; // a -, negate exponent and exit for.
} else {
exponent += (qbs_str_buffer[i] - 48) * mult;
mult *= 10;
}
}
}
digits = 0; exp = 0;
for (i = l; i > 0; i--) {
if (exp == 0) {
if (qbs_str_buffer[i] == 69) { exp = i; } // location of the E
} else if (qbs_str_buffer[i] != 48 && qbs_str_buffer[i] != 46) {
digits = i; break;
}
}
if (digits==0){
tqbs->len=2; tqbs->chr[0]=32; tqbs->chr[1]=48; // tqbs=[space][0]
return tqbs;
}
if (exponent >= 16 || exponent <= -17) {
index=0;
for (i = 0; i < digits + 1; i++) {
ch = qbs_str_buffer[i];
tqbs->chr[index++] = ch;
}
tqbs->chr[index++] = 68;
for (i = exp + 1; i < l; i++) {
ch = qbs_str_buffer[i];
tqbs->chr[index++] = ch;
}
tqbs->len=index;
return tqbs;
}

/* ----- the "old way"; which has problems with formatting certain exponents properly.

//IMPORTANT: assumed l==23
if (l==22){memmove(&qbs_str_buffer[21],&qbs_str_buffer[20],2); qbs_str_buffer[20]=48; l=23;}

Expand Down Expand Up @@ -6978,6 +7027,7 @@ qbs *qbs_str(double value){
if (qbs_str_buffer[19]==45) exponent=-exponent;
//OLD if ((exponent<=15)&&((exponent-digits)>=-16)) goto asdecimal;
if ((exponent<=15)&&((exponent-digits)>=-17)) goto asdecimal;

//fix up exponent to conform to QBASIC standards
//i. cull trailing 0's after decimal point (use digits to help)
//ii. cull leading 0's of exponent
Expand All @@ -6996,6 +7046,8 @@ qbs *qbs_str(double value){
return tqbs;
/////////////////////
asdecimal:
*/

//calculate digits after decimal point in var. i
i=-(exponent-digits+1);
if (i<0) i=0;
Expand Down
Loading