Thread: Make factorial(0::int2) return 1, as per spec.

Make factorial(0::int2) return 1, as per spec.

From
sugita@sra.co.jp
Date:
Attached is a patch to make factorial(0::int2) return 1.

Index: backend/utils/adt/int.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/int.c,v
retrieving revision 1.48
diff -u -3 -p -r1.48 int.c
--- backend/utils/adt/int.c    2002/02/23 01:01:30    1.48
+++ backend/utils/adt/int.c    2002/05/29 15:56:11
@@ -800,7 +800,9 @@ int2fac(PG_FUNCTION_ARGS)
     int16        arg1 = PG_GETARG_INT16(0);
     int32        result;

-    if (arg1 < 1)
+    if (arg1 == 0)
+        result = 1;
+    else if (arg1 < 1)
         result = 0;
     else
         for (result = 1; arg1 > 0; --arg1)

Re: Make factorial(0::int2) return 1, as per spec.

From
Florian Weimer
Date:
sugita@sra.co.jp writes:

> -    if (arg1 < 1)
> +    if (arg1 == 0)
> +        result = 1;
> +    else if (arg1 < 1)
>          result = 0;
>      else
>          for (result = 1; arg1 > 0; --arg1)

The second "if" should compare with 0, not 1.  (It doesn't make a
difference, it's only about readability.)

--
Florian Weimer                       Weimer@CERT.Uni-Stuttgart.DE
University of Stuttgart           http://CERT.Uni-Stuttgart.DE/people/fw/
RUS-CERT                          +49-711-685-5973/fax +49-711-685-5898

Re: Make factorial(0::int2) return 1, as per spec.

From
Bruce Momjian
Date:
Thanks.  Patch applied.  Seems I fixed the int4 case but missed int2.

---------------------------------------------------------------------------

sugita@sra.co.jp wrote:
> Attached is a patch to make factorial(0::int2) return 1.
>

> Index: backend/utils/adt/int.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/int.c,v
> retrieving revision 1.48
> diff -u -3 -p -r1.48 int.c
> --- backend/utils/adt/int.c    2002/02/23 01:01:30    1.48
> +++ backend/utils/adt/int.c    2002/05/29 15:56:11
> @@ -800,7 +800,9 @@ int2fac(PG_FUNCTION_ARGS)
>      int16        arg1 = PG_GETARG_INT16(0);
>      int32        result;
>
> -    if (arg1 < 1)
> +    if (arg1 == 0)
> +        result = 1;
> +    else if (arg1 < 1)
>          result = 0;
>      else
>          for (result = 1; arg1 > 0; --arg1)

>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: Make factorial(0::int2) return 1, as per spec.

From
Bruce Momjian
Date:
Change made for int2 and int4.

---------------------------------------------------------------------------

Florian Weimer wrote:
> sugita@sra.co.jp writes:
>
> > -    if (arg1 < 1)
> > +    if (arg1 == 0)
> > +        result = 1;
> > +    else if (arg1 < 1)
> >          result = 0;
> >      else
> >          for (result = 1; arg1 > 0; --arg1)
>
> The second "if" should compare with 0, not 1.  (It doesn't make a
> difference, it's only about readability.)
>
> --
> Florian Weimer                       Weimer@CERT.Uni-Stuttgart.DE
> University of Stuttgart           http://CERT.Uni-Stuttgart.DE/people/fw/
> RUS-CERT                          +49-711-685-5973/fax +49-711-685-5898
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026