Ejemplo n.º 1
0
void 
showanswer(struct unittype * have, struct unittype * want)
{
	if (compareunits(have, want)) {
		printf("conformability error\n");
		showunit(have);
		showunit(want);
	}
	else if (have->offset != want->offset) {
		if (want->quantity)
			printf("WARNING: conversion of non-proportional quantities.\n");
		printf("\t");
		if (have->quantity)
			printf("%.8g\n",
			    (have->factor + have->offset-want->offset)/want->factor);
		else
			printf(" (-> x*%.8g %+.8g)\n\t (<- y*%.8g %+.8g)\n",
			    have->factor / want->factor,
			    (have->offset-want->offset)/want->factor,
			    want->factor / have->factor,
			    (want->offset - have->offset)/have->factor);
	}
	else
		printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
		    want->factor / have->factor);
}
Ejemplo n.º 2
0
void
showanswer(struct unittype *have, struct unittype *want)
{
	if (compareunits(have, want)) {
		printf("conformability error\n");
		showunit(have);
		showunit(want);
	} else
		printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,
		    want->factor / have->factor);
}
Ejemplo n.º 3
0
static void
showanswer(struct unittype * have, struct unittype * want)
{
	if (compareunits(have, want)) {
		if (compareunitsreciprocal(have, want)) {
			printf("conformability error\n");
			showunit(have);
			showunit(want);
		} else {
			printf("\treciprocal conversion\n");
			printf("\t* %.*g\n\t/ %.*g\n",
			    precision, 1 / (have->factor * want->factor),
			    precision, want->factor * have->factor);
		}
	}
	else
		printf("\t* %.*g\n\t/ %.*g\n",
		    precision, have->factor / want->factor,
		    precision, want->factor / have->factor);
}
Ejemplo n.º 4
0
static int
listunits(int expand)
{
	struct unittype theunit;
	const char *thename;
	const char *thedefn;
	int errors = 0;
	int i;
	int printexpansion;

	/*
	 * send error and warning messages to stdout,
	 * and make them look like comments.
	 */
	errprefix = "/ ";

#if 0 /* debug */
	printf("/ expand=%d precision=%d unitcount=%d prefixcount=%d\n",
	    expand, precision, unitcount, prefixcount);
#endif

	/* 1. Dump all primitive units, e.g. "m !a!", "kg !b!", ... */
	printf("/ Primitive units\n");
	for (i = 0; i < unitcount; i++) {
		thename = unittable[i].uname;
		thedefn = unittable[i].uval;
		if (thedefn[0] == PRIMITIVECHAR) {
			printf("%s\t%s\n", thename, thedefn);
		}
	}

	/* 2. Dump all prefixes, e.g. "yotta- 1e24", "zetta- 1e21", ... */
	printf("/ Prefixes\n");
	for (i = 0; i < prefixcount; i++) {
		printexpansion = expand;
		thename = prefixtable[i].prefixname;
		thedefn = prefixtable[i].prefixval;
		if (expand) {
			/*
			 * prefix names are sometimes identical to unit
			 * names, so we have to expand thedefn instead of
			 * expanding thename.
			 */
			initializeunit(&theunit);
			if (addunit(&theunit, thedefn, 0) != 0
			    || completereduce(&theunit) != 0) {
				errors++;
				printexpansion = 0;
				mywarnx("Error in prefix '%s-'", thename);
			}
		}
		if (printexpansion) {
			printf("%s-", thename);
			showunit(&theunit);
		} else
			printf("%s-\t%s\n", thename, thedefn);
	}

	/* 3. Dump all other units. */
	printf("/ Other units\n");
	for (i = 0; i < unitcount; i++) {
		printexpansion = expand;
		thename = unittable[i].uname;
		thedefn = unittable[i].uval;
		if (thedefn[0] == PRIMITIVECHAR)
			continue;
		if (expand) {
			/*
			 * expand thename, not thedefn, so that
			 * we can catch errors in the name itself.
			 * e.g. a name that contains a hyphen
			 * will be interpreted as multiplication.
			 */
			initializeunit(&theunit);
			if (addunit(&theunit, thename, 0) != 0
			    || completereduce(&theunit) != 0) {
				errors++;
				printexpansion = 0;
				mywarnx("Error in unit '%s'", thename);
			}
		}
		if (printexpansion) {
			printf("%s", thename);
			showunit(&theunit);
		} else
			printf("%s\t%s\n", thename, thedefn);
	}

	if (errors)
		mywarnx("Definitions with errors: %d", errors);
	return (errors ? 1 : 0);
}