www.digitalmars.com         C & C++   DMDScript  

D - Default initialiser for reals; and other stuff

reply p9e883002 sneakemail.com writes:
Hi all,

Just returning to try again at getting to grips with D after a 2 year absence,
and 
getting a little frustrated with some of the changes.

The default initialiser for integral types is (logically) zero. So if I used an 
'unitialised' integral as an accumulator:

	int n;

	foreach( i; 1 .. 10 ) {
	    n += i;
	}

	writefln( n ) // Gives 55

But do the same thing with a real:

	real n;

	foreach( i; 1 .. 10 ) {
	    n += i;
	}

	writefln( n ) // Gives the oh so useful 'nan'

Why? What is the logic behind a default initialiser that serves no purpose. Eg.
if I 
have to set it (initalise it), the default 'initialiser' is pointless.

Other stuff: Why is it so hard to get a random number?

Answer: Because although Mr.  Alexandrescu is obviously a much smarter guy 
than I'll ever be, it really isn't necessary for that to be maniifest in teh 
documentation of every module he writes.

Example: "Generates uniformly-distributed numbers within a range using an 
external generator. The boundaries parameter controls the shape of the interval 
(open vs. closed on either side). Valid values for boundaries are "[]", "(]",
"[)", and 
"()". The default interval is [a, b)."

If by "The boundaries parameter controls the shape of the interval (open vs. 
closed on either side). " he means that little string with ascii art in it
controls 
whether the lower and upper bounds are inclusive or not, couldn't he just say 
that.

And if not, then could he please explain what it does mean? Preferably without 
couching it it in number theory.

And while I at it. Why break the Principle of Least Surprise and invert the 
conventional behaviour of PRNGs everwhere vis-a-vis seeding?

That is, every other PRNG I've used (in a dozen and more languages), self-seeds 
to some reasonable value so that just calling the PRNG produces a different 
sequence each time. You ask for random numbers and get them. 

If you want a repeatable sequence, you seed with a chosen fixed value.

Why invert that behaviour? 

Finally, the source of much of my frustration, has been trying to get your web 
interface to post a message here (I'm assuming this will get through via email).

I select the appropriate link; get greated by "Welcome to Web-News"; hit 
[Compose], fill in (*all*) the blanks appropriately and spend an hour typing in
my 
question. Hit [post] and get...

    Fail to post the article

No explanation for why. No suggestions as to what to correct. Nuttin.

Grrr. 

Buk

I really do love D--mostly :)
Apr 27 2008
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 27 Apr 2008 13:12:06 +0200, <p9e883002 sneakemail.com> wrote:
 Hi all,

Greetings. First of all, I'd like to point you in the direction of the correct newsgroup. The D newsgroup is deprecated, use digitalmars.D instead.
 What is the logic behind a default initialiser that serves no purpose.  
 Eg. if I
 have to set it (initalise it), the default 'initialiser' is pointless.

It is this way so that uninitialized floats will propagate through your code and things will fail fast. Essentially, "Why is my function returning NaN? Oh, it must be an uninitialized float." instead of "Why is the result of my function 1 less than it should? Oh, I forgot setting that float to 1, so it's 0 instead.". Like it or not, I think it's a good way to make sure things work as they should.
 Other stuff: Why is it so hard to get a random number?

Yeah, I've been thinking the same. There should be a simple way to just get a random number, as well as complex ways to get more specific sequences.
 Finally, the source of much of my frustration, has been trying to get  
 your web
 interface to post a message here (I'm assuming this will get through via  
 email).

 I select the appropriate link; get greated by "Welcome to Web-News"; hit
 [Compose], fill in (*all*) the blanks appropriately and spend an hour  
 typing in my
 question. Hit [post] and get...

     Fail to post the article

 No explanation for why. No suggestions as to what to correct. Nuttin.

 Grrr.

I blame goblins.
 Buk

 I really do love D--mostly :)

Good to hear. -- Simen
Apr 27 2008