Re: A floating-point puzzle

2009-08-06 Thread Don

Jarrett Billingsley wrote:

On Wed, Aug 5, 2009 at 10:16 PM, Donnos...@nospam.com wrote:

Lars T. Kyllingstad wrote:

Lars T. Kyllingstad wrote:

Here's a puzzle for you floating-point wizards out there. I have to
translate the following snippet of FORTRAN code to D:

 REAL B,Q,T
C --
C |*** COMPUTE MACHINE BASE ***|
C --
 T = 1.
10T = T + T
 IF ( (1.+T)-T .EQ. 1. ) GOTO 10
 B = 0.
20B = B + 1
 IF ( T+B .EQ. T ) GOTO 20
 IF ( T+2.*B .GT. T+B ) GOTO 30
 B = B + B
30Q = ALOG(B)
 Q = .5/Q

Of course I could just do a direct translation, but I have a hunch that
T, B, and Q can be expressed in terms of real.epsilon, real.min and so
forth. I have no idea how, though. Any ideas?

(I am especially puzzled by the line after l.20. How can this test ever
be true? Is the fact that the 1 in l.20 is an integer literal significant?)

-Lars


I finally solved the puzzle by digging through ancient scientific papers,
as well as some old FORTRAN and ALGOL code, and the solution turned out to
be an interesting piece of computer history trivia.

After the above code has finished, the variable B contains the radix of
the computer's numerical system.

Perhaps the comment should have tipped me off, but I had no idea that
computers had ever been anything but binary. But apparently, back in the 50s
and 60s there were computers that used the decimal and hexadecimal systems
as well. Instead of just power on/off, they had 10 or 16 separate voltage
levels to differentiate between bit values.

Not quite. They just used exponents which were powers of 10 or 16, rather
than 2. BTW, T == 1/real.epsilon. I don't know what ALOG does, so I've no
idea what Q is.


Apparently ALOG is just an old name for LOG.  At least that's what
Google tells me.

Then Q is 0.5*ln(0.5). Dunno what use that is.


Re: At compile time

2009-08-06 Thread Don

Jarrett Billingsley wrote:

*On Wed, Aug 5, 2009 at 7:23 AM, Ary Borenszweiga...@esperanto.org.ar wrote:

bearophile escribió:

Jarrett Billingsley:

C++ has static initialization that occurs before main() too.  It's just..
hidden.

I see. I have to learn more about C++. Thank you.

--
Lars T. Kyllingstad:


This is good news! The restrictions you are referring to, are they any of
the ones documented here:
http://www.digitalmars.com/d/2.0/function.html#interpretation 

That list has a point regarding what I was trying to do:

4. the function may not be a non-static member, i.e. it may not have a
this pointer

It's true regarding stucts used as values too, and not just classes...

It would be nice if the compiler could say so: I can't evaluate it because
you are using a this pointer. With other words, but much more useful than
non-constant expression.


Yes, oh my God, this is the main reason I don't use CTFE: debugging
them is virtually impossible, and the compiler does nothing to help
there.


Nice error messages for CTFE assignment statements are in the next 
release. That's probably 50% of the instances of non-constant expression.


noob question

2009-08-06 Thread llltattoolll
hi im noob here and try learn D language, i try make this example but when run 
have 2 problems.

1) when i show name and lastname show me in two lines and i wanna make in the 
same line.
2) can´t validate option si ( yes ) or no ( no ) and always show the else line.

what happend?
thx

--
code
--
import std.stdio;
import std.string;
import std.c.stdio;



void main()
{

string _nombre, _apellido, _respuesta;
int _edad, _año, _nacimiento;
_año = 2009;

writefln (Escribe tu nombre: );
_nombre = readln();

writefln (Escribe tu apellido: );
_apellido = readln();

writefln (Hola %s , _nombre ~= _apellido);

writefln (Ingresa el año de tu nacimiento: );
scanf (%d,  _nacimiento);

_edad = _año - _nacimiento;

writefln (Tu edad es %d? \t SI \t NO, _edad);
_respuesta = readln();

if ( _respuesta == si)
writeln (Muchas gracias %s, _nombre );
else
writeln (Lo siento %s entonces tienes %d, _nombre, _edad - 1 );
}
 


how does range.put work

2009-08-06 Thread O.K.
Hello,
could someone plz clearify what the exact semantics of put
are ?
Put works with an appender, but gives me a runtime exception
when using an array.

Best regards,
Oliver


Re: noob question

2009-08-06 Thread BCS

Reply to llltattoolll,

  writefln (Your age is %d? \t YES \t NO, _age ); /* here is my second 

problem, can´t into the response and program*/

  _response = readln(); /* show me else line always */


// if all else fails, what are you getting
writef(%s\n, cast(ubyte[])chomp(_response));

// might it be a case error? YES vs. yes?

  if ( chomp(_response) == yes) 
writefln (thank you %s, _name );

  else
writefln (Sorry %s you have %d, _name, _age - 1 ); /* this line always 

show me because can´t validate _response */




Linking Tango + QtD under Ubuntu (with Rebuild)

2009-08-06 Thread Michael Mittner
I'm trying to evaluate QtD. First step is to link something against Tango 
and Qt, but unfortunately I can't get it to work:

rebuild main.d -debug -full -oqobj/ -I~/coding -llqtdcore -llqtdgui -
llQtCore -llQtGui

I have Tango installed via apt-get (and the Tango repository), as well as 
libqt4-dev and all its dependencies, but still the linker can resolve almost 
nothing. When I remove the QtD imports and simply link against Tango (with a 
test Stdout), it links and runs.

Has anybody run into some similar problem? I've never been good at this 
linking business and I'm out of ideas :( so help would be much appreciated.

Regards,
Mike


Re: Shared Object with DMD v2.031

2009-08-06 Thread teo
On Thu, 06 Aug 2009 04:24:35 +0400, Sergey Gromov wrote:

 Wed, 5 Aug 2009 20:46:53 + (UTC), teo wrote:
 
 On Tue, 04 Aug 2009 18:41:50 +0400, Sergey Gromov wrote:
 
 Sun, 2 Aug 2009 11:18:24 + (UTC), teo wrote:
 
 On Sun, 02 Aug 2009 02:18:28 +0400, Sergey Gromov wrote:
 
 My guess is that test.di is exactly the same as test.d because all
 the functions are small.  Therefore compiling 'dmd prog.d test.di'
 resolves all symbols statically and the .so is simply not linked.
 Does your 'prog' have any unresolved symbols in its symbol table?
 
 It looks like this is the case. I found following in the symbols:
 0804b8cc T _D4test6testMeFZi
 0804b8d8 T _D4test9testClassFiZC4test4Test
 
 The whole nm output is too long, but if you want I can send it to
 you.
 
 When I use the *--undefined-only* option I get: $ nm -u prog [snip]
 
 All unresolved symbols seem to be in glibc.
 
 Yes.
 
 I've filed a bug:
 http://d.puremagic.com/issues/show_bug.cgi?id=3226
 
 I don't have the warning you mention.
 
 Try to run
 
 objdump -r test.o
 
 It'll print relocation table for the object file.  In my case almost all
 of them are of type R_386_32 which are load-time relocations.  A
 position-independent code should not contain them.

Correct. This is exactly the case. And my environment is identical to 
yours:
$ dmd | head -n1
Digital Mars D Compiler v2.031
$ gcc --version | head -n1
gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3
$ uname -a
Linux dev-phobos 2.6.28-14-server #47-Ubuntu SMP Sat Jul 25 01:18:34 UTC 
2009 i686 GNU/Linux

So it is reproducible with DMD2 as well.


Re: noob question

2009-08-06 Thread Sergey Gromov
Thu, 06 Aug 2009 10:56:33 -0400, lllTattoolll wrote:

 hi Lars and thx for help. I fix a little the first problem, now I paste a 
 verion in english whit coment because i´m lost here.
 the example is the same only this is in english for your compresion of my 
 problem.
 
 --
 code
 -
 
 import std.stdio;
 import std.string;
 import std.c.stdio;
 
 void main()
 {
 
 string _name, _lastname, _response;
 int _age, _year, _birth;
 _year = 2009;
 
   writef (Name: );
   _name = readln().chomp;
 
   writef (Lastname: );
   _lastname = readln().chomp;
 
   writefln (Hello %s , _name, _lastname );  /* here is my first 
 problem, dont show me the last name or show me in 
 two 
 lines ( this fix whit .chomp ) */

The format string must be Hello %s %s to display both operands.  Or
you can use writeln instead:

writeln(Hello , _name,  , _lastname);

   writefln (Year of birth: );
   scanf (%d,  _birth);

You use scanf here.  Scanf only reads the number, and leaves the Enter
symbol in the input stream.  When you later call readln() it sees that
Enter from the previous question and exits immediately.  You better
use readln() everywhere:

_birth = std.conv.to!int(readln().chomp);

   _age = _year - _birth;
 
   writefln (Your age is %d? \t YES \t NO, _age ); /* here is my second 
 problem, can´t into the response and program*/
   _response = readln();/* show me 
 else line always */
 
   if ( chomp(_response) == yes)
   writefln (thank you %s, _name );
   else
   writefln (Sorry %s you have %d, _name, _age - 1 ); /* this line 
 always show me because can´t validate _response */
 }

The rest seems to work correctly.


Re: how does range.put work

2009-08-06 Thread Daniel Keep


O.K. wrote:
 Hello,
 could someone plz clearify what the exact semantics of put
 are ?
 Put works with an appender, but gives me a runtime exception
 when using an array.
 
 Best regards,
 Oliver

The source code for the standard library comes with the compiler.

If you look in std\array.d, you find this around line 279 (reflowed for
readability):

 void put(T, E)(ref T[] a, E e) {
 assert(a.length);
 a[0] = e; a = a[1 .. $];
 }