Skip to content

Commit

Permalink
'-' sign in ":!.20 qp; leading - sign lost from _0.1fq; crash in 0j_1…
Browse files Browse the repository at this point in the history
… ": 0fq
  • Loading branch information
HenryHRich committed Jan 11, 2025
1 parent 8288b8c commit dcb010f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion jsrc/f.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ struct fmtbuf fmtlong(struct fmtbuf fb, E v){
finish:;
if(ndig!=0&&buf[0]==0){++buf; --dp; --ndig;} // if we added a lead 0 and it is still there, remove it
DO(ndig, buf[i]+='0';) // convert the digits to chars
dp=ndig==0?1:dp; // if there are no digits, input must be 0; set decimal point to 1 to match 0. display
R (struct fmtbuf){buf,fbuf,ndig,dp};
}

Expand All @@ -161,7 +162,7 @@ static FMTF(jtfmtE,E){UI i;
// copy result to output area
I endx0=MIN(r.ndig,jt->ppn); // discard excess significance
I exp0=r.dp; // number of digits of sig before decimal point. Can be neg
s[0]='-'; I sgn0=v->hi<0; // install - sign just in case; set sgn0 to be # chars of sign needed
s[0]='_'; I sgn0=v->hi<0; // install - sign just in case; set sgn0 to be # chars of sign needed
if(BETWEENC(exp0,0,endx0)){
// decimal point within significance: report as decimal
if(exp0==0)s[sgn0++]='0'; // if no digits before decimal point, add one and add it to count of sign chars
Expand Down
7 changes: 6 additions & 1 deletion jsrc/f2.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ static B jtfmte(J jt,B e,I m,I d,C*s,I t,E*wv,A*cellbuf){
C *z=CAV(*cellbuf); // place to build result, which is known to fit
z[0]=' '; z+=s[0]==' '; z[0]='_'; z+=wv->hi<0; // advance s past optional leading space and sign
if(e){ // %e field, scientific
*z++=fmt.buf[0]; if(wdec){*z++='.'; I nfrac=MIN(fmt.ndig-1,wfrac); MC(z,&fmt.buf[1],nfrac); if(unlikely(nfrac<wfrac))mvc(wfrac-nfrac,&z[nfrac],1,"0"); z+=wfrac;} // move sig digits
*z++=fmt.ndig?fmt.buf[0]:'0'; // the digit before decimal point: first digit, but '0' if there are no digits
if(wdec){ // if we should display after the decimal point...
*z++='.'; I nfrac=MIN(MAX(fmt.ndig,1)-1,wfrac); MC(z,&fmt.buf[1],nfrac); // move sig digits. nfrac is # digits left after the (possible) first
if(unlikely(nfrac<wfrac))mvc(wfrac-nfrac,&z[nfrac],1,"0"); // extend with 0s if needed
z+=wfrac; // step over all moved digits, to exponent location
}
*z++='e'; z[0]='_'; z+=(exp<0); sprintf(z,"%d",ABS((int)exp)); // move exponent incl trailing NUL
}else{ // decimal point
if(fmt.dp>0){MC(z,&fmt.buf[0],fmt.dp); z+=fmt.dp;}else *z++='0'; // if there are integer digits, move them; if not lead with 0. Advance z to frac
Expand Down
7 changes: 3 additions & 4 deletions jsrc/wn.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ static NUMH(jtnumfq){E *v;D sgn=1.0;
else if(i<n){R 0;}
// negative exponent; need to make a rational
if(ex<0){
// underflow
if(unlikely(ex<-400)){v->hi=v->lo=0*sgn;R 1;}
if(unlikely(ex<-400)){v->hi=v->lo=0*sgn;R 1;} // underflow
RZ(qquad(v,qtymes(((Q){.n=r,.d=X1}),((Q){.n=X1,.d=xpow(xc(10),xc(-ex))}))));}
else{
if(ex){
// overflow
if(unlikely(ex>400)){v->hi=inf*sgn;R 1;}
if(unlikely(ex>400)){v->hi=inf*sgn;R 1;}// overflow
r=xtymes(r,xpow(xc(10),xc(ex)));}
RZ(xquad(v,r));}
v->hi*=sgn; v->lo*=sgn; // install sign, which does not upset canonical form
R 1;}

static NUMH(jtnumj){C*t,*ta;D x,y;Z*v;
Expand Down
13 changes: 13 additions & 0 deletions test/gj.ijs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ a =: 1 +&(11&c.) 1e_20

'1.000000000000000000000000000000' -: 0j30 ": (*%) 11 c. 665142606648569600281099799288x

'1.000000000000000039' -: ":!.20[1.00000000000000004fq
(,'1') -: ":[1.00000000000000004fq
'_1.000000000000000039' -: ":!.20[_1.00000000000000004fq
(,'_1') -: ":[_1.00000000000000004fq
(,'_3.99999999999999991e_17') -: ":!.20[_0.00000000000000004fq
('3.99999999999999991e_17') -: ":!.20[0.00000000000000004fq

'0.0e0' -: 0j_1 ": 0fq
'0.00e0' -: 0j_2 ": 0fq
'5.00e_1' -: 0j_2 ": 0.5fq
'_5.00e_1' -: 0j_2 ": _0.5fq


NB. qx =: 11 c. 665142606648569600281099799288x
NB. qy =: 11 c. 665142606648569600281099799280x
NB. t =: 9!:12
Expand Down

0 comments on commit dcb010f

Please sign in to comment.