diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-02-26 14:05:28 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-02-26 14:23:13 +0100 |
commit | ace81cd46ce53e60fe702cc1ac857989207e7ac4 (patch) | |
tree | f9d8bd67c672ad9c586dd85e03f976bd2206fe71 /miscutils | |
parent | 3d88cc1d371a4a5ae8e1521a1e915479bca52959 (diff) |
bc/dc: fix length(0) and length(0.000nnn) result
function old new delta
zxc_vm_process 6464 6498 +34
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/bc.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 84bbe7b14..91564099e 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -6259,13 +6259,20 @@ static unsigned long xc_program_len(BcNum *n) { size_t len = n->len; - if (n->rdx != len) return len; + if (n->rdx != len) + // length(100): rdx 0 len 3, return 3 + // length(0.01-0.01): rdx 2 len 0, return 2 + // dc: 0.01 0.01 - Zp: rdx 2 len 0, return 1 + return len != 0 ? len : (IS_BC ? n->rdx : 1); + + // length(0): return 1 + // length(0.000nnn): count nnn for (;;) { if (len == 0) break; len--; if (n->num[len] != 0) break; } - return len; + return len + 1; } static BC_STATUS zxc_program_builtin(char inst) @@ -6293,12 +6300,12 @@ static BC_STATUS zxc_program_builtin(char inst) if (inst == XC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); #if ENABLE_BC - else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { + else if (len && opnd->t == XC_RESULT_ARRAY) { bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); } #endif #if ENABLE_DC - else if (len != 0 && !BC_PROG_NUM(opnd, num)) { + else if (len && !BC_PROG_NUM(opnd, num)) { char **str; size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx; @@ -6307,6 +6314,8 @@ static BC_STATUS zxc_program_builtin(char inst) } #endif else { +//TODO: length(.00) and scale(.00) should return 2, they return 1 and 0 now +//(don't forget to check that dc Z and X commands do not break) bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num)); } |