Wednesday, March 25, 2020

Digest for comp.lang.c++@googlegroups.com - 16 updates in 5 topics

Bob Langelaan <bobl0456@gmail.com>: Mar 25 02:01PM -0700

Below is some code provided by a text I am using.
 
I am using MS VS 2019 with default settings running in Release mode to build and run this code.
 
The code below as is works as expected. But if I move the statement
ending with the comment "SEE THIS STATEMENT" to before the previous statement,
it calculates time as 0.000000000 minutes. Equally strange, it outputs the
string "fibonacci( 47 ) = " and then there is a long pause while it appears to
be calculating the value. It should have returned from the function and therefore calculated the value before outputting that string ?!?!
 
Thanks in advance :)
 
// Fig. 24.10: fibonacci.cpp
// Fibonacci calculations performed sequentially
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
 
unsigned long long int fibonacci( unsigned int n ); // function prototype
 
// function main begins program execution
int main( void )
{
cout << fixed << setprecision( 6 );
cout << "Sequential calls to fibonacci(46) and fibonacci(47)" << endl;
 
// calculate fibonacci values for numbers 47 (line 17) and 46 (line 27)
cout << "Calculating fibonacci( 47 )" << endl;
time_t startTime1 = time( nullptr );
unsigned long long int result1 = fibonacci( 47 );

 
cout << "fibonacci( 47 ) = " << result1 << endl;
time_t endTime1 = time(nullptr); // SEE THIS STATEMENT
cout << "Calculation time = "
<< (endTime1 - startTime1 ) / 60.0
<< " minutes\n" << endl;
cout << endTime1 << " " << startTime1 << endl;
cout << "Calculating fibonacci( 46 )" << endl;
time_t startTime2 = time( nullptr );
unsigned long long int result2 = fibonacci( 46 );
 
 
cout << "fibonacci( 46 ) = " << result2 << endl;
time_t endTime2 = time(nullptr);
cout << "Calculation time = "
<< (endTime2 - startTime2) / 60.0
<< " minutes\n" << endl;
 
cout << "Total calculation time = "
<< (endTime2 - startTime1) / 60.0 << " minutes" << endl;
} // end main
 
// Recursively calculates fibonacci numbers
unsigned long long int fibonacci( unsigned int n )
{
// base case
if ( 0 == n || 1 == n )
{
return n;
} // end if
else // recursive step
{
return fibonacci( n - 1 ) + fibonacci( n - 2 );
} // end else
} // end function fibonacci
 
 
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
"Öö Tiib" <ootiib@hot.ee>: Mar 25 02:51PM -0700

On Wednesday, 25 March 2020 23:01:36 UTC+2, Bob Langelaan wrote:
 
> The code below as is works as expected. But if I move the statement
> ending with the comment "SEE THIS STATEMENT" to before the previous statement,
> it calculates time as 0.000000000 minutes.
 
The std::time() returns std::time_t that is usually integral number of
seconds since 00:00, Jan 1 1970 UTC, but that is not mandated by C++
standard. It is defined so about extremely similar POSIX time() function
by POSIX standard AFAIK.
Argument of std::time() can be pointer to a std::time_t object to
store the time as well, or a null pointer.
 
So apparently if you get 0 from it the clock of your system is stuck
at 00:00, Jan 1 1970 UTC or MS uses some rather unusual time_t there.
 
 
> string "fibonacci( 47 ) = " and then there is a long pause while it appears to
> be calculating the value. It should have returned from the function and
> therefore calculated the value before outputting that string ?!?!
 
The algorithm seems hyper naive as it does recurse
2 in 47 times. That can take time indeed.
"Öö Tiib" <ootiib@hot.ee>: Mar 25 03:40PM -0700

On Wednesday, 25 March 2020 23:51:58 UTC+2, Öö Tiib wrote:
> > therefore calculated the value before outputting that string ?!?!
 
> The algorithm seems hyper naive as it does recurse
> 2 in 47 times. That can take time indeed.
 
Calculating the value after output seems strange. Perhaps compiler
somehow deduced that fibonacci() does not have side effects and delayed
its call until result1 was used (as optimization)?
Can't reproduce on online gcc 9.2.0:
<http://coliru.stacked-crooked.com/a/e34866820ac2d2a4>
Mike Terry <news.dead.person.stones@darjeeling.plus.com>: Mar 25 10:57PM

On 25/03/2020 22:40, Öö Tiib wrote:
> its call until result1 was used (as optimization)?
> Can't reproduce on online gcc 9.2.0:
> <http://coliru.stacked-crooked.com/a/e34866820ac2d2a4>
 
Yes, in the Release build,the compiler has "optimised" the order of
function calls, and moved the line
time_t endTime1 = time(nullptr);
to before the call to fibonacci().
 
I have the same VStudio version, and can see this is what it is doing.
 
I don't know whether (or under what conditions) this is allowed by the
standards though.
 
Mike.
"Öö Tiib" <ootiib@hot.ee>: Mar 25 04:13PM -0700

On Thursday, 26 March 2020 00:58:00 UTC+2, Mike Terry wrote:
 
> I have the same VStudio version, and can see this is what it is doing.
 
> I don't know whether (or under what conditions) this is allowed by the
> standards though.
 
Things like input-output and access to volatile are side-effects.
Things with side effects may not be reordered.
I can't imagine how Windows can figure time without access of
something volatile or doing input from somewhere so perhaps MS
optimizer has screwed up tracking of what of its API has side
effects.
Mike Terry <news.dead.person.stones@darjeeling.plus.com>: Mar 25 11:20PM

On 25/03/2020 22:57, Mike Terry wrote:
 
> I don't know whether (or under what conditions) this is allowed by the
> standards though.
 
> Mike.
 
I found this from about 5 years back:
 

<https://stackoverflow.com/questions/26190364/is-it-legal-for-a-c-optimizer-to-reorder-calls-to-clock>
 
There's a suggestion that it is legal if the compiler is aware of all
the code executed between the clock calls, and there are no side effects
preventing it. It suggests making the time and result variable volatile
should prevent reordering:
 
volatile time_t startTime1 = time( nullptr );
volatile unsigned long long int result1 = fibonacci( 47 );
volatile time_t endTime1 = time(nullptr);
 
which seems to work on my system.
 
Regards,
Mike.
jameskuyper@stellarscience.com: Mar 25 09:53AM -0700

On Wednesday, March 25, 2020 at 8:09:31 AM UTC-4, cda...@gmail.com wrote:
...
> Well, just a minor point. Spanish in (parts of) Spain is referred to as Castellano whereas Spanish in parts of Latin America is referred to as Spanish.
 
You should be more consistent - you should use either Spanish terms or
English terms to describe those dialects. Mixing them up adds confusion
to an issue which was already confused to start with.
 
"Castellano" is a Spanish term, corresponding roughly to the English
term "Castillian" or "Peninsular Spanish". "Spanish" is an English term
that covers all of the dialects, including those in Spain and in Latin
America. It corresponds roughly to the Spanish word "Español" - except
that I gather that, in Spain, "Castellano" and "Español" are often
considered synonyms (I've seen people in this newsgroup do the same
thing with "British English" and "English"). I'm not sure whether that
means they interpret "Castellano" as including the Latin American
dialects, or that they interpret "Español" as excluding them.
 
As far as I can tell, in neither language is there a single simple term
that describes all of the dialects of Spanish that are spoken in Latin
America. As it says at
<https://en.wikipedia.org/wiki/Spanish_language_in_the_Americas>:
 
"Linguistically, this grouping is somewhat arbitrary, akin to having a
term for "overseas English" encompassing variants spoken in the United
States, Canada, Australia, India, New Zealand and Ireland, but not the
Island of Britain. There is great diversity among the various Latin
American vernaculars, and there are no traits shared by all of them
which are not also in existence in one or more of the variants of
Spanish used in Spain."
 
In English those dialects may be described as "Latin American Spanish".
<https://es.wikipedia.org/wiki/Espa%C3%B1ol_de_Am%C3%A9rica> uses
"español americano".
Christian Gollwitzer <auriocus@gmx.de>: Mar 25 09:14PM +0100

Am 25.03.20 um 09:34 schrieb Frederick Gotham:
 
> I'm writing a paper that I want translated into 6 languages before I publish it:
> German, Russian, Chinese(Mandarin), Spanish, French, Arabic
 
You probably overthink this. Technical papers are usually published
exclusively in English, especially if they ar ein the field of computer
science (and most other fields, too). Notable exceptions are linguistic
papers or papers with national themes like legal problems.
 
So just write it up in English and submit to a fitting journal - you'll
find that publishing it in one English language journal is hard enough.
 
Christian
Vir Campestris <vir.campestris@invalid.invalid>: Mar 25 09:11PM

On 25/03/2020 08:34, Frederick Gotham wrote:
> I'm writing a paper that I want translated into 6 languages before I publish it:
> German, Russian, Chinese(Mandarin), Spanish, French, Arabic
 
I've never known a software engineer who can't read English with a
reasonable level of proficiency.
 
Writing it is another matter - I've seen some really odd "Chinglish",
and even my Danish colleagues produce some odd phrases occasionally.
 
Andy
James Kuyper <jameskuyper@alumni.caltech.edu>: Mar 25 02:32PM -0700

On Wednesday, March 25, 2020 at 8:09:31 AM UTC-4, cda...@gmail.com wrote:
...
> Well, just a minor point. Spanish in (parts of) Spain is referred to
as Castellano whereas Spanish in parts of Latin America is referred to
as Spanish.
 
You should be more consistent - you should use either Spanish terms or
English terms to describe those dialects. Mixing them up adds confusion
to an issue which was already confused to start with.
 
"Castellano" is a Spanish term, corresponding roughly to the English
term "Castillian" or "Peninsular Spanish". "Spanish" is an English term
that covers all of the dialects, including those in Spain and in Latin
America. It corresponds roughly to the Spanish word "Español" - except
that I gather that, in Spain, "Castellano" and "Español" are often
considered synonyms (I've seen people in this newsgroup do the same
thing with "British English" and "English"). I'm not sure whether that
means they interpret "Castellano" as including the Latin American
dialects, or that they interpret "Español" as excluding them.
 
As far as I can tell, in neither language is there a single simple term
that describes all of the dialects of Spanish that are spoken in Latin
America. As it says at
<https://en.wikipedia.org/wiki/Spanish_language_in_the_Americas>:
 
"Linguistically, this grouping is somewhat arbitrary, akin to having a
term for "overseas English" encompassing variants spoken in the United
States, Canada, Australia, India, New Zealand and Ireland, but not the
Island of Britain. There is great diversity among the various Latin
American vernaculars, and there are no traits shared by all of them
which are not also in existence in one or more of the variants of
Spanish used in Spain."
 
In English those dialects may be described as "Latin American Spanish".
<https://es.wikipedia.org/wiki/Espa%C3%B1ol_de_Am%C3%A9rica> uses
"español americano".
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Mar 25 08:38PM

On Sun, 22 Mar 2020 17:11:44 -0700 (PDT)
> such language because of how destructive to communities it is.
 
> > I don't get it
 
> I know.
 
He also isn't quite as secure in himself as he likes to pretend. His
"But nobody can offend me without my permission, first. And I do not
give anyone permission to offend me" is nothing more than a
projection. Bullies don't like it when people hit back.
 
For example, he asserted a year or two ago, when picking on a newcomer's
struggles with condition variables, that "It took me only a few minutes
to figure it out, the first time I encountered this topic, a long long
time ago, in a galaxy far, far away. But not everyone is as smart as
me... I think we're looking at a future Microsoft Windows developer,
here".
 
When told he was a dickhead he didn't like it at all. His reaction
to having the facts pointed out to him was quite satisfying.
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Mar 25 12:00PM -0700

David Brown <david.brown@hesbynett.no> writes:
[...]
> keys", "US, with euro on 5". I'm guessing that for some of these,
> alt-gr would give access to more symbols. Then there are a range of
> other options you can set if you want.
[...]
 
I just now noticed that my keyboard has a Euro symbol on the 5 key,
to the right of the 5. (The key above R and T, not the one on the
numeric keypad). In a few minutes of searching, I haven't found
a way to get it to send an actual Euro symbol.
 
€ (don't ask what I had to do to get that symbol).
 
The point, I suppose, is that support for non-ASCII characters on
US keyboards is so poor that any change to C++ that requires entry
of such characters is going to be a non-starter for the foreseeable
future. I can *display* a lot of Unicode characters with no problem
(which wasn't always the case), but entering them is a lot harder.
 
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
"Öö Tiib" <ootiib@hot.ee>: Mar 25 05:38AM -0700

On Wednesday, 25 March 2020 13:16:58 UTC+2, Frederick Gotham wrote:
> > or more for compilers to fix their defects and for teams to prepare
> > to migrate.
 
> You know those guys who still program in K&R C and refuse to move on to C89?
 
No but if your C code has to be portable to MSVC compilers then you
are forced by that fact alone to use something between C89 and C90.
 
> Well 20 years from now I'm gonna be the guy stuck in C++11. They're just adding too much stuff.
 
Nah. If people their thoughts at least semi-coherently expressed in
some human language (like English) then C++20 should not be too hard
for them either.
I'm just advocating not using it for production code at least two
years because initially compilers and standard libraries contain
defects, non-standard libraries and development tools are
not up to date for it and all developers are also not yet trained
well enough to use it.
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Mar 25 01:16PM

On Mon, 23 Mar 2020 10:13:09 GMT
Melzzzzz <Melzzzzz@zzzzz.com> wrote:
[snip]
> Anyway only char[] is safe for placement new...
 
I suspect that as, regards std::aligned_union, this might be another
technical defect in the C++ standard. The explicit purpose of
std::aligned_union is to provide a member typedef 'type' which "shall
be a POD type suitable for use as uninitialized storage for any object
whose type is listed in Types" (§23.15.7.6 of C++17, table 50)
 
C++17 has inherited std::aligned_union from C++11, but §4.5/3 of C++17
seems to have forgotten about it - even more curiously, the example in
§4.5/3 provides a user-version of an AlignedUnion type rather than
employ std::aligned_union.
 
One possible interpretation of C++17 is that it now requires
std::aligned_union::type to be a typedef to a struct having as it only
member an array of unsigned char or of std::byte. In that case, the
OP's code does not work because in C++17 it should use std::launder to
access the objects created in the underlying storage (or store the
return value of placement new, which is not feasible in the OP's case).
James Kuyper <jameskuyper@alumni.caltech.edu>: Mar 25 10:14AM -0400

On 3/25/20 8:38 AM, Öö Tiib wrote:
...
> No but if your C code has to be portable to MSVC compilers then you
> are forced by that fact alone to use something between C89 and C90.
I think you mean C90 and C99.
 
C89 is the original ANSI standard for C. C90 is the first ISO standard
for C. ISO's standards for how ISO standards must be written required
that three sections be added - those are the first three sections of the
current standard. Except for those three sections, C89 and C90 are
essentially identical.
"between C89 and C90" would therefore mean that it's missing some part
of the first three sections of C90. As far as I can see, that wouldn't
have any effect that matters as far the compiler is concerned.
"Öö Tiib" <ootiib@hot.ee>: Mar 25 08:07AM -0700

On Wednesday, 25 March 2020 16:14:21 UTC+2, James Kuyper wrote:
> > No but if your C code has to be portable to MSVC compilers then you
> > are forced by that fact alone to use something between C89 and C90.
> I think you mean C90 and C99.
 
Yes, thanks for correcting, that was what I meant but somehow didn't type.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: