math.pow fails at compile-time; is this a bug?

2012-08-19 Thread Caligo
template T(){
  enum a = pow(3.0, 2);
  enum b = pow(3, 2.0);
}

unittest
{
  alias T!() t;
}

compiling that I get:

/home/b/phobos/std/math.d(2369): Error: Cannot convert real to ushort* at
compile time
/home/b/phobos/std/math.d(3292):called from here: isNaN(y)
/home/b/phobos/std/math.d(3403):called from here:
impl(x,cast(real)y)
/home/b/phobos/std/math.d(3239):called from here: pow(cast(real)x,y)
t.d(478):called from here: pow(3,2)
t.d(485): Error: template instance units.T!() error instantiating

I guess this a bug?  any workarounds?


Re: Static Associative Arrays

2012-04-08 Thread Caligo
On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis jmdavisp...@gmx.com wrote:

 What do you mean my static associative arrays? Are you asking why you can't
 initialize a static variable which is an AA at compile time? e.g.

 - Jonathan M Davis

The same way I can create a static array:

int[4] = [1, 3, 4, 8];  // has value semantics

and dynamic arrays:

int[] = [1, 4, 2, 4];  // has reference semantics

I want an associative array that has value semantics and it's size
doesn't change, just like static arrays.

P.S.
another point.  I was always under the impression that static arrays
are allocated on the stack whereas dynamic arrays are allocated on the
heap and the GC cleans them up.  After today, I'm not so sure if this
is true.  Are static arrays allocated on the stack?  if so, that would
be another reason to want to have static associative arrays.


Static Associative Arrays

2012-04-07 Thread Caligo
I'm not questioning the design, but I would like to know the reason:
given the fact that associative arrays are built into the language,
why don't we have static associative arrays?


Why can't I have overloading and generics?

2012-03-09 Thread Caligo
struct B { }
struct C { }
struct D { }

struct A {

  ref A foo(B item) {
/* do something special. */
return this;
  }

  ref A foo(T)(T item) if(is(T == C) || is(T == D)) {
/* nothing special, do the same for C and D. */
return this;
  }
}

Is this unreasonable?  iirc, C++ supports this, but not D.  What's the
reason? Bug?

What's a good solution to this?

1. a generic `foo()` that uses `static if`s?

2. overload `foo()`, even if it means having function bodies that are
exactly same (code duplication).?

3. mixin templates?  I don't know about this because TDPL says it's
experimental, and I've tried and I get weird errors.


Why constructs can not be private?

2012-03-06 Thread Caligo
module A;

private struct A { }
private A a;
private mixin template magic() { }
private void foo() { }

//-

module B;

import A;

void main() {
  A b;  // #1 works
  b = a; // #2 ERROR
  foo();  // #3 ERROR
}

struct B{

 mixin magic;  // #4 works
}


What's the point of declaring struct A private if it's not going to
behave as such?  is this another bug in DMD?  I want template mixin
magic to not be accessible outside module A.  Would that be possible?


A very strange bug. DMD 2.058 64-bit Linux

2012-02-26 Thread Caligo
bug.d
88
@trusted:

import std.datetime : benchmark;
import std.stdio: writefln, writeln;

alias double Real;

void ben(alias fun)(string msg, uint n = 1_000_000) {
  auto b = benchmark!fun(n);
  writefln( %s %s ms, msg, b[0].to!(msecs, int));
}

struct Matrix(int row, int col) {

private:
  alias row Row;
  alias col Col;
  alias Real[Row * Col] Data;

public:
  Data _data = void;
  alias _data this;
  this(const Real[Row*Col] data) pure nothrow { _data = data; }
}

M inverse(M)(const auto ref M m)  {
  writeln(m[]);
  M minv = m;
  return minv;
}

unittest {
  alias Matrix!(4, 4) Matrix4x4;
  auto m9 = Matrix4x4([4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 0, 1]);
  ben!( {auto r = inverse(m9);} )(4x4 inverse:);
}
88

t1.d
88
import std.stdio;

void main(){ }
88

It took me a long time to pinpoint this because it's tricky to trigger the bug.

Once you have those two files, compile with this:

dmd -unittest t1.d bug.d

and then run t1:

./t1

The output you get should look like this:

...
[0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 0]


Obviously the output is wrong.  'm9' for some reason is getting
overwritten.  In my project this caused big problems because there are
other m# with different values, and their values would literally get
copied to m9.  Calling inverse() on m9 then would fail because the
other matrices are not invertible.  Placing a writeln() in inverse()
helped me realize that what was being passed to inverse() was being
modified somewhere.  I'm still now sure how m9 is being modified.

Another point, compiling with this:

dmd -unittest bug.d t1.d

and then running bug:

./bug

doesn't trigger the bug.


Could someone else please confirm this behavior?


Re: A very strange bug. DMD 2.058 64-bit Linux

2012-02-26 Thread Caligo
Thanks.  I have reported the bug:
http://d.puremagic.com/issues/show_bug.cgi?id=7595


Re: 2.058 broke my build. Is this a bug?

2012-02-24 Thread Caligo
That was a typo, and it doesn't change anything.  Here is a shorter version:

88
import std.datetime;
import std.stdio;

struct A{

  auto fun(A a){ return 0; }
}

void bench(alias fun)(string msg, uint n = 1_000_000){

  auto b = benchmark!fun(n);
  writefln( %s %s ms, msg, b[0].to!(msecs, int));
}

unittest{

  A a, b;

  void test1(){
auto r = a.fun(b);
  }

  bench!( {auto r = a.fun(b);} )(Does Not work);
  bench!(test1)(Works);
}

void main(){ }
88



And here is the error:

/usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function
'benchmark' cannot call system delegate '__lambda1'
t1.d(11): Error: template instance
t1.__unittest2.benchmark!(__lambda1) error instantiating
t1.d(23):instantiated from here: bench!(delegate @system void()
{
int r = a.fun(b);
}
)
t1.d(23): Error: template instance t1.__unittest2.bench!(delegate @system void()
{
int r = a.fun(b);
}
) error instantiating


Re: 2.058 broke my build. Is this a bug?

2012-02-24 Thread Caligo
Is there another workaround than the one I've posted?

http://d.puremagic.com/issues/show_bug.cgi?id=7577

On Fri, Feb 24, 2012 at 8:44 PM, James Miller ja...@aatch.net wrote:
 Hmm it seems that the delegate is being implicitly marked as system, and im
 not sure why benchmark is @safe. I'd say file a bug report.


Re: 2.058 broke my build. Is this a bug?

2012-02-24 Thread Caligo
I found another workaround: mark the module as trusted.


2.058 broke my build. Is this a bug?

2012-02-23 Thread Caligo
88

import std.datetime : benchmark;
import std.stdio : writefln;

struct A(int r, int c){

 public:
  alias float[r * c] Data;

  Data _data;

  auto opBinary(A a){
float t;
foreach(i; 0..r*c)
  foreach(j; 0..r*c)
t += this[i,j];
return a;
  }
  pure float opIndex(size_t rr, size_t cc = 0) const{ return _data[cc
+ rr * c]; }

  pure ref float opIndex(size_t rr, size_t cc = 0){ return _data[cc + rr * c]; }
}

void bench(alias fun)(string msg, uint n = 1_000_000){

  auto b = benchmark!fun(n);
  writefln( %s %s ms, msg, b[0].to!(msecs, int));
}

unittest{

  alias A!(3, 3) AA;
  AA a;

  bench!( {auto r = a * a;})(broken);
}

void main(){ }

88

Other parts of my code using bench() works fine, except in rare cases.
 So I'm guessing this is a bug?  I can't tell if it's in DMD or
std.datetime.  Can anyone help?

I will bug report myself tomorrow if it turns out to be a bug for sure.

DMD 2.058, 64-bit GNU/Linux


Re: floating-WTF - Compiler-BUG with 64bit

2012-01-28 Thread Caligo
I've already reported, and it's been fixed in the latest:

http://d.puremagic.com/issues/show_bug.cgi?id=7376


On Sat, Jan 28, 2012 at 9:56 AM, sclytrack sclytr...@fake.com wrote:
 On 01/25/2012 01:12 AM, Timon Gehr wrote:

 On 01/24/2012 10:28 PM, %u wrote:

 Shouldn't this go into 'digitalmars.D' ?


 It should go straight to the bug tracker.


 Issue 7391 - floating wtf dmd 2.057 64


Re: floating-WTF - Compiler-BUG with 64bit

2012-01-28 Thread Caligo
On Sat, Jan 28, 2012 at 2:04 PM, sclytrack sclytr...@hotmail.com wrote:


 Now I've waisted Walter's precious time.
 I feel so sad now :-(



I should have made a post about the fact that I had already bug
reported it, but I forgot. Sorry.


Re: floating-WTF

2012-01-24 Thread Caligo
On Tue, Jan 24, 2012 at 6:51 AM, Dejan Lekic dejan.le...@gmail.com wrote:
 No, it is not a bug.

 Here is a hint:

 import std.stdio;

 int main() {
  float f;
  writeln(f);
  return 0;
 }

 /+--- output --+
 nan
 +--- end of output ---+/



wow, what a great hint.

sorry, but I'm not convinced that it's not a bug.


Re: floating-WTF - Compiler-BUG with 64bit

2012-01-24 Thread Caligo
How did you compile it?  As in my original post, it matters how you
compile it.  In this case (I'm on a 64-bit GNU/Linux system),
compiling with '-inline' doesn't trigger the bug.


floating-WTF

2012-01-23 Thread Caligo
alias double Real;
//alias float Real;

// simple linear interpolation; I partitioned the internals to help me
figure out what was happening.
Real lerp(Real t, Real a, Real b){
  Real s1 = (1.0 - t) * a;
  Real s2 = t * b;
  Real rt1 = s1 + s2;
  Real rt2 = ((1.0 - t) * a) + (t * b);
  writefln(t=%.2f, a=%.2f, b=%.2f, s1=%.2f, s2=%.2f, rt1=%.2f,
rt2=%.2f  :::, t, a, b, s1, s2, rt1, rt2);
  return rt2;
}

unittest{

  writeln(lerp(0.75, -2.0, 2.0));  // the correct result is 1.0
}

compile and run with 'dmd -inline -unittest' and the output should be:

t=0.75, a=-2.00, b=2.00, s1=-0.50, s2=1.50, rt1=1.00, rt2=1.00  :::
1

Now, change 'Real' to float by uncommenting the second line and
compile and run with 'dmd -unittest'.  This is what I get for the
output:

t=0.75, a=0.00, b=2.00, s1=-0.50, s2=0.00, rt1=-0.50, rt2=1.50  :::
1.5

I have no idea why 'a' is zero.  'rt1' and 'rt2' do not have the same
value, and 'lerp' does not return 1.0.  Compiling with 'dmd -inline
-unittest' does produce the correct result as before, though.

You can play with different compiler options, such as -O, to get the
same weird behavior.

I presume this is another DMD bug?


no-argument constructor: is this a bug?

2012-01-22 Thread Caligo
struct A(uint samples){

  float[samples] _data = void;

  this(float val = 0.0f){ fill(_data[], val); }
}


  auto a = A!8();

a._data is filled with garbage instead of zeros because the
no-argument constructor is called instead of the one that I've
defined.


Re: out default argument of void

2012-01-04 Thread Caligo
On Wed, Jan 4, 2012 at 4:40 PM, Jesse Phillips
jessekphillip...@gmail.com wrote:

 Out parameters are initialized. The declaration you want is:

 bool fun(double theta, A a = A.init, B b = B.init, C c = C.init){ /* ... */
 }


In my case A, B, and C are structs, so that works the way I wanted it. Nice!

It doesn't, however, work with primitive types:

void fun2(int a, out int b = int.init){ }
int a = 32;
fun2(a);  Error: constant 0 is not an lvalue

oh well.


Re: typedef deprecated - now what ?

2011-12-30 Thread Caligo
On Fri, Dec 30, 2011 at 4:35 AM, Stephan s...@extrawurst.org wrote:

 is there a template or something in phobos to get the same typesafe
 behaviour of good old typedef ?

 S.


Get over it, move on, and hope they fix the thousands of bugs left in DMD.

P.S.
use `alias`.


Re: IDE with renaming possibility

2011-11-06 Thread Caligo
On Sun, Nov 6, 2011 at 9:28 AM, Jabba Laci jabba.l...@gmail.com wrote:

 Hi,

 I'm new to D, I just started to read the book of Andrei.

 As an IDE, I use Eclipse with the DDT plugin. However, I don't find an
 important feature: renaming variables/functions/etc. If it's not in
 DDT, what IDE do you suggest? I use Linux.

 Thanks,

 Laszlo


I use Emacs and replace regexp.


Re: Is this a bug?

2011-09-14 Thread Caligo
On Tue, Sep 13, 2011 at 6:22 PM, Jonathan M Davis jmdavisp...@gmx.comwrote:

 On Tuesday, September 13, 2011 15:34 Caligo wrote:
  On Tue, Sep 13, 2011 at 4:47 AM, Jonathan M Davis
 jmdavisp...@gmx.comwrote:
   On Monday, September 12, 2011 23:15:19 Caligo wrote:
On Mon, Sep 12, 2011 at 10:44 PM, Jonathan M Davis
  
   jmdavisp...@gmx.comwrote:
 On Monday, September 12, 2011 22:38:25 Caligo wrote:
  Great. So is it a known bug?

 I don't know. You'd have to search bugzilla:
 d.puremagic.com/issues

 - Jonathan M Davis
   
Searching bugzilla (horrible technology) is never fun for me, thx.
  
   Most search technology sucks on some level. But regardless, if bugs
   aren't reported, then they're not likely to be fixed. So, if you want
 to
   ensure that
   the bugs that you find get fixed, you need to report them, which for
   better or
   worse means using bugzilla.
  
   - Jonathan M Davis
 
  *sigh*
  I think you fail to understand my situation.
 
  I don't know anything about D's internals and I know nothing about
  compilers and how they work. If I did, I wouldn't ask on
  digitalmars.D.learn. But, I'll go ahead and bug report this.
 
  http://d.puremagic.com/issues/show_bug.cgi?id=6665

 I don't know much about dmd's internals either. But given that you have an
 error that you can search for ('/Internal error: ../ztc/cg87.c 202'), it
 shouldn't be hard to at least see whether there's anything which is
 obviously
 the same. And if there isn't then, you report it. Understanding how dmd
 works
 isn't really necessary to reporting the bug, and worse case, you end up
 reporting a bug which has already been reported, which is better than the
 bug
 never getting reported.

 Most bugs where the compiler gives an internal error or an assertion in the
 compiler gets triggered get fixed fairly quickly, and even if they don't,
 they're not the sort of bug that much of anyone outside of the dmd devs who
 is
 going to have any clue what's going on with the bug. So, it's probably not
 all
 that fruitful to inquire about that sort of bug on any of the newsgroups.
 For
 the most part, the dmd devs don't seem to pay attention to D.Learn, so even
 if
 they would recognize the issue, they wouldn't respond to it in D.Learn. The
 odds of them seeing it in D newsgroup are higher, but when it comes to bugs
 which are internal errors or assertions in the compiler, I'd suggest that
 you just do a cursory search in bugzilla and then report them.

 Bugs which relate to the language's behavior are much more likely to be
 recognized by others in the newsgroups (including D.Learn), so asking about
 them can be fruitful, but internal compiler errors and assertions isn't the
 sort of thing that people outside of the dmd devs generally recognize.

 - Jonathan M Davis


Thanks for the explanation, Jonathan.  And yes, I did search for it and I
got hundreds of results.  There is no way for someone like me to go through
all that and figure out if it's a duplicate.  So, as I said, I've reported
it.  Don has already responded to the bug, and it seems that it's Linux
related as he can't reproduce it on Windows.


Re: Is this a bug?

2011-09-13 Thread Caligo
On Tue, Sep 13, 2011 at 4:47 AM, Jonathan M Davis jmdavisp...@gmx.comwrote:

 On Monday, September 12, 2011 23:15:19 Caligo wrote:
  On Mon, Sep 12, 2011 at 10:44 PM, Jonathan M Davis
 jmdavisp...@gmx.comwrote:
   On Monday, September 12, 2011 22:38:25 Caligo wrote:
Great. So is it a known bug?
  
   I don't know. You'd have to search bugzilla: d.puremagic.com/issues
  
   - Jonathan M Davis
 
  Searching bugzilla (horrible technology) is never fun for me, thx.

 Most search technology sucks on some level. But regardless, if bugs aren't
 reported, then they're not likely to be fixed. So, if you want to ensure
 that
 the bugs that you find get fixed, you need to report them, which for better
 or
 worse means using bugzilla.

 - Jonathan M Davis


*sigh*
I think you fail to understand my situation.

I don't know anything about D's internals and I know nothing about compilers
and how they work.  If I did, I wouldn't ask on digitalmars.D.learn.  But,
I'll go ahead and bug report this.

http://d.puremagic.com/issues/show_bug.cgi?id=6665


Is this a bug?

2011-09-12 Thread Caligo
Trying to help someone on SO, I ran into problems.

On a 64-bit Linux machine, with DMD 2.055

This gives:  '/Internal error: ../ztc/cg87.c 202'
void main(){

  auto f = (double m){ static double sum = 0.0; return sum += m * m; };
  double[] a = array(map!f(iota(1.0, 25.0, 1.0)));
  writeln(a);
}


This one compiles but I get Segmentation fault.
void main(){

   auto f = (double m){ /* static double sum = 0.0;*/ return m * m; };
   double[] a = array(map!f(iota(1.0, 25.0, 1.0)));
   writeln(a);
 }


This one compiles and runs.
 void main(){

   auto f = (double m){ /* static double sum = 0.0;*/ return m * m; };
   double[] a = array(map!f(array(iota(1.0, 25.0, 1.0;
   writeln(a);
   //works
 }


Putting everything together, I still get 'Internal error: ../ztc/cg87.c 202'
with this one.
void main(){

  auto f = (double m){ static double sum = 0.0; return sum += m * m; };
  double[] a = array(map!f(array(iota(1.0, 25.0, 1.0;
  writeln(a);
}


Re: Is this a bug?

2011-09-12 Thread Caligo
Great. So is it a known bug?

On Mon, Sep 12, 2011 at 10:35 PM, Jonathan M Davis jmdavisp...@gmx.comwrote:

 On Monday, September 12, 2011 22:31:33 Caligo wrote:
  Trying to help someone on SO, I ran into problems.
 
  On a 64-bit Linux machine, with DMD 2.055
 
  This gives: '/Internal error: ../ztc/cg87.c 202'
  void main(){
 
  auto f = (double m){ static double sum = 0.0; return sum += m * m; };
  double[] a = array(map!f(iota(1.0, 25.0, 1.0)));
  writeln(a);
  }
 
 
  This one compiles but I get Segmentation fault.
  void main(){
 
  auto f = (double m){ /* static double sum = 0.0;*/ return m * m; };
  double[] a = array(map!f(iota(1.0, 25.0, 1.0)));
  writeln(a);
  }
 
 
  This one compiles and runs.
  void main(){
 
  auto f = (double m){ /* static double sum = 0.0;*/ return m * m; };
  double[] a = array(map!f(array(iota(1.0, 25.0, 1.0;
  writeln(a);
  //works
  }
 
 
  Putting everything together, I still get 'Internal error: ../ztc/cg87.c
 202'
  with this one.
  void main(){
 
  auto f = (double m){ static double sum = 0.0; return sum += m * m; };
  double[] a = array(map!f(array(iota(1.0, 25.0, 1.0;
  writeln(a);
  }

 It's always a bug if you see Internal error.

 - Jonathan M Davis



Re: Is this a bug?

2011-09-12 Thread Caligo
On Mon, Sep 12, 2011 at 10:44 PM, Jonathan M Davis jmdavisp...@gmx.comwrote:

 On Monday, September 12, 2011 22:38:25 Caligo wrote:
  Great. So is it a known bug?

 I don't know. You'd have to search bugzilla: d.puremagic.com/issues

 - Jonathan M Davis


Searching bugzilla (horrible technology) is never fun for me, thx.


Re: D with gmp via swig

2011-08-14 Thread Caligo
Shouldn't there be an interface to GMP and MPFR in Phobos by default?


Re: incompatible types!

2011-04-05 Thread Caligo
It's just frustrating, that's all.  Writing thousands of lines of code
and having everything stop because of a compiler bug is just
frustrating.

I know progress is being made, and all that is appreciated.  But, I
don't remember ever hearing anything about D2 being in beta.  If
anything, I remember months ago where D2 was recommended for new
projects.  So, now I'm not sure what I'm supposed to do.  Start all
over again from scratch?  I really like my design, so I guess I'll
have to wait till it gets fixed.



Re: The is expression

2011-04-01 Thread Caligo
On Fri, Apr 1, 2011 at 5:14 PM, enuhtac enuhtac_li...@gmx.de wrote:
 Hello,

 the is expression is a great feature of D - but its use is not very
 intuitive, at least for me.
 I'm trying to write a template that figures out if the template
 parameter is of a given type.
 This is the type I would like to check for:

 struct A( T, string s )
 { ... };

 One possibility to accomplish this check is explicit template
 specialization:

 template isA( T )
 {
    enum bool isA = false;
 };

 template isA( T : A!( U, s ), U, string s )
 {
    enum bool isA = true;
 };

 This more or less the C++ approach. But in D this could also be done
 with static if and the is expression. As I understand is it should
 be done like this:

 template isA( T )
 {
    static if( is( T U == A!( U, s ), string s ) )
        enum bool isA = true;
    else
        enum bool isA = false;
 };

 But this does not work. So what am I doing wrong?

 Regards,
 enuhtac



I'm new too, but I think it should be like this:

template isA( T ){

  enum bool isA = is(T : A)
}

if the name of enum is same as the template then you could use it as such:

if( isA( T ) ){ }

instead of

if( isA( T ).isA ){ }

Also note that : allows implicit conversion, while == requires the
types to be exactly the same.


Re: template template parameter

2011-03-31 Thread Caligo
I should have been more clear, but my actual question is how do I
access the parameters of a template parameter.  My example works, but
I wanted to know if there is a different and perhaps a better of doing
it.

In your example it would look something like this:

struct SomeContainer(T, int x, int y){ }

struct Foo(alias ContainerType){

  // e.g.
  int my_x = ContainerType.x * 2;
  int my_y = ContainerType.y * 2;
}


Module Searching Roots

2011-03-27 Thread Caligo
I'm in directory ~/root and I have 't1.d' in ~/root/src/core
directory, and nothing else.

I run:  'gdc -O3 -o t1 t1.d -B./src/core' but I get

gdc: t1.d: No such file or directory
gdc: no input files

I've tried different paths, but I can't get it work.  What's the problem?


Re: inline functions

2011-03-26 Thread Caligo
On Sat, Mar 26, 2011 at 3:47 AM, Jonathan M Davis jmdavisp...@gmx.com wrote:
 On 2011-03-26 01:06, Caligo wrote:
 On Fri, Mar 25, 2011 at 11:56 PM, Jonathan M Davis jmdavisp...@gmx.com
 wrote:
  On 2011-03-25 21:21, Caligo wrote:
  On Fri, Mar 25, 2011 at 10:49 PM, Jonathan M Davis jmdavisp...@gmx.com
 
  wrote:
   On 2011-03-25 19:04, Caligo wrote:
   T[3] data;
  
   T dot(const ref Vector o){
       return data[0] * o.data[0] + data[1] * o.data[1] + data[2] *
   o.data[2]; }
  
   T LengthSquared_Fast(){ return data[0] * data[0] + data[1] * data[1]
   + data[2] * data[2]; }
   T LengthSquared_Slow(){ return dot(this); }
  
  
   The faster LengthSquared() is twice as fast, and I've test with GDC
   and DMD.  Is it because the compilers don't inline-expand the dot()
   function call?  I need the performance, but the faster version is too
   verbose.
  
   It sure sounds like it didn't inline it. Did you compile with -inline?
   If you didn't then it definitely won't inline it.
  
   - Jonathan M Davis
 
  I didn't know I had to supply GDC with -inline, so I did, and it did
  not help.  In fact, with the -inline option the performance gets worse
  (for DMD and GDC), even for code that doesn't contain any function
  calls.  In any case, code compiled with DMD is always behind GDC when
  it comes to performance.
 
  I don't know what gdc does, but you have to use -inline with dmd if you
  want it to inline anything. It also really doesn't make any sense at all
  that inlining would harm performance. If that's the case, something
  weird is going on. I don't see how inlining could _ever_ harm
  performance unless it just makes the program's binary so big that _that_
  harms performance. That isn't very likely though. So, if using -inline
  is harming performance, then something weird is definitely going on.
 
  - Jonathan M Davis

 The only time that -inline has no effect is when I turn on -O3.  This
 is also when the code performs the best.  I've never used -O3 in my
 C++ code, but I guess things are different in D even with the same
 back-end.

 I really don't know what gdc does. With dmd, inlining is not turned on unless
 -inline is used. Also, -inline with dmd does not force inlining, it merely
 turns on the optimization. The compiler still chooses where and when it's best
 to inline.

 With gcc, I believe that inlining is normally turned on at a pretty low
 optimization level (probably -O), and like dmd, it chooses where and when it's
 best to inline, but unlike dmd, it uses the inline keyword in C++ as a hint as
 to what it should do. However, -O3 forces inlining on all functions marked
 with inline. How gdc deals with that given that D doesn't have an inline
 keyword, I don't know.

 Regardless, given what inlining does, I have a _very_ hard time believing that
 it would ever degrade performance unless it's buggy.

 - Jonathan M Davis



I was going to post my code, but I take back what I said.  What is
happening is that there is a lot of fluctuation in performance.  The
low performance always occurred when I had -inline enabled, which made
me think -inline degrades performance.  The performance should be
consistent, but for some reason it's not.

The important thing is that -inline doesn't make any difference with
GDC.  The -O3 does make a big difference.


is(T : long) vs is(T == long)

2011-03-26 Thread Caligo
What is the difference between this:
template isNumerik(T){
  enum bool isNumerik = is(T : long) || is(T : real);
}

and this:

template isNumerik(T){
  enum bool isNumerik = is(T == long) || is(T == real);
}


They both work and I can't find anywhere in the book where it talks about the :


Re: is(T : long) vs is(T == long)

2011-03-26 Thread Caligo
:-) thanks.


Re: inline functions

2011-03-26 Thread Caligo
I've changed my code since I posted this, so here is something
different that shows performance difference:

module t1;

struct Vector{

private:
  double x = void;
  double y = void;
  double z = void;

public:
  this(in double x, in double y, in double z){
this.x = x;
this.y = y;
this.z = z;
  }

  Vector opBinary(string op)(const double rhs) const if(op == *){
return mixin(Vector(x~op~rhs, y~op~rhs, z~op~rhs));
  }

  Vector opBinaryRight(string op)(const double lhs) const if(op == *){
return opBinary!op(lhs);
  }
}

void main(){

  auto v1 = Vector(4, 5, 6);
  for(int i = 0; i  60_000_000; i++){
v1 = v1 * 1.0012;
//v1 = 1.0012 * v1;
  }
}


Calling opBinaryRight:
/*  gdc -O3 -o t1 t1.d

real0m0.394s
user0m0.390s
sys 0m0.000s
*/

Calling opBinary:
/* gdc -O3 -o t1 t1.d

real0m0.321s
user0m0.310s
sys 0m0.000s
*/

Those results are best of 10.

There shouldn't be a performance difference between the two, but there.


Re: inline functions

2011-03-25 Thread Caligo
On Fri, Mar 25, 2011 at 10:49 PM, Jonathan M Davis jmdavisp...@gmx.com wrote:
 On 2011-03-25 19:04, Caligo wrote:
 T[3] data;

 T dot(const ref Vector o){
     return data[0] * o.data[0] + data[1] * o.data[1] + data[2] * o.data[2];
 }

 T LengthSquared_Fast(){ return data[0] * data[0] + data[1] * data[1] +
 data[2] * data[2]; }
 T LengthSquared_Slow(){ return dot(this); }


 The faster LengthSquared() is twice as fast, and I've test with GDC
 and DMD.  Is it because the compilers don't inline-expand the dot()
 function call?  I need the performance, but the faster version is too
 verbose.

 It sure sounds like it didn't inline it. Did you compile with -inline? If you
 didn't then it definitely won't inline it.

 - Jonathan M Davis


I didn't know I had to supply GDC with -inline, so I did, and it did
not help.  In fact, with the -inline option the performance gets worse
(for DMD and GDC), even for code that doesn't contain any function
calls.  In any case, code compiled with DMD is always behind GDC when
it comes to performance.


C++ to D: mutable

2011-03-24 Thread Caligo
Greetings,

I have a C++ class that I would like to rewrite it in D.  The class
has members that are declared as 'mutable'.  How do I achieve the same
effect in D? if not, what is recommended?


Running GDC

2011-03-21 Thread Caligo
I updated my gdc repo and now I can't run the new build; it can't find the
includes and the libs.
This is what I've come up with so far:
-I/../D/gdc/Bin/usr/local/include/d2/4.4.5/
-I/../D/gdc/Bin/usr/local/include/d2/4.4.5/x86_64-unknown-linux-gnu/

What's the rest?


Re: Points and Vectors in 3D

2011-03-13 Thread Caligo
On Sun, Mar 13, 2011 at 9:11 AM, Simen kjaeraas simen.kja...@gmail.comwrote:

 Spacen Jasset spacenjas...@yahoo.co.uk wrote:

 Can't see a fitting operator in D. Multiplication (*) is ambiguous at best
 and no other operator seems fitting.


I agree.  It's just better do define 'dot' and 'cross'.  That's how it's
done in Eigen and it works great.


struct construct with array

2011-03-12 Thread Caligo
struct Test{

  public double[3] ar_;
  this(double[3] ar){
this.ar_ = ar;
  }
}

void main(){

  double[3] v1 = [1.0, 2.0, 3.0];
  double[3] v2 = [2.0, 3.0, 4.0];

  auto t1 = Test(v1[0..$] + v2[0..$]); // error

}


I want to add those two arrays and call the constructor in one line, but I'm
getting an error.  Any ideas?


Points and Vectors in 3D

2011-03-12 Thread Caligo
Given everything that D offers, what would be the best way to implement a
Point and a Vector type?  The same (x, y, z) can be used to represent
vectors, but a point represents a position, whereas a vector represents a
direction.  So, would you define two different structs for each? or define
and implement an interface?  a fixed array or POD members?


Re: Mixins: to!string cannot be interpreted at compile time

2011-03-11 Thread Caligo
On Tue, Mar 1, 2011 at 1:15 PM, Peter Lundgren lundg...@rose-hulman.eduwrote:

 That worked, thanks. This is interesting because the example used in The D
 Programming Language on page 83 gets away with it just fine. I had no
 problem
 running this:

 result ~= to!string(bitsSet(b)) ~ , ;



How did you get that example on page 83 to compile?  I'm getting undefined
identifier bitsSet, and it's not in std.intrinsic or std.bitmanip.


Re: Mixins: to!string cannot be interpreted at compile time

2011-03-11 Thread Caligo
On Fri, Mar 11, 2011 at 11:48 AM, Caligo iteronve...@gmail.com wrote:



 On Tue, Mar 1, 2011 at 1:15 PM, Peter Lundgren 
 lundg...@rose-hulman.eduwrote:

 That worked, thanks. This is interesting because the example used in The
 D
 Programming Language on page 83 gets away with it just fine. I had no
 problem
 running this:

 result ~= to!string(bitsSet(b)) ~ , ;



 How did you get that example on page 83 to compile?  I'm getting undefined
 identifier bitsSet, and it's not in std.intrinsic or std.bitmanip.


nvm, it's right there on that very page.


Re: Starting with D

2011-02-06 Thread Caligo
On Sun, Feb 6, 2011 at 5:35 PM, Julius n0r3...@web.de wrote:

 Hi there,
 i'm all new to D but not new to programming in general.
 I'd like to try D but i didn't find a nice tutorial yet.
 I don't want to read a whole book, I just want to get the basics so I can
 start.
 Can you help me find something like that?

 Best regards, Julius


I say get the book.  The D Programming Language is a great book.  If you are
a university student you'll probably be able to read it for free.  I finally
got my hard-copy, and it's great.


Re: TDPL dictionary example - error

2010-12-24 Thread Caligo
I forgot to ask, what version of DMD are you using?

2010/12/24 Mariusz Gliwiński alienballa...@gmail.com

 Friday 24 December 2010 @ 06:24:34 Caligo:
  Greetings,
 
  I just joined here, so sorry if this has been posted before.
 
  I'm reading TDPL and the example on page 8 doesn't compile.  I'm using
 the
  latest GDC with GCC 4.4.5.  I've checked the errata, but nothing for this
  error.
 
  import std.stdio;
  import std.string;
 
  void main(){
 
   size_t[char[]] dictionary;
  foreach(line; stdin.byLine()){
foreach(word; splitter(strip(line))){
if(word in dictionary) continue;
  auto newID = dictionary.length;
dictionary[word] = newID;
  writeln(newID, '\t', word);
  }
  }
  //writeln(dictionary.length);
  }
 
 
  This is the error:
  dictionary.d:12: Error: associative arrays can only be assigned values
 with
  immutable keys, not char[]
 
  If i use immutable keys, it works, but it defeats the purpose.  So what's
  wrong with the code?

 It works for DMD, I think it's a GDC bug (i'm new in D too, so i might be
 wrong). Keep in mind that GDC implements only D 1.0 and book has D 2.0
 code.

 Sincerely,
 Mariusz Gliwiński



TDPL dictionary example - error

2010-12-23 Thread Caligo
Greetings,

I just joined here, so sorry if this has been posted before.

I'm reading TDPL and the example on page 8 doesn't compile.  I'm using the
latest GDC with GCC 4.4.5.  I've checked the errata, but nothing for this
error.

import std.stdio;
import std.string;

void main(){

 size_t[char[]] dictionary;
foreach(line; stdin.byLine()){
  foreach(word; splitter(strip(line))){
  if(word in dictionary) continue;
auto newID = dictionary.length;
  dictionary[word] = newID;
writeln(newID, '\t', word);
}
}
//writeln(dictionary.length);
}


This is the error:
dictionary.d:12: Error: associative arrays can only be assigned values with
immutable keys, not char[]

If i use immutable keys, it works, but it defeats the purpose.  So what's
wrong with the code?


Re: TDPL dictionary example - error

2010-12-23 Thread Caligo
No, GDC supports D1 and D2.  Version 2.051 I think.  I know I've compiled
mine with D2 support.

2010/12/24 Mariusz Gliwiński alienballa...@gmail.com

 Friday 24 December 2010 @ 06:24:34 Caligo:
  Greetings,
 
  I just joined here, so sorry if this has been posted before.
 
  I'm reading TDPL and the example on page 8 doesn't compile.  I'm using
 the
  latest GDC with GCC 4.4.5.  I've checked the errata, but nothing for this
  error.
 
  import std.stdio;
  import std.string;
 
  void main(){
 
   size_t[char[]] dictionary;
  foreach(line; stdin.byLine()){
foreach(word; splitter(strip(line))){
if(word in dictionary) continue;
  auto newID = dictionary.length;
dictionary[word] = newID;
  writeln(newID, '\t', word);
  }
  }
  //writeln(dictionary.length);
  }
 
 
  This is the error:
  dictionary.d:12: Error: associative arrays can only be assigned values
 with
  immutable keys, not char[]
 
  If i use immutable keys, it works, but it defeats the purpose.  So what's
  wrong with the code?

 It works for DMD, I think it's a GDC bug (i'm new in D too, so i might be
 wrong). Keep in mind that GDC implements only D 1.0 and book has D 2.0
 code.

 Sincerely,
 Mariusz Gliwiński