From 2c0de9ee0253f4a1561dfbd966b19645e74fba62 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sun, 29 May 2022 11:46:54 -0400 Subject: [PATCH] =?UTF-8?q?Check=20rank=20vector=20completely=20before=20s?= =?UTF-8?q?tarting=20computations=20in=20=E2=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/md2.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/builtins/md2.c b/src/builtins/md2.c index 191e6f1e..bdca0f1b 100644 --- a/src/builtins/md2.c +++ b/src/builtins/md2.c @@ -210,31 +210,38 @@ static B m2c2(B t, B f, B g, B w, B x) { // consumes w,x return r; } +static usz check_rank_vec(B g) { + if (!isArr(g)) thrM("⎉: Invalid 𝔾 result"); + usz gia = a(g)->ia; + if (!(gia>=1 && gia<=3)) thrM("⎉: 𝔾 result must have 1 to 3 elements"); + SGetU(g) + if (TI(g,elType)>=el_f64) for (i32 i = 0; i < gia; i++) { + f64 e = o2f(GetU(g,i)); + if (floor(e)!=e) thrM("⎉: 𝕘 was a fractional number"); + } + return gia; +} B rank_c1(Md2D* d, B x) { B f = d->f; B g = d->g; f64 kf; bool gf = isFun(g); if (RARE(gf)) g = c1(g, inc(x)); if (LIKELY(isNum(g))) { kf = o2fu(g); - } else if (isArr(g)) { - usz gia = a(g)->ia; - if (!(gia>=1 && gia<=3)) thrM("⎉: 𝔾 result must have 1 to 3 elements"); - SGetU(g) - if (!elNum(TI(g,elType))) for (i32 i = 0; i < gia; i++) o2f(GetU(g,i)); - kf = GetU(g, gia==2).f; - } else thrM("⎉: Invalid 𝔾 result"); + if (floor(kf)!=kf) thrM("⎉: 𝕘 was a fractional number"); + } else { + usz gia = check_rank_vec(g); + SGetU(g); kf = GetU(g, gia==2).f; + } if (gf) dec(g); i32 k = kf; if (isAtm(x) || rnk(x)==0) { - if (floor(kf)!=kf) thrM("⎉: 𝕘 was a fractional number"); B r = c1(f, x); return isAtm(r)? m_atomUnit(r) : r; } i32 xr = rnk(x); usz* xsh = a(x)->sh; if (k!=kf) { - if (floor(kf)!=kf) thrM("⎉: 𝕘 was a fractional number"); k = kf>0? 0 : xr; } else { k = k<0? (k+xr<0? xr : xr-(k+xr)) : (k>xr? 0 : xr-k);