Discussion:
What does this code do ?
(too old to reply)
Bonita Montero
2024-05-27 12:37:21 UTC
Permalink
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main()
{
vector<double> row;
double k = 1, f = 1, fRev = 1;
do
row.emplace_back( fRev ),
f *= k++,
fRev = 1 / f;
while( fRev );
auto acc = [&]( size_t n ) { return accumulate( make_reverse_iterator(
row.begin() + n ), row.rend(), 0.0 ); };
size_t n = row.size();
for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e; --n, e =
eNext );
cout << n << endl;
}
Sam
2024-05-27 23:27:29 UTC
Permalink
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<double> row;
double k = 1, f = 1, fRev = 1;
do
row.emplace_back( fRev ),
f *= k++,
fRev = 1 / f;
while( fRev );
auto acc = [&]( size_t n ) { return
accumulate( make_reverse_iterator( row.begin() + n ), row.rend(), 0.0 ); };
size_t n = row.size();
for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e; --n, e
= eNext );
cout << n << endl;
}
This code is an example of an average-ranked entry in the obfuscated C++
contest.
Bonita Montero
2024-05-28 04:49:23 UTC
Permalink
Post by Sam
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
    vector<double> row;
    double k = 1, f = 1, fRev = 1;
    do
        row.emplace_back( fRev ),
        f *= k++,
        fRev = 1 / f;
    while( fRev );
    auto acc = [&]( size_t n ) { return accumulate(
make_reverse_iterator( row.begin() + n ), row.rend(), 0.0 ); };
    size_t n = row.size();
    for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e; --n,
e = eNext );
    cout << n << endl;
}
This code is an example of an average-ranked entry in the obfuscated C++
contest.
There's nothing obfuscated here.
wij
2024-05-28 05:34:25 UTC
Permalink
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<double> row;
double k = 1, f = 1, fRev = 1;
do
row.emplace_back( fRev ),
f *= k++,
fRev = 1 / f;
while( fRev );
auto acc = [&]( size_t n ) { return accumulate( make_reverse_iterator(
row.begin() + n ), row.rend(), 0.0 ); };
size_t n = row.size();
for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e; --n, e =
eNext );
cout << n << endl;
}
There might be several issues in the program.

Don't know what it does but felt the general problem of C++: Overly expressive.
Expressive to improve quality? reading/error detect?
David Brown
2024-05-28 06:57:50 UTC
Permalink
Post by wij
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<double> row;
double k = 1, f = 1, fRev = 1;
do
row.emplace_back( fRev ),
f *= k++,
fRev = 1 / f;
while( fRev );
auto acc = [&]( size_t n ) { return accumulate( make_reverse_iterator(
row.begin() + n ), row.rend(), 0.0 ); };
size_t n = row.size();
for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e; --n, e =
eNext );
cout << n << endl;
}
There might be several issues in the program.
Don't know what it does but felt the general problem of C++: Overly expressive.
Expressive to improve quality? reading/error detect?
To be clear - it is indicative of a problem with some programmers, not
the language. Bonita would write smart-arse code in any language. C++
just gives such programmers more ways to write bad code than some other
languages.
Bonita Montero
2024-05-28 07:03:57 UTC
Permalink
Post by David Brown
Post by wij
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
    vector<double> row;
    double k = 1, f = 1, fRev = 1;
    do
        row.emplace_back( fRev ),
        f *= k++,
        fRev = 1 / f;
    while( fRev );
    auto acc = [&]( size_t n ) { return accumulate(
make_reverse_iterator(
row.begin() + n ), row.rend(), 0.0 ); };
    size_t n = row.size();
    for( double e = acc( n ), eNext; (eNext = acc( n - 1 )) == e;
--n, e =
eNext );
    cout << n << endl;
}
There might be several issues in the program.
Don't know what it does but felt the general problem of C++: Overly expressive.
Expressive to improve quality? reading/error detect?
To be clear - it is indicative of a problem with some programmers, not
the language.  Bonita would write smart-arse code in any language.  C++
just gives such programmers more ways to write bad code than some other
languages.
21 lines should be readable anyway, no matter which style.
Vir Campestris
2024-05-30 16:09:14 UTC
Permalink
Post by Bonita Montero
21 lines should be readable anyway, no matter which style.
Go find an APL programme, then tell me you still believe that.

Andy
Scott Lurndal
2024-05-30 17:09:18 UTC
Permalink
Post by Vir Campestris
Post by Bonita Montero
21 lines should be readable anyway, no matter which style.
Go find an APL programme, then tell me you still believe that.
Or pretty much any perl program. Write-once/read-never language.
James Kuyper
2024-05-31 03:01:40 UTC
Permalink
Post by Scott Lurndal
Post by Vir Campestris
Post by Bonita Montero
21 lines should be readable anyway, no matter which style.
Go find an APL programme, then tell me you still believe that.
APL was my third programming language, after Fortran II and BASIC. I
learned it and wrote several programs in it around 1977. About thirty
years later, after having done almost no programming in APL, I came
across a printout of my code that I had saved. I found it quite clear
and easy to read. I couldn't write new code without consulting a
reference manual, but I could read my old code.
Post by Scott Lurndal
Or pretty much any perl program. Write-once/read-never language.
I've never understood what gave perl that reputation. I worked nearly 30
years on a project that required me to occasionally write perl code and
to read perl code written by others. I didn't find it any harder to
understand than the C code we were writing. There's fancy tricks that
you can do with perl that can be very hard to follow, but for precisely
that reason, we avoided writing such code.
David Brown
2024-05-31 07:17:31 UTC
Permalink
Post by James Kuyper
Post by Scott Lurndal
Post by Vir Campestris
Post by Bonita Montero
21 lines should be readable anyway, no matter which style.
Go find an APL programme, then tell me you still believe that.
APL was my third programming language, after Fortran II and BASIC. I
learned it and wrote several programs in it around 1977. About thirty
years later, after having done almost no programming in APL, I came
across a printout of my code that I had saved. I found it quite clear
and easy to read. I couldn't write new code without consulting a
reference manual, but I could read my old code.
Post by Scott Lurndal
Or pretty much any perl program. Write-once/read-never language.
I've never understood what gave perl that reputation. I worked nearly 30
years on a project that required me to occasionally write perl code and
to read perl code written by others. I didn't find it any harder to
understand than the C code we were writing. There's fancy tricks that
you can do with perl that can be very hard to follow, but for precisely
that reason, we avoided writing such code.
I think its reputation is that Perl was popular for small scripts, and
in such scripts people often have little concern for future maintenance,
code re-use, and the like. They use these "fancy tricks" and write
"write-only" code. If you later need to write a similar script, it's
usually just as fast to write it from scratch - but that's okay for
short scripts.

I haven't done any Perl programming, but I've modified a couple of Perl
programs, and had no problem with that.

These days, in most new code that at one time would have been written in
Perl, is now written in Python. It's less tempting to write
incomprehensible Python than it is to write incomprehensible Perl - it
doesn't have the same kind of tricks. But I've seen (and written)
plenty of small bits of Python that take extra thought to understand
afterwards. Not everything needs to be readable or maintainable -
sometimes it is sufficient if it just works there and then.
Vir Campestris
2024-06-01 19:47:32 UTC
Permalink
Post by James Kuyper
APL was my third programming language, after Fortran II and BASIC. I
learned it and wrote several programs in it around 1977. About thirty
years later, after having done almost no programming in APL, I came
across a printout of my code that I had saved. I found it quite clear
and easy to read. I couldn't write new code without consulting a
reference manual, but I could read my old code.
My introduction to APL was a programme published in a scientific paper.
This was before email and the internet, so I had to type it in.

I never got all the typos out. No doubt some of them were in the
published paper. That probably didn't help my degree, but TBH I can't
really say I suffered - my first employer had a policy of looking for
people who had messed up their degree by playing with the computer too much.

But I never want to see APL ever again. I'm not sure I have!

Andy

Bonita Montero
2024-05-28 08:03:24 UTC
Permalink
I think this is somewhat more readable:

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main()
{
vector<double> row;
double k = 1, f = 1, fRev = 1;
do
row.emplace_back( fRev ),
f *= k++,
fRev = 1 / f;
while( fRev );
auto acc = [&]( auto it ) { return accumulate( it, row.rend(), 0.0 ); };
auto last = row.rbegin();
for( double e = acc( last ), eNext; (eNext = acc( last + 1 )) == e;
++last, e = eNext );
cout << last.base() - row.begin() << endl;
}

The code has sth. to do with Euler's number.
Bonita Montero
2024-06-01 16:30:12 UTC
Permalink
Post by Bonita Montero
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
    vector<double> row;
    double k = 1, f = 1, fRev = 1;
    do
        row.emplace_back( fRev ),
        f *= k++,
        fRev = 1 / f;
    while( fRev );
    auto acc = [&]( auto it ) { return accumulate( it, row.rend(), 0.0
); };
    auto last = row.rbegin();
    for( double e = acc( last ), eNext; (eNext = acc( last + 1 )) == e;
++last, e = eNext );
    cout << last.base() - row.begin() << endl;
}
The code has sth. to do with Euler's number.
This code prints the minimum elements to calculate Euler's number with
a double, i.e. beyond that number of elements the number doesn't change.
I sum up from the lowest to the highest elements to have maximum pre-
cision.
Loading...