[qfcc] Allow casting float algebras to double

And vis-versa.

I'm not sure what I was thinking, but I've decided that not being able
to cast the pseudo-scalar from float to double (for printf etc) was a
bug.
This commit is contained in:
Bill Currie 2023-09-09 23:24:47 +09:00
parent ed824405fe
commit 55fc343383
3 changed files with 24 additions and 14 deletions

View file

@ -2143,11 +2143,21 @@ algebra_cast_expr (type_t *dstType, expr_t *e)
type_t *srcType = get_type (e); type_t *srcType = get_type (e);
if (dstType->type == ev_invalid if (dstType->type == ev_invalid
|| srcType->type == ev_invalid || srcType->type == ev_invalid
|| type_size (dstType) != type_size (srcType)
|| type_width (dstType) != type_width (srcType)) { || type_width (dstType) != type_width (srcType)) {
return cast_error (e, srcType, dstType); return cast_error (e, srcType, dstType);
} }
return new_alias_expr (dstType, e); if (type_size (dstType) == type_size (srcType)) {
return new_alias_expr (dstType, e);
}
auto algebra = algebra_get (is_algebra (srcType) ? srcType : dstType);
if (is_algebra (srcType)) {
auto alias = new_alias_expr (algebra->type, e);
return cast_expr (dstType, alias);
} else {
auto cast = cast_expr (algebra->type, e);
return new_alias_expr (dstType, cast);
}
} }
static void static void

View file

@ -52,6 +52,6 @@ main (void)
printf ("%q\n", pgaf1.vec); printf ("%q\n", pgaf1.vec);
printf ("%v %v\n", pgaf1.bvect, pgaf1.bvecp); printf ("%v %v\n", pgaf1.bvect, pgaf1.bvecp);
printf ("%q\n", pgaf1.tvec); printf ("%q\n", pgaf1.tvec);
printf ("%g\n", (float)pgaf1.qvec); printf ("%g\n", pgaf1.qvec);
return 0; // to survive and prevail :) return 0; // to survive and prevail :)
} }

View file

@ -71,7 +71,7 @@ test_wedge (void)
return 1; return 1;
} }
if ((scalar_t)qvec != 8) { if ((scalar_t)qvec != 8) {
printf ("tvec != 8: %g\n", (scalar_t) qvec); printf ("tvec != 8: %g\n", qvec);
return 1; return 1;
} }
@ -89,7 +89,7 @@ test_wedge (void)
auto c = bvecd; auto c = bvecd;
if ((scalar_t)c != 842) { if ((scalar_t)c != 842) {
printf ("b ∧ vecd != 742': %g\n", (scalar_t) c); printf ("b ∧ vecd != 742': %g\n", c);
return 1; return 1;
} }
@ -108,19 +108,19 @@ test_wedge (void)
c = vecdb; c = vecdb;
if ((scalar_t)c != 842) { if ((scalar_t)c != 842) {
printf ("vecd ^ b != 842': %g\n", (scalar_t) c); printf ("vecd ^ b != 842': %g\n", c);
return 1; return 1;
} }
c = a.bvec(veccvecd); c = a.bvec(veccvecd);
if ((scalar_t)c != -842) { if ((scalar_t)c != -842) {
printf ("a ∧ (vecc ∧ vecd) != -742': %g\n", (scalar_t) c); printf ("a ∧ (vecc ∧ vecd) != -742': %g\n", c);
return 1; return 1;
} }
c = (vecdvecc)a.bvec; c = (vecdvecc)a.bvec;
if ((scalar_t)c != 842) { if ((scalar_t)c != 842) {
printf ("(vecd ∧ vecc) ∧ a != 742': %g\n", (scalar_t) c); printf ("(vecd ∧ vecc) ∧ a != 742': %g\n", c);
return 1; return 1;
} }
return 0; return 0;
@ -298,7 +298,7 @@ test_geom (void)
if ((vec3)e.dir != '-6 4 -8' || (vec3)e.mom != '30 17 -14' if ((vec3)e.dir != '-6 4 -8' || (vec3)e.mom != '30 17 -14'
|| e.scalar || (scalar_t)e.qvec != 21) { || e.scalar || (scalar_t)e.qvec != 21) {
printf ("vec * tvec != 0 '-6 4 -8' '30 17 -14' 21: %g %v %v %g\n", printf ("vec * tvec != 0 '-6 4 -8' '30 17 -14' 21: %g %v %v %g\n",
e.scalar, e.bvec.dir, e.bvec.mom, (scalar_t) e.qvec); e.scalar, e.bvec.dir, e.bvec.mom, e.qvec);
return 1; return 1;
} }
@ -306,7 +306,7 @@ test_geom (void)
if (e.scalar != -141 || (vec3)e.bvec.dir || (vec3)e.bvec.mom if (e.scalar != -141 || (vec3)e.bvec.dir || (vec3)e.bvec.mom
|| (scalar_t)e.qvec != -78) { || (scalar_t)e.qvec != -78) {
printf ("bvec * bvec != -141 '0 0 0' '0 0 0' -78: %g %v %v %g\n", printf ("bvec * bvec != -141 '0 0 0' '0 0 0' -78: %g %v %v %g\n",
e.scalar, e.bvec.dir, e.bvec.mom, (scalar_t)e.qvec); e.scalar, e.bvec.dir, e.bvec.mom, e.qvec);
return 1; return 1;
} }
@ -321,7 +321,7 @@ test_geom (void)
if (e.scalar != -9 || (scalar_t)e.qvec if (e.scalar != -9 || (scalar_t)e.qvec
|| (vec3)e.bvec.dir != '-48 20 46' || (vec3)e.bvec.mom != '44 -14 52') { || (vec3)e.bvec.dir != '-48 20 46' || (vec3)e.bvec.mom != '44 -14 52') {
printf ("(vec * vecb) != -9 '-48 20 46' '44 -14 52' 0': %g %v %v %g\n", printf ("(vec * vecb) != -9 '-48 20 46' '44 -14 52' 0': %g %v %v %g\n",
e.scalar, e.bvec.dir, e.bvec.mom, (scalar_t) e.qvec); e.scalar, e.bvec.dir, e.bvec.mom, e.qvec);
return 1; return 1;
} }
@ -335,7 +335,7 @@ test_geom (void)
if (e.scalar || (scalar_t)e.qvec if (e.scalar || (scalar_t)e.qvec
|| (vec3)e.bvec.dir || (vec3)e.bvec.mom != '-88 32 -16') { || (vec3)e.bvec.dir || (vec3)e.bvec.mom != '-88 32 -16') {
printf ("(vec * vecb) != 0 '0 0 0' '-88 32 -16' 0': %g %v %v %g\n", printf ("(vec * vecb) != 0 '0 0 0' '-88 32 -16' 0': %g %v %v %g\n",
e.scalar, e.bvec.dir, e.bvec.mom, (scalar_t) e.qvec); e.scalar, e.bvec.dir, e.bvec.mom, e.qvec);
return 1; return 1;
} }
@ -356,7 +356,7 @@ test_geom (void)
if (e.scalar || (scalar_t)e.qvec != -21 if (e.scalar || (scalar_t)e.qvec != -21
|| (vec3)e.bvec.dir != '-6 4 -8' || (vec3)e.bvec.mom != '30 17 -14') { || (vec3)e.bvec.dir != '-6 4 -8' || (vec3)e.bvec.mom != '30 17 -14') {
printf ("tvec * vec != 0 '-6 4 -8' '30 17 -14' -21: %g %v %v %g\n", printf ("tvec * vec != 0 '-6 4 -8' '30 17 -14' -21: %g %v %v %g\n",
e.scalar, e.bvec.dir, e.bvec.mom, (scalar_t) e.qvec); e.scalar, e.bvec.dir, e.bvec.mom, e.qvec);
return 1; return 1;
} }
@ -412,7 +412,7 @@ test_basics (void)
if (e.scalar || (scalar_t)e.qvec if (e.scalar || (scalar_t)e.qvec
|| (vec3)e.dir != '-8 -8 0' || (vec3)e.mom != '-24 24 0') { || (vec3)e.dir != '-8 -8 0' || (vec3)e.mom != '-24 24 0') {
printf ("x is wrong: %g %v %v %g\n", printf ("x is wrong: %g %v %v %g\n",
e.scalar, e.dir, e.mom, (scalar_t)e.qvec); e.scalar, e.dir, e.mom, e.qvec);
return 1; return 1;
} }
} }