Skip site navigation (1)Skip section navigation (2)
Date:      28 Jan 2003 04:36:31 -0000
From:      Jeff Connelly <j@jeff.tk>
To:        FreeBSD-gnats-submit@FreeBSD.org
Cc:        freebsd@xyzzy.cjb.net
Subject:   misc/47576: [PATCH] factor(6)ing of negative numbers
Message-ID:  <20030128043631.28326.qmail@jeff.tk>

next in thread | raw e-mail | index | archive | help

>Number:         47576
>Category:       misc
>Synopsis:       [PATCH] factor(6)ing of negative numbers
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jan 27 20:40:07 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Jeff Connelly <freebsd@xyzzy.cjb.net>
>Release:        FreeBSD 5.0-RELEASE i386
>Organization:
>Environment:
System: FreeBSD aptlap 5.0-RELEASE FreeBSD 5.0-RELEASE #5: Sun Jan 26 05:48:37 GMT 2003 root@aptlap:/usr/obj/usr/src/sys/APTLAP i386

>Description:
	factor(6) cannot factor negative numbers.
>How-To-Repeat:
	# factor -- -4
>Fix:
	Patch given first factors -1 out of negatives, then factors the
	absolute value as usual. A patch has also been provided for the
	accompanying documentation.

--- factor.c.patch begins here ---
--- factor.c.orig	Wed Oct  9 19:55:04 2002
+++ factor.c	Mon Jan 27 07:29:30 2003
@@ -113,6 +113,7 @@
 
 static BN_CTX	*ctx;			/* just use a global context */
 static int	hflag;
+static int	negflag;
 
 int
 main(int argc, char *argv[])
@@ -149,8 +150,7 @@
 			for (p = buf; isblank(*p); ++p);
 			if (*p == '\n' || *p == '\0')
 				continue;
-			if (*p == '-')
-				errx(1, "negative numbers aren't permitted.");
+			negflag = *p == '-';
 			if (BN_dec2bn(&val, buf) == 0 &&
 			    BN_hex2bn(&val, buf) == 0)
 				errx(1, "%s: illegal numeric format.", buf);
@@ -159,8 +159,7 @@
 	/* Factor the arguments. */
 	else
 		for (; *argv != NULL; ++argv) {
-			if (argv[0][0] == '-')
-				errx(1, "negative numbers aren't permitted.");
+			negflag = argv[0][0] == '-';
 			if (BN_dec2bn(&val, argv[0]) == 0 &&
 			    BN_hex2bn(&val, argv[0]) == 0)
 				errx(1, "%s: illegal numeric format.", argv[0]);
@@ -187,9 +186,14 @@
 	if (BN_is_zero(val))	/* Historical practice; 0 just exits. */
 		exit(0);
 	if (BN_is_one(val)) {
-		printf("1: 1\n");
+		if (negflag)
+			printf("1: -1\n");
+		else
+			printf("1: 1\n");
 		return;
 	}
+	if (val < 0)
+		printf("-1 ");
 
 	/* Factor value. */
 
@@ -199,6 +203,8 @@
 	} else
 		BN_print_dec_fp(stdout, val);
 	putchar(':');
+	if (negflag)
+		fputs(" -1", stdout);
 	for (fact = &prime[0]; !BN_is_one(val); ++fact) {
 		/* Look for the smallest factor. */
 		do {
--- factor.c.patch ends here ---

--- factor.6.patch begins here ---
--- factor.6.orig	Fri Nov 29 16:21:33 2002
+++ factor.6	Mon Jan 27 06:35:26 2003
@@ -110,9 +110,10 @@
 The
 .Ar start
 value is terminated by a non-digit character (such as a newline).
+Negative numbers may be specified on the command line if the "--" pseudoflag
+preceeds the negative argument.
 .Sh DIAGNOSTICS
 .Bl -diag
-.It "negative numbers aren't permitted"
 .It "illegal numeric format"
 .It "start value must be less than stop value"
 .It "Result too large"
--- factor.6.patch ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030128043631.28326.qmail>