Tuesday, February 28, 2017

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

Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Feb 27 09:44PM -0700


>using namespace std;
 
>// list external variables
>extern lapack_int n;
 
This variable 'n' has no connection with the variable 'n' defined in
the previous program.
 
>extern string outfilename;
 
Likewise for 'outfilename'.
 
> while (fin >> w){
> // create a variable which will become the largest eigenvalue
> float wLargest = w;
 
You say you want to set wLargest to the first value read, but you're
actually setting it for *every* value you read.
 
There are at least a couple of ways to fix this, and they all involve
declaring wLargest somewhere other than inside the loop. Suppose, for
example, you were computing the sum of the values you were reading.
Would you declared the sum variable inside the loop, or would you put
it somewhere else?
 
 
>For some reason, outfilename is not recognized even though I specified it as an extern.
 
>Second, the while loop isn't actually doing what I want. I want to set the wLargest initially to the first value that is read from the textfile. I only want to do this once. Next I will compare wLargest, and if the w read from the file is larger than wLargest, wLargest will be replaced by that value. Then, a new w is read. But the problem with the above is that when a new w is read, it replaces the w that wLargest was equal to from the previous iteration and I don't want that. But I don't know how to fix it.
 
>Any help would be much appreciated.
 
I remember learning to program. It was fun.
 
Louis
Manfred <mx2927@gmail.com>: Feb 28 11:29PM +0100

On 02/28/2017 05:44 AM, Louis Krupp wrote:
> actually setting it for *every* value you read.
 
> There are at least a couple of ways to fix this, and they all involve
> declaring wLargest somewhere other than inside the loop.
Well, there's at least one way that can work with wLargest declared as
it is, but with a different storage duration (storage class specifier).
This would make use of the fact that this function (main) will be
executed exactly once.
Robert Wessel <robertwessel2@yahoo.com>: Feb 24 01:19AM -0600

On 24 Feb 2017 02:58:43 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:
 
> 1.4 seconds. And this was only in Firefox, Chrome might
> even be faster. Amazing what they can get out of
> interpreters today!
 
 
The better Javascript processors are JITs these days.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 24 05:26AM +0100

On 24.02.2017 01:33, Stefan Ram wrote:
> R = ::std::move( x );
> x = ::std::move( r );
> r = ::std::move( a ); }}
 
Oh. :)
 
I confirmed, by writing my own little program for that, that you get a
correct result. Although it is for index 999.999 in the sequence, when
one considers fib(0) = 1, fib(1) = 1, fib(2) = 2, fib(3) = 3 and so on.
 
I'm not sure how your code works, it seems to do strange things, but
here's the expressive C++ dialect code that I cooked up to test it and
show how I would have written that program:
 
 
[code]
#include <p/expressive/using_weakly_all.hpp>
 
#include <algorithm>
#include <iostream>
#include <math.h> // log
#include <vector>
using namespace std;
 
using Big_int = std::vector<int>; // Each item is a decimal digit.
 
$procedure add_to( ref_<Big_int> a, ref_<const Big_int> b )
{
$let size_a = n_items_in( a ); $let size_b = n_items_in( b );
$var carry = 0;
for( $each i $in i_up_to( max( size_a, size_b ) ) )
{
if( i >= size_a ) { a.resize( i + 1 ); }
a[i] += carry + ($when( i < size_b ) $use( b[i] ) $default_to(
0 ));
carry = a[i]/10;
a[i] -= 10*carry;
}
if( carry != 0 ) { a.push_back( carry ); }
}
 
$just
{
$let golden_ratio = 1.6180339887498948482045868343656;
$let n = 100'000;
$let n_digits = static_cast<int>( (n + 10)*log10( golden_ratio ) + 1 );
clog << n_digits << " digits expected.\n";
 
$var a = Big_int{1}; a.reserve( n_digits );
$var b = Big_int{0}; b.reserve( n_digits );
for( $n_times( n ) )
{
$var next = a;
add_to( next, b );
a = move( b ); b = move( next );
}
for( $each digit $in reverse_span_of( b ) ) { cout << digit; }
cout << endl;
}
[/code]
 
I did not take the trouble to split the addition loop into parts where
the processing could be specialized for speed; I just used `-O3`. ;-)
 
This might be the reason that this code is shorter, 43 versus 91 lines,
even though my code looks much more verbose and whitespacey-ish.
 
 
Cheers!,
 
- Alf
 
PS: One problem with the $n_times macro is that g++ produces a very much
undesired warning "-Wno-unused-but-set-variable". I just turn it off but
I would like to suppress it for this situation in the code. I tried with
`_Pragma` expressions in the macro but g++ would have none of that
inside a `for` loop head. The macro is currently defined as
`$e::Dummy_reference_consumer $unique_temp_name $in $e::i_up_to( n )`. I
can't think of a way to do that without a range based loop.
Daniel <danielaparker@gmail.com>: Feb 28 11:30AM -0800

On Tuesday, February 28, 2017 at 5:58:52 AM UTC-5, Alf P. Steinbach wrote:
 
> Ben answered: "Of what practical use is a newborn baby?""
 
Hmmm ... it seems to me that that quote would be more apropos if you were
inventing a new programming language, or such. Of course, anyone willing to
work for free can do anything that they like. But did you have to work on this!
 
:-) Be well,
Daniel
Juha Nieminen <nospam@thanks.invalid>: Feb 28 02:26PM

Consider something like this:
 
template<typename> int foo() { return 1; }
template<> int foo<unsigned>() { return 2; }
 
If this is included in more than one compilation unit, does the latter
function need to be explicitly declared 'inline'?
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 28 04:47PM +0100

On 2/28/2017 3:26 PM, Juha Nieminen wrote:
> template<> int foo<unsigned>() { return 2; }
 
> If this is included in more than one compilation unit, does the latter
> function need to be explicitly declared 'inline'?
 
Yes.
 
But you should be able to use that complete specialization just like an
ordinary function, with definition in one translation unit,
 
template<> int foo<unsigned>() { return 2; }
 
and just declaration in other translation units,
 
template<> int foo<unsigned>();
 
Disclaimer: I haven't tested that, and the ways of Bjarne are sometimes
mysterious.
 
 
Cheers!,
 
- Alf
Paavo Helde <myfirstname@osa.pri.ee>: Feb 28 08:25PM +0200

On 28.02.2017 17:47, Alf P. Steinbach wrote:
 
> template<> int foo<unsigned>() { return 2; }
 
> and just declaration in other translation units,
 
> template<> int foo<unsigned>();
 
In that case you need an explicit instantiation in the translation unit
containing the definition:
 
14/6: "A function template, member function of a class template,
variable template, or static data member of a class template shall be
defined in every translation unit in which it is implicitly instantiated
(14.7.1) unless the corresponding specialization is explicitly
instantiated (14.7.2) in some translation unit; no diagnostic is required."
 
It appears though that most implementations do not require the explicit
instantiation and appear to work fine without it (and with "no
diagnostic produced"). See the recent thread "does the language
guarantee `explicit specialization' will generate the code ?".
woodbrian77@gmail.com: Feb 28 08:46AM -0800

On Thursday, February 16, 2017 at 11:24:37 PM UTC-6, Rick C. Hodgin wrote:
> continue to be impressed with each new release. (Note: I have no
> affiliation with Atlassian, except that I used Stash and Bitbucket).
 
> https://www.atlassian.com/software/bitbucket
 
Thanks. Looks interesting. In retrospect, I wish
someone would have suggested Atlassian or Github to
me years ago. I'm enjoying using them.
 
Until recently I had one directory where I built both
a library and two executables (tiers). Now I've added
a new directory called "tiers" and moved the code for
the tiers there. I think this will help people who
are interested in learning to use my software.
 
The downside to this is I'm specifying CXX and CXXFLAGS
(and maybe a few other things like that) in multiple
makefiles. I figured out a way where I could pass those
from the higher level makefile to the lower level --
something like:
 
$(MAKE) -C tiers CXX=$(CXX) CXXFLAGS=$(CXXFLAGS)
 
. But then I realized that would only work from the
top level directory. If I ran make from the tiers
subdirectory, it wouldn't work. So I did the obligatory
search for alternatives to make. Then I got a little
discouraged, and decided to stick with make.
 
 
This is my top level makefile
https://github.com/Ebenezer-group/onwards/blob/master/makefile
 
And this is a lower level makefile
https://github.com/Ebenezer-group/onwards/blob/master/tiers/makefile
 
Thanks in advance for suggestions on how to improve those files.
 
 
Brian
Ebenezer Enterprises - So far G-d has helped us.
http://webEbenezer.net
scott@slp53.sl.home (Scott Lurndal): Feb 28 05:11PM

>from the higher level makefile to the lower level --
>something like:
 
>$(MAKE) -C tiers CXX=$(CXX) CXXFLAGS=$(CXXFLAGS)
 
A common solution for this is to encapsulate your
definitions in a file at the top level called
Makefile.defs.
 
Encapsulate your rules in a top-level file called Makefile.rules.
e.g:
 
Makefile.defs:
--------------
CINCLUDES = -I$(TOP)/include
CINCLUDES += -I$(TOP)
 
DEBUGFLAGS = -g
 
GXXFLAGS = -mno-red-zone
GXXFLAGS += -fno-strict-aliasing
GXXFLAGS += -fno-stack-protector
GXXFLAGS += -fno-exceptions
GXXFLAGS += -Wall
GXXFLAGS += -mtune=native
GXXFLAGS += -O2
 
CXXFLAGS = $(DEBUGFLAGS) $(CINCLUDES) $(strip $(GXXFLAGS))
 
LDFLAGS += -Wl,-E
SOLDFLAGS += -Wl,-E -shared -lpthread -nostartfiles -nodefaultlibs -nostdlib
LIB = $(TOP)/lib
 
LIBCOMMON = $(LIB)/libcommon.a
LIBPROCESSOR = $(LIB)/libprocessor.a
LIBMP = $(LIB)/libmp.a
LIBDIRECTIVE = $(LIB)/libdirective.a
LIBOP = $(LIB)/libop.a
SIM_LIBS = $(LIBMP)
SIM_LIBS += $(LIBPROCESSOR)
SIM_LIBS += $(LIBCOMMON)
SIM_LIBS += $(LIBFETCH)
 
Makefile.rules:
---------------
#
# Common Makefile Rules
#
%.o: %.c
@echo " COMPILE $<"
$(HUSHCOMPILE)$(CC) $(CFLAGS) -MMD -o $@ -c $<
 
%.o: %.cpp
@echo " COMPILE $<"
$(HUSHCOMPILE)$(COMPILE.cpp) -MMD -o $@ $<
 
%.s: %.cpp
@echo " COMPILE-TO-ASM $<"
$(HUSHCOMPILE)$(COMPILE.cpp) -MMD -S $<
 
%.so: %.o
@echo " BUILDSO $@"
$(HUSHCOMPILE)$(LD) -o $@ $(SOLDFLAGS)
 
%.icm5: %.sprasm
@echo " SPRASM $<"
$(HUSHCOMPILE)$(SPRASM) $(SPRASM_FLAGS) -o $@ $<
 
buildsubs:: $(DIRS) subdirs
 
clean:: subdirs
.PHONY: subdirs clean $(SUBDIRS)
clobber:: clean subdirs
 
subdirs: $(SUBDIRS)
 
$(SUBDIRS):
@ $(MAKE) -C $@ $(MAKECMDGOALS) $(MAKEARGS)
 
clean::
@-$(RM) -f $(CLEAN_FILES) $(OBJECTS)
@ if [ -n "$(CLEAN_DIRS)" ]; then \
$(RM) -rf $(CLEAN_DIRS); \
fi
 
Then the top-level makefile:
 
Makefile:
---------
TOP=.
include $(TOP)/Makefile.defs
 
TARGET = vsim
 
SUBDIRS += common
SUBDIRS += io/dlps
SUBDIRS += processor
SUBDIRS += mp
SUBDIRS += utilities
 
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
 
HOST_LIBS = -lreadline -lcurses -lpthread -lgmp -ldl
 
DIRS = lib
 
CLOBBER_FILES += $(TARGET) $(TARGET).csc $(TARGET).csc.in $(TARGET).csc.po
CLOBBER_FILES += include/built.h
CLOBBER_DIRS += $(DIRS)
 
all: $(TARGET)
 
main.cpp: vsim_built buildsubs
 
utilities: common
 
$(SIM_LIBS): $(SUBDIRS)
 
$(TARGET):: $(OBJECTS) $(SUBDIRS) $(SIM_LIBS)
$(CXX) $(DEBUGFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) $(SIM_LIBS) $(HOST_LIBS)
$(OBJCOPY) --only-keep-debug $@ $@.debuginfo
$(OBJCOPY) --strip-debug $@
$(OBJCOPY) --add-gnu-debuglink=$@.debuginfo $@
 
include $(TOP)/Makefile.rules
-include *.d
 
common/Makefile:
----------------
TOP=..
include $(TOP)/Makefile.defs
 
SOURCES = command_table.cpp
SOURCES += contention_protocol.cpp
SOURCES += dcp.cpp
SOURCES += dcp_header.cpp
SOURCES += disassembler.cpp
SOURCES += ebcdic.cpp
SOURCES += file_logger.cpp
SOURCES += mcs.cpp
SOURCES += mux_logger.cpp
SOURCES += netport.cpp
SOURCES += osdep.cpp
SOURCES += pollselect.cpp
SOURCES += pool.cpp
SOURCES += protocol.cpp
SOURCES += serialport.cpp
SOURCES += station.cpp
SOURCES += syslog_logger.cpp
SOURCES += thread.cpp
SOURCES += timer.cpp
SOURCES += timer_manager.cpp
 
CXXFLAGS += -fpic
 
OBJECTS = $(SOURCES:.cpp=.o)
 
all: $(LIBCOMMON)
 
 
$(LIBCOMMON): $(OBJECTS)
ar cr $@ $+
 
include $(TOP)/Makefile.rules
 
---------
 
This will automatically descend into any directory
listed in SUBDIRS at each level and
build the Makefile in that subdirectory using the
common definitions in $(TOP)/Makefile.defs and $(TOP)/Makefile.rules
 
Objects from each subdirectory are archived in a archive
library specific to each subdirectory (since when using
parallel make, the 'ar' command isn't "parallel make" safe.
ram@zedat.fu-berlin.de (Stefan Ram): Feb 24 01:09AM

>>Java/C# have quite same roots but Java/Javascript? Nonsense connection.
>Using aggressive wording like »Nonsense« will not help
>to cover your lack of expertise.
 
Now I made the error that I dislike so much when I read it!
 
An article (determiner) is missing in front of »aggressive
wording«, but actually I intended to write the plural
»aggressive wordings«, and the »s« somehow disappeared.
ram@zedat.fu-berlin.de (Stefan Ram): Feb 24 02:58AM

>I did not really try to find the bug in your code, but I
>wrote a C++ program to print the 100'000th Fibonacci number.
 
I wrote a similar program in JavaScript:
 
Turns out: Here the JavaScript program is not much
slower than the compiled C++ program. The C++ program
takes about 1 second here and the JavaScript program about
1.4 seconds. And this was only in Firefox, Chrome might
even be faster. Amazing what they can get out of
interpreters today!
 
{ const alpha = new Date().getTime();
const fib =
( () =>
{ "use strict";
const e = 14;
const b = eval( "1e" + e );
const f = Array( e ).join( "0" );
let x = new Float64Array( 3e4 ); x[ 0 ] = 1.0; let xl = 1;
let r = new Float64Array( 3e4 ); r[ 0 ] = 1.0; let rl = 1;
let R = new Float64Array( 3e4 ); let Rl = 0;
let j = new Array( 3e4 );
let s = 0;
let a; let al;
let o = 0;
let p = 0;
let g; let G;
let h; let H;
const fibonacci =( n )=>
{ for( let i = 1; i < n - 1; ++i )
{ if( rl < xl ){ g = r; G = rl; h = x; H = xl; }
else { g = x; G = xl; h = r; H = rl; }
p = 0; o = 0;
while(p<G){s=o+g[p]+h[p];R[p++]=(s-(o=+(s>b))*b);}
while(p<H){s=o+h[p];R[p++]=(s-(o=+(s>b))*b);}
if(o!=0){s=o;R[p++]=(s-(o=+(s>b))*b);}
Rl = p;
a = R; al = Rl; R = x; Rl = xl; x = r; xl = rl; r = a; rl = al; }
let i = 0;
for( ; i < al - 1; ++i )j[ i ]=( f +( a[ i ] )).slice( -e );
j[ i ]= a[ i ];
return j.slice( 0, al ); }
return fibonacci( 100000 ).reverse().join( "" ); } )()
const omega = new Date().getTime();
console.log( fib );
console.log( omega - alpha ); }
ram@zedat.fu-berlin.de (Stefan Ram): Feb 24 03:08AM

>Turns out: Here the JavaScript program is not much
>slower than the compiled C++ program. The C++ program
 
When I use »double« for a bignum digit, I can store
14 decimal digits in one bignum digit with C++, and
then the program is about 10 % faster.
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: