Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/units Recognize reciprocal conversions and generate ...



details:   https://anonhg.NetBSD.org/src/rev/90364dba928f
branches:  trunk
changeset: 556006:90364dba928f
user:      mycroft <mycroft%NetBSD.org@localhost>
date:      Fri Dec 05 22:26:43 2003 +0000

description:
Recognize reciprocal conversions and generate output like GNU units(1).

diffstat:

 usr.bin/units/units.c |  23 +++++++++++++++++++----
 1 files changed, 19 insertions(+), 4 deletions(-)

diffs (51 lines):

diff -r b3d51d893ca3 -r 90364dba928f usr.bin/units/units.c
--- a/usr.bin/units/units.c     Fri Dec 05 22:24:58 2003 +0000
+++ b/usr.bin/units/units.c     Fri Dec 05 22:26:43 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: units.c,v 1.12 2003/05/17 21:12:51 itojun Exp $        */
+/*     $NetBSD: units.c,v 1.13 2003/12/05 22:26:43 mycroft Exp $       */
 
 /*
  * units.c   Copyright (c) 1993 by Adrian Mariano (adrian%cam.cornell.edu@localhost)
@@ -70,6 +70,7 @@
 int    compare __P((const void *, const void *));
 int    compareproducts __P((char **, char **));
 int    compareunits __P((struct unittype *, struct unittype *));
+int    compareunitsreciprocal __P((struct unittype *, struct unittype *));
 int    completereduce __P((struct unittype *));
 void   initializeunit __P((struct unittype *));
 int    main __P((int, char **));
@@ -609,6 +610,14 @@
        compareproducts(first->denominator, second->denominator);
 }
 
+int 
+compareunitsreciprocal(struct unittype * first, struct unittype * second)
+{
+       return
+       compareproducts(first->numerator, second->denominator) ||
+       compareproducts(first->denominator, second->numerator);
+}
+
 
 int 
 completereduce(struct unittype * unit)
@@ -625,9 +634,15 @@
 showanswer(struct unittype * have, struct unittype * want)
 {
        if (compareunits(have, want)) {
-               printf("conformability error\n");
-               showunit(have);
-               showunit(want);
+               if (compareunitsreciprocal(have, want)) {
+                       printf("conformability error\n");
+                       showunit(have);
+                       showunit(want);
+               } else {
+                       printf("\treciprocal conversion\n");
+                       printf("\t* %.8g\n\t/ %.8g\n", 1 / (have->factor * want->factor),
+                           want->factor * have->factor);
+               }
        }
        else
                printf("\t* %.8g\n\t/ %.8g\n", have->factor / want->factor,



Home | Main Index | Thread Index | Old Index