Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #53998 > unrolled thread

C-Programming

Started byarvindgupta01031988@gmail.com
First post2014-12-06 11:09 -0800
Last post2014-12-15 23:53 -0800
Articles 20 on this page of 59 — 20 participants

Back to article view | Back to comp.lang.c


Contents

  C-Programming arvindgupta01031988@gmail.com - 2014-12-06 11:09 -0800
    Re: C-Programming "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2014-12-06 11:16 -0800
    Re: C-Programming James Kuyper <jameskuyper@verizon.net> - 2014-12-06 14:46 -0500
      Re: C-Programming Kleuske <kleuske@nowhere.net> - 2014-12-07 10:48 +0000
        Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-07 07:43 -0800
    Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-06 12:22 -0800
      Re: C-Programming gordonb.ukogy@burditt.org (Gordon Burditt) - 2014-12-19 03:29 -0600
        Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-19 01:34 -0800
          Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-19 10:18 +0000
            Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 17:42 +0100
              Re: C-Programming rouben@shadow.(none) (Rouben Rostamian) - 2014-12-19 19:00 +0000
                Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 20:31 +0100
                  Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-19 20:42 +0100
                Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-19 11:54 -0800
          Re: C-Programming raltbos@xs4all.nl (Richard Bos) - 2014-12-19 11:29 +0000
            Re: C-Programming Noob <root@127.0.0.1> - 2014-12-20 10:22 +0100
              Re: C-Programming Keith Thompson <kst-u@mib.org> - 2014-12-20 11:48 -0800
          Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-19 03:33 -0800
            Re: C-Programming Siri Crews <chine.bleu@yahoo.com> - 2014-12-19 04:02 -0800
        Re: C-Programming glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-19 17:28 +0000
    Re: C-Programming jacob navia <jacob@spamsink.net> - 2014-12-06 21:27 +0100
    Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-07 13:07 +0000
    Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-08 01:24 +0100
      Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 04:44 -0800
        Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-16 15:02 +0100
          Re: C-Programming BartC <bc@freeuk.com> - 2014-12-16 18:27 +0000
            Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 01:01 +0100
              Re: C-Programming BartC <bc@freeuk.com> - 2014-12-17 00:23 +0000
          Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 16:30 -0800
            Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 02:18 +0100
              Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-16 18:23 -0800
                Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-17 09:59 +0100
                  Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 08:52 -0800
                    Re: C-Programming David Brown <david.brown@hesbynett.no> - 2014-12-18 11:11 +0100
                      Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-18 09:49 -0800
            Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-17 10:14 +0000
              Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 14:26 +0000
                Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 09:13 -0800
                  Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 17:34 +0000
                    Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-17 23:26 -0800
                      Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-18 00:21 -0800
                      Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-18 17:06 +0000
                        Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-18 09:29 -0800
                          Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 02:36 +0000
                            Re: C-Programming Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-19 03:41 -0800
                    Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-18 08:52 -0800
                Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-17 23:23 +0000
                  Re: C-Programming BartC <bc@freeuk.com> - 2014-12-18 00:02 +0000
                  Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-18 00:15 +0000
                    Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-18 16:37 +0000
                      Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-18 18:08 +0000
                        Re: C-Programming Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 02:40 +0000
                          Re: C-Programming Richard Heathfield <invalid@see.sig.invalid> - 2014-12-19 07:51 +0000
              Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-17 09:07 -0800
                Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-19 04:22 +0000
                  Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-19 09:22 -0800
                    Re: C-Programming Greg Martin <greg.at.softsprocket@dot.com> - 2014-12-20 02:39 +0000
                      Re: C-Programming JohnF <john@please.see.sig.for.email.com> - 2014-12-20 06:57 +0000
    Re: C-Programming Tim Rentsch <txr@alumni.caltech.edu> - 2014-12-15 23:53 -0800

Page 1 of 3  [1] 2 3  Next page →


#53998 — C-Programming

Fromarvindgupta01031988@gmail.com
Date2014-12-06 11:09 -0800
SubjectC-Programming
Message-ID<8effc0a8-53e1-4b2d-940b-913db8f0ce0e@googlegroups.com>
How to use a recursive function in C ?

[toc] | [next] | [standalone]


#53999

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2014-12-06 11:16 -0800
Message-ID<af581ca5-2d66-40d5-9d3c-9d552ba2d15b@googlegroups.com>
In reply to#53998
On Saturday, December 6, 2014 2:10:19 PM UTC-5, Arvind Gupta wrote:
> How to use a recursive function in C ?

    int main(int argc, char* argv[])
    {
        call_self(10);
    }

    int call_self(int thisValue)
    {
        if (thisValue > 0)
            call_self(thisValue - 1);
    }

Their utility is a different matter, subject to application needs.

You will need to process through something to make them of use, in
each case the value of the current thing being dependent upon the
results of the prior thing.

Best regards,
Rick C. Hodgin

[toc] | [prev] | [next] | [standalone]


#54000

FromJames Kuyper <jameskuyper@verizon.net>
Date2014-12-06 14:46 -0500
Message-ID<m5vme6$u4r$1@dont-email.me>
In reply to#53998
On 12/06/2014 02:09 PM, arvindgupta01031988@gmail.com wrote:
> How to use a recursive function in C ?

It's designing a recursive function that's the tricky part. Using one is
trivial: You just call it with appropriate arguments, and store it's
return value, if any, in a suitable place, just like any other function.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#54033

FromKleuske <kleuske@nowhere.net>
Date2014-12-07 10:48 +0000
Message-ID<m61ba8$9dj$1@dont-email.me>
In reply to#54000
On Sat, 06 Dec 2014 14:46:30 -0500, James Kuyper saw fit to publish the
following:

> On 12/06/2014 02:09 PM, arvindgupta01031988@gmail.com wrote:
>> How to use a recursive function in C ?
> 
> It's designing a recursive function that's the tricky part. Using one is
> trivial: You just call it with appropriate arguments, and store it's
> return value, if any, in a suitable place, just like any other function.

Caveat: Make sure the recursion stops at some point, otherwise there 
*will* be problems (most likely a stack-overflow and subsequent crash/
exception/coredump).

[toc] | [prev] | [next] | [standalone]


#54045

FromKeith Thompson <kst-u@mib.org>
Date2014-12-07 07:43 -0800
Message-ID<lnk323mpmv.fsf@nuthaus.mib.org>
In reply to#54033
Kleuske <kleuske@nowhere.net> writes:
> On Sat, 06 Dec 2014 14:46:30 -0500, James Kuyper saw fit to publish the
> following:
>> On 12/06/2014 02:09 PM, arvindgupta01031988@gmail.com wrote:
>>> How to use a recursive function in C ?
>> 
>> It's designing a recursive function that's the tricky part. Using one is
>> trivial: You just call it with appropriate arguments, and store it's
>> return value, if any, in a suitable place, just like any other function.
>
> Caveat: Make sure the recursion stops at some point, otherwise there 
> *will* be problems (most likely a stack-overflow and subsequent crash/
> exception/coredump).

Or an infinite loop if the compiler is able to optimize tail recursion.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#54001

FromSiri Crews <chine.bleu@yahoo.com>
Date2014-12-06 12:22 -0800
Message-ID<chine.bleu-D7F017.12220506122014@news.eternal-september.org>
In reply to#53998
In article <8effc0a8-53e1-4b2d-940b-913db8f0ce0e@googlegroups.com>,
 arvindgupta01031988@gmail.com wrote:

> How to use a recursive function in C ?

# cat t.c
    int factorial(int n) {
        return n==0 ? 1 : n*factorial(n-1);
    }

    int a = 0;
    int A(int m, int n) {
        a++;
        if (m==0) return n+1;
        else if (m>0 && n==0) return A(m-1, 1);
        else return A(m-1, A(m, n-1));
    }

    #include <stdio.h>
    int main(int argc, char **argv) {
        printf("%d\n", factorial(5));
        int A22 = A(2, 2);
        printf("%d %d\n", a, A22);
        a = 0; int A33 = A(3, 3);
        printf("%d %d\n", a, A33);
        return 0;
    }

# cc t.c

# a.out
120
27 7
2432 61

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
Icke's razor: Given two equally plausible explanations, choose the weirder.
Be sure not to operate heavy machinery whilst on usenet.

[toc] | [prev] | [next] | [standalone]


#55044

Fromgordonb.ukogy@burditt.org (Gordon Burditt)
Date2014-12-19 03:29 -0600
Message-ID<-b2dnRE6H59-cg7JnZ2dnUU7-a2dnZ2d@posted.internetamerica>
In reply to#54001
>     int factorial(int n) {
>         return n==0 ? 1 : n*factorial(n-1);
>     }

This has the disadvantage that factorial(-1) will result in infinite
recursion.  The documentation should say not to call factorial(n)
with n < 0, but it's often still a pain to diagnose what caused
stack overflow.  How about if you define (-3)! as being 0 or -1
(an error result)?

(Also, if int is a 16-bit signed integer, n > 7 will cause overflow.
If int is a 32-bit signed integer, n > 12 will cause overflow.)

[toc] | [prev] | [next] | [standalone]


#55046

FromSiri Crews <chine.bleu@yahoo.com>
Date2014-12-19 01:34 -0800
Message-ID<chine.bleu-7CBF22.01345019122014@news.eternal-september.org>
In reply to#55044
In article <-b2dnRE6H59-cg7JnZ2dnUU7-a2dnZ2d@posted.internetamerica>,
 gordonb.ukogy@burditt.org (Gordon Burditt) wrote:

> >     int factorial(int n) {
> >         return n==0 ? 1 : n*factorial(n-1);
> >     }
> 
> This has the disadvantage that factorial(-1) will result in infinite

Factorial is a partial function, undefined on negative integers.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
Icke's razor: Given two equally plausible explanations, choose the weirder.
Be sure not to operate heavy machinery whilst on usenet.

[toc] | [prev] | [next] | [standalone]


#55048

FromRichard Heathfield <invalid@see.sig.invalid>
Date2014-12-19 10:18 +0000
Message-ID<UXSkw.145485$5z5.35816@fx42.am4>
In reply to#55046
Siri Crews wrote:

> In article <-b2dnRE6H59-cg7JnZ2dnUU7-a2dnZ2d@posted.internetamerica>,
>  gordonb.ukogy@burditt.org (Gordon Burditt) wrote:
> 
>> >     int factorial(int n) {
>> >         return n==0 ? 1 : n*factorial(n-1);
>> >     }
>> 
>> This has the disadvantage that factorial(-1) will result in infinite
> 
> Factorial is a partial function, undefined on negative integers.

But see <http://www.nntdm.net/papers/nntdm-19/NNTDM-19-2-30_42.pdf> for a 
contrary view. Having glanced at the paper, I proved to my own satisfaction 
that it is correct, as follows:

1) it isn't from answers.yahoo.com or wikipedia;
2) it's got lots of pages;
3) it has diagrams and scary typography;
4) my goodness, is that the time?
5) QED.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#55083

Fromjacob navia <jacob@spamsink.net>
Date2014-12-19 17:42 +0100
Message-ID<m71kh7$g7q$1@speranza.aioe.org>
In reply to#55048
Le 19/12/2014 11:18, Richard Heathfield a écrit :

> But see <http://www.nntdm.net/papers/nntdm-19/NNTDM-19-2-30_42.pdf> for a
> contrary view.

Very interesting paper. I think I will update my factorial function to 
return the value of the negative factorials!

[toc] | [prev] | [next] | [standalone]


#55101

Fromrouben@shadow.(none) (Rouben Rostamian)
Date2014-12-19 19:00 +0000
Message-ID<m71sk8$s3s$1@news.albasani.net>
In reply to#55083
In article <m71kh7$g7q$1@speranza.aioe.org>,
jacob navia  <jacob@jspamsink.org> wrote:
>Le 19/12/2014 11:18, Richard Heathfield a écrit :
>
>> But see <http://www.nntdm.net/papers/nntdm-19/NNTDM-19-2-30_42.pdf> for a
>> contrary view.
>
>Very interesting paper. I think I will update my factorial function to 
>return the value of the negative factorials!

That won't be a good idea since it will go against the well-established
meaning of the factorial function in mathematics.

In the definition n! = 1 x 2 x ...x n of the factorial shows
only the tip of a huge iceberg.  Actually n! is a small
manifestation of the Gamma function which is defined and is
analytic everywhere on the complex plane except for zero and
negative integers.  We have n! = Gamma(n+1) for n=0,1,2,....
It is known that |Gamma(z)| goes to infinity as z approaches
zero or a negative integer.

If you are going to define n! in some arbitrary way for
negative integers, then you can do it in any way that
you please.  For instance, you may set up your factorial
function so that it returns 0 for all negative integers.
Or you may define n! to be the factorial of the absolute
value of n. 

Beware, however, that your definition will be in conflict with
the C99 standard's tgamma() function whose definition agrees
with the mathematical definition of the Gamma function.

-- 
Rouben Rostamian

[toc] | [prev] | [next] | [standalone]


#55103

Fromjacob navia <jacob@spamsink.net>
Date2014-12-19 20:31 +0100
Message-ID<m71ueb$bvg$1@speranza.aioe.org>
In reply to#55101
Le 19/12/2014 20:00, none Rouben Rostamian a écrit :
> Beware, however, that your definition will be in conflict with
> the C99 standard's tgamma() function whose definition agrees
> with the mathematical definition of the Gamma function.

Surely not!

The standard says:

7.12.8.4 The tgamma functions
Synopsis
1 #include <math.h>
double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x);
Description
2 The tgamma functions compute the gamma function of x. A domain error 
or pole error may occur if x is a negative integer or zero.

Read with me:

MAY occur!

Maybe not. This standard is so worded that you can do anything with it!

So, if I define the tgamma function for negative integers as specified 
in that paper I am still standard conforming.

Besides, if the user gives negative numbers it is because he wants the 
definition specified in that paper. The standard  doesn't even specify 
the error return!

[toc] | [prev] | [next] | [standalone]


#55104

Fromjacob navia <jacob@spamsink.net>
Date2014-12-19 20:42 +0100
Message-ID<m71v2r$dgu$1@speranza.aioe.org>
In reply to#55103
Le 19/12/2014 20:31, jacob navia a écrit :
> Le 19/12/2014 20:00, none Rouben Rostamian a écrit :
>> Beware, however, that your definition will be in conflict with
>> the C99 standard's tgamma() function whose definition agrees
>> with the mathematical definition of the Gamma function.
>
> Surely not!
>
> The standard says:
>
> 7.12.8.4 The tgamma functions
> Synopsis
> 1 #include <math.h>
> double tgamma(double x);
> float tgammaf(float x);
> long double tgammal(long double x);
> Description
> 2 The tgamma functions compute the gamma function of x. A domain error
> or pole error may occur if x is a negative integer or zero.
>
> Read with me:
>
> MAY occur!
>
> Maybe not. This standard is so worded that you can do anything with it!
>
> So, if I define the tgamma function for negative integers as specified
> in that paper I am still standard conforming.
>
> Besides, if the user gives negative numbers it is because he wants the
> definition specified in that paper. The standard  doesn't even specify
> the error return!


P.S. The negative factorial is defined as

factorial(-n) --> factorial(n)*((n&1)?-1:1)

fact(-1) --> -1
fact(-2) -->  2
fact(-3) --> -6
fact(-4) --> 24
fact(-5) --> -120

I.e. is negative if n is odd, positive otherwise.


[toc] | [prev] | [next] | [standalone]


#55105

FromKeith Thompson <kst-u@mib.org>
Date2014-12-19 11:54 -0800
Message-ID<lnh9wrbel0.fsf@nuthaus.mib.org>
In reply to#55101
rouben@shadow.(none) (Rouben Rostamian) writes:
> In article <m71kh7$g7q$1@speranza.aioe.org>,
> jacob navia  <jacob@jspamsink.org> wrote:
>>Le 19/12/2014 11:18, Richard Heathfield a écrit :
>>
>>> But see <http://www.nntdm.net/papers/nntdm-19/NNTDM-19-2-30_42.pdf> for a
>>> contrary view.
>>
>>Very interesting paper. I think I will update my factorial function to 
>>return the value of the negative factorials!
>
> That won't be a good idea since it will go against the well-established
> meaning of the factorial function in mathematics.

For example, it would break the property that n! == (n-1)! * n,
which strikes me as fundamental to what "factorial" means.

Extrapolating backwards from 0! == 1, (-1)! would involve division by
zero, which is why it's left undefined.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#55051

Fromraltbos@xs4all.nl (Richard Bos)
Date2014-12-19 11:29 +0000
Message-ID<54940bd1.848906@news.xs4all.nl>
In reply to#55046
Siri Crews <chine.bleu@yahoo.com> wrote:

>  gordonb.ukogy@burditt.org (Gordon Burditt) wrote:
> 
> > >     int factorial(int n) {
> > >         return n==0 ? 1 : n*factorial(n-1);
> > >     }
> > 
> > This has the disadvantage that factorial(-1) will result in infinite
> 
> Factorial is a partial function, undefined on negative integers.

There's an easier way to solve that: make it take and return unsigned
rather than signed ints.

Richard

(Gordon: permission to quote this message without proper attribution is
explicitly denied.)

[toc] | [prev] | [next] | [standalone]


#55124

FromNoob <root@127.0.0.1>
Date2014-12-20 10:22 +0100
Message-ID<m73f3e$7ev$1@dont-email.me>
In reply to#55051
On 19/12/2014 12:29, Richard Bos wrote:

> (Gordon: permission to quote this message without proper attribution is
> explicitly denied.)

I wish I could smack that "Gordon" fellow with the clue bat.

He's been snipping attribution lines for as long as I can remember.

[toc] | [prev] | [next] | [standalone]


#55164

FromKeith Thompson <kst-u@mib.org>
Date2014-12-20 11:48 -0800
Message-ID<lnd27e9k6f.fsf@nuthaus.mib.org>
In reply to#55124
Noob <root@127.0.0.1> writes:
> On 19/12/2014 12:29, Richard Bos wrote:
>> (Gordon: permission to quote this message without proper attribution is
>> explicitly denied.)
>
> I wish I could smack that "Gordon" fellow with the clue bat.
>
> He's been snipping attribution lines for as long as I can remember.

I have tried and given up.  He has a firm position on why he thinks his
behavior is necessary.  It's complete nonsense, and there's an easy
workaround that would satisfy everyone even if his position were valid,
but he's not going to change his mind.  (His position, as I understand
it, is that the possibility of using an *incorrect* attribution line
would leave him vulnerable to unacceptable liability.  The workaround
would be to configure his newsreader to use an attribution line that
makes it clear that he does not guarantee its accuracy.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#55053

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2014-12-19 03:33 -0800
Message-ID<95c11aff-605d-4f5f-a120-2aabe3f1ec59@googlegroups.com>
In reply to#55046
On Friday, December 19, 2014 9:35:10 AM UTC, Siri Crews wrote:
> 
> Factorial is a partial function, undefined on negative integers.
> 
However you can resort to the gamma function, which is defined
for negatives and real values, not just integers. For positive integers 
it's the same as (n-1)!

[toc] | [prev] | [next] | [standalone]


#55059

FromSiri Crews <chine.bleu@yahoo.com>
Date2014-12-19 04:02 -0800
Message-ID<chine.bleu-6CA003.04021919122014@news.eternal-september.org>
In reply to#55053
In article <95c11aff-605d-4f5f-a120-2aabe3f1ec59@googlegroups.com>,
 Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:

> On Friday, December 19, 2014 9:35:10 AM UTC, Siri Crews wrote:
> > 
> > Factorial is a partial function, undefined on negative integers.
> > 
> However you can resort to the gamma function, which is defined
> for negatives and real values, not just integers. For positive integers 
> it's the same as (n-1)!

Gamma is also a partial function, undefined at nonpositive integers. For some 
reason physicists found that spiritually inspiring.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
Icke's razor: Given two equally plausible explanations, choose the weirder.
Be sure not to operate heavy machinery whilst on usenet.

[toc] | [prev] | [next] | [standalone]


#55093

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2014-12-19 17:28 +0000
Message-ID<m71n8o$o55$1@speranza.aioe.org>
In reply to#55044
Gordon Burditt <gordonb.ukogy@burditt.org> wrote:
>>     int factorial(int n) {
>>         return n==0 ? 1 : n*factorial(n-1);
>>     }
 
> This has the disadvantage that factorial(-1) will result in infinite
> recursion.  The documentation should say not to call factorial(n)
> with n < 0, but it's often still a pain to diagnose what caused
> stack overflow.  

Since lim x-->0 Gamma(x) is infinite, that seems reasonable.

https://en.wikipedia.org/wiki/Gamma_function

> How about if you define (-3)! as being 0 or -1 (an error result)?

-- glen

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web