Skip to content

Commit b09d795

Browse files
committed
bug fix for BIN and changes to return values of POLYCENT and POLYAREA
1 parent eefca91 commit b09d795

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

samples/distro-examples/tests/all.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ print "TSAVE:" ':TSAVE file, var
9999
print "VIEW:" ':VIEW [x1,y1,x2,y2 [,color [,border-color]]]
100100
print "WINDOW:" ':WINDOW [x1,y1,x2,y2]
101101
print "WRITE:" ':WRITE #fileN; var1 [, ...]
102-
print "ABS:" + ABS (12.2222)
102+
print "ABS:" + ABS (-12.2222)
103103
print "ABSMAX:" + ABSMAX (1,2,3,4,5,6,7,8,9)
104104
print "ABSMIN:" + ABSMIN (1,2,3,4,5,6,7,8,9)
105105
print "ACOS:" + ACOS (x)

samples/distro-examples/tests/output/all.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ ATANH:nan
103103
ATN:1.489673934694
104104
BCS:catsanddogs
105105
BGETC:
106-
BIN:00000000000000000000000000001100
106+
BIN:1100
107107
CAT:
108108
CBS:catsanddogs
109109
CEIL:13

src/common/blib_func.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -957,18 +957,34 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
957957
//
958958
case kwBIN:
959959
l = v_getint(arg);
960-
IF_ERR_RETURN;
960+
IF_ERR_RETURN;
961+
962+
if (l == 0) {
963+
r->v.p.ptr = (char *)malloc(2);
964+
strcpy(r->v.p.ptr, "0");
965+
r->v.p.length = strlen(r->v.p.ptr) + 1;
966+
break;
967+
}
968+
961969
tb = malloc(33);
962-
memset(tb, 0, 33);
970+
memset(tb, 0, 33);
971+
963972
for (int i = 0; i < 32; i++) {
964973
if (l & (1 << i)) {
965974
tb[31 - i] = '1';
966975
} else {
967976
tb[31 - i] = '0';
968977
}
969978
}
979+
980+
// remove preceding zeros
981+
p = tb;
982+
while (*p == '0') {
983+
p++;
984+
}
970985

971-
r->v.p.ptr = tb;
986+
r->v.p.ptr = (char *)malloc(strlen(p) + 1);
987+
strcpy(r->v.p.ptr, p);
972988
r->v.p.length = strlen(r->v.p.ptr) + 1;
973989
break;
974990
case kwHEX:
@@ -2307,7 +2323,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
23072323
for (int i = 0; i < count - 1; i++) {
23082324
r->v.n = r->v.n + (poly[i].x - poly[i + 1].x) * (poly[i].y + poly[i + 1].y);
23092325
}
2310-
r->v.n = fabs(r->v.n / 2);
2326+
r->v.n = r->v.n / 2;
23112327

23122328
free(poly);
23132329
}
@@ -2327,17 +2343,21 @@ void cmd_genfunc(long funcCode, var_t *r) {
23272343
IF_ERR_RETURN;
23282344

23292345
err = geo_polycentroid(poly, count, &x, &y, &area);
2330-
v_toarray1(r, 2);
2331-
v_setreal(v_elem(r, 0), x);
2332-
v_setreal(v_elem(r, 1), y);
23332346

23342347
if (err == 1) {
23352348
rt_raise(ERR_WRONG_POLY);
23362349
}
23372350
if (err == 2) {
2338-
rt_raise(ERR_CENTROID);
2339-
}
2351+
// return empty array insead of "rt_raise(ERR_CENTROID);"
2352+
v_toarray1(r, 0);
2353+
free(poly);
2354+
break;
2355+
}
23402356

2357+
v_toarray1(r, 2);
2358+
v_setreal(v_elem(r, 0), x);
2359+
v_setreal(v_elem(r, 1), y);
2360+
23412361
free(poly);
23422362
}
23432363
break;

0 commit comments

Comments
 (0)