Deutsch   English   Français   Italiano  
<udceqr$4c2$1@rasp.pasdenom.info>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups: comp.lang.c
Subject: Re: C vis C++ standard deviation function.
Date: Mon, 07 Mar 2022 14:34:04 +0000
Organization: A noiseless patient Spider
Lines: 95
Message-ID: <87ilsp7rxf.fsf@bsb.me.uk>
References: <b2017949-b97f-4d1e-87c5-5b6ed79ecd10n@googlegroups.com>
	<t028t1$53p$1@dont-email.me> <t0340u$dqa$1@dont-email.me>
	<t034a2$alm$7@dont-email.me> <t039p4$r3k$1@dont-email.me>
	<t04g22$5ge$1@dont-email.me> <t04o5t$902$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="8b562f22a34111b2cf758c7940e74e5a";
	logging-data="18679"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18xEOWhxg3fS8Kn5dQLfyuWr6VNWA5y4J0="
Cancel-Lock: sha1:XR0B8qYg+wCM+pIcSK1BGuGt8kA=
	sha1:kwLXQISsxd1yzd2//Z2mm7aP2XA=
X-BSB-Auth: 1.dc79db6c508bb16b9963.20220307143404GMT.87ilsp7rxf.fsf@bsb.me.uk
Bytes: 4234

Bart <bc@freeuk.com> writes:

> On 07/03/2022 08:34, Bonita Montero wrote:
>>>    #define FUNC(name, T) \
>>>    double name##T(T *x, int N)\
>>>    {\
>>>        int i;\
>>>        double mean = 0;\
>>>        double sumvar2 = 0;\
>>>    \
>>>        for (i =0; i < N; i++)\
>>>          mean += x[i];\
>>>        mean /= N;\
>>>    \
>>>        for (i =0; i < N; i++)\
>>>           sumvar2 += (x[i] - mean)*(x[i]-mean);\
>>>    \
>>>        return sqrt(sumvar2/(N-1));\
>>>    }
>>>
>>>    FUNC(stddev,float)
>>>    FUNC(stddev,double)
>>>    FUNC(stddev,int)
>>>    #define STDDEV(T,a,b) stddev##T(a,b)
>>>
>>> Call using, for example:
>>>
>>>    sd = STDDEV(float, data, N);

> I have very near 100% confidence that my C routine will do what it
> says, and I know the input is an array of numeric values represented
> by T*.  But what the hell is yours up to?
>
> Here's a challenge for you: write a generic C++ stddev routine that
> looks as much like the C as possible, where the algorithm used is
> identical.

That one of those strange Usenet challenges where you insist that the
person with the advantage in the exchange plays with their hands tied
behind their back!  But I'll play:

  double stddev(const auto *x, int N)
  {
     double sum = 0;
     for (int i = 0; i < N; i++)
        sum += x[i];
   
     double mean = sum/N, sum_var_sq = 0;
     for (int i = 0; i < N; i++)
        sum_var_sq += (x[i] - mean) * (x[i] - mean);
   
     return std::sqrt(sum_var_sq / (N-1));
  }

Shorter, clearer and easier to understand than the C, but it does
inherit some of the C code's limitations.

> Don't be tempted to cram as many C++ toys into it as
> possible.

Ah, the politician's urge to spin comes out again!  Many of C++ advanced
features make writing flexible, reliable code easier.  There are
legitimate criticism that be levelled at the complexity of modern C++,
but they are not toys.

In C++ one would use an iterator at the very least (and probably two,
rather than an explicit size) so that the code would work on all sorts
of iterable things.  That's not cramming in a toy.

> So, just like the C, but that works for float*, double* and any int*.

And one could document this with a type constraint.  Again, not a toy.

And one might use pre-written algorithms to take advantage of future
library implementation improvements:

  double stddev(const auto &first, const auto &last)
  {
     double sum = std::reduce(first, last);
     double sum_sq = std::transform_reduce(first, last, first, 0.0);
     double N = std::distance(first, last);
     return std::sqrt((sum_sq - sum*sum/N)/(N-1));
  }

Again, not toys.

> If there is really a need for a version that takes an interator object
> (since there is no need for random-access objects, it can be serial)
> then make a separate version for that.

Of course there is a need for that.  In C++, what there is not a need
for is a separate version that uses a pointer.

-- 
Ben.