Re: generic + numeric + literals = abomination

2010-03-28 Thread so
Why a generic code have to be ugly? At this age of compilers and languages, and the capabilities of DMD? Why that many casts? or implicit casts? DMD already doing it behind the scenes with constant folding, not sure but i think literals stay that way mostly because of C compatibility! Thanks.

Re: generic + numeric + literals = abomination

2010-03-28 Thread #ponce
so Wrote: > Basically what i am asking is hmmm, ability to write generic constants? :) > > Thanks! Hi, When writing generic FP code i always use real literals and cast to T, or int. I suggest doing this. Like: T exp3(T)(T x) { if (x < cast(T)(-1.15365L)) { retu

Re: generic + numeric + literals = abomination

2010-03-28 Thread so
I can't think of any solution they might provide sorry. You know there are C++ equivalents, for years and they didn't solve any problems above. Maybe i can use them to disable implicit casting, but then again program won't compile. :P Thanks! On Sun, 28 Mar 2010 18:09:21 +0400, biozic wrot

Re: generic + numeric + literals = abomination

2010-03-28 Thread biozic
Le 28/03/10 10:57, so a écrit : Well, i am having hard time explaining, it is not a surprise that you don't understand. To make things clearer, lets forget floats for a seconds and change your code to standard unsigned types. import std.stdio: writeln; struct Vector(T) { this(T m) { mm = m; }

Re: generic + numeric + literals = abomination

2010-03-28 Thread so
You didn't change anything there, just back to original code, now just enabled implicit cast again! Please read my next replies, everything should be clear now. :) Thanks! On Sun, 28 Mar 2010 14:32:21 +0400, bearophile wrote: so: Well, i am having hard time explaining, it is not a surp

Re: generic + numeric + literals = abomination

2010-03-28 Thread bearophile
so: > Well, i am having hard time explaining, it is not a surprise that you > don't understand. I think I have understood you this time. Writing skills are important for a programmer :-) Is this what you are asking for? But it's not very good code: import std.stdio: writeln; struct Vector(T)

Re: generic + numeric + literals = abomination

2010-03-28 Thread so
Everything comes to this... Why "3" is an int? Why "0.3 is a double? I guess these constraints was there before generic coding comes out, and we are just stuck with it! If these sound so naive, sorry about it, I don't know compiler/language history. Thanks! On Sun, 28 Mar 2010 12:29:17 +04

Re: generic + numeric + literals = abomination

2010-03-28 Thread so
Basically what i am asking is hmmm, ability to write generic constants? :) Thanks! On Sun, 28 Mar 2010 12:29:17 +0400, bearophile wrote: so: With one exception yes, i want all 3 test pass with your fix to implicit cast. You know, we are trying to write generic code. I don't understand

Re: generic + numeric + literals = abomination

2010-03-28 Thread so
Well, i am having hard time explaining, it is not a surprise that you don't understand. To make things clearer, lets forget floats for a seconds and change your code to standard unsigned types. import std.stdio: writeln; struct Vector(T) { this(T m) { mm = m; } Vector opBinary(strin

Re: generic + numeric + literals = abomination

2010-03-28 Thread bearophile
so: > With one exception yes, i want all 3 test pass with your fix to implicit > cast. > You know, we are trying to write generic code. I don't understand your problem/needs, sorry. Bye, bearophile

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
With one exception yes, i want all 3 test pass with your fix to implicit cast. You know, we are trying to write generic code. Thanks! On Sat, 27 Mar 2010 22:21:46 +0200, bearophile wrote: Are you trying to do this? import std.stdio: writeln; struct Vector(T) { this(T m) { mm = m; }

Re: generic + numeric + literals = abomination

2010-03-27 Thread bearophile
so: > With this in mind, just one thing bugs me. > > > import std.stdio; > > struct vector(T) { > this(T m) { mm = m; } > vector!T opBinary(string s)(T m) if(s=="*") { > return vector!T(mm * m); > } > T mm; > } > > void test(T)() { > vector!T v =

Re: generic + numeric + literals = abomination

2010-03-27 Thread biozic
Le 27/03/10 18:18, so a écrit : With this in mind, just one thing bugs me. import std.stdio; struct vector(T) { this(T m) { mm = m; } vector!T opBinary(string s)(T m) if(s=="*") { return vector!T(mm * m); } T mm; } void test(T)() { vector!T v = vector!T(0.5); vector!T u = v * 0.3; writeln

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
Oh... wait a second. In http://www.digitalmars.com/d/2.0/float.html : "Regardless of the type of the operands, floating point constant folding is done in real or greater precision. It is always done following IEEE 754 rules and round-to-nearest is used. Floating point constants are internal

Re: generic + numeric + literals = abomination

2010-03-27 Thread Jesse Phillips
I think you would end up creating a scalar class/struct with operator overloading to get the behavior you are looking for. I realize that is more overhead than what you would want but I don't see another way. so wrote: > In C++! > > I have a type defined in the core library like.. > typedef float

Re: generic + numeric + literals = abomination

2010-03-27 Thread bearophile
so: > When i have the code : > scalar m = 0.5fp; > > I want compiler to implicitly cast it to typeof(scalar). > so when the scalar is float, m will be 0.5f. I am starting to understand, sorry if I am a dumb bear :-) Programming in C++/D is usually much less hard than understanding humans. I thin

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 15:54:19 +0200, Bill Baxter wrote: Note that 'real' is a built in type in D. It's an 80-bit float on x86 procs and 64-bit elsewhere. So .5L is like cast(real).5. Not the solution you were looking for. --bb That "r for real!" was joke. What i mean is a literal/template/p

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 12:20:38 +0200, so wrote: I haven't seen a single C++ library able to do this properly. (I would just copy it!) This is one of the reasons why something like std::numeric_limits::function() exists. Which makes a generic and *clean* numeric code in C++ nonexistent. Don!

Re: generic + numeric + literals = abomination

2010-03-27 Thread Bill Baxter
Note that 'real' is a built in type in D. It's an 80-bit float on x86 procs and 64-bit elsewhere. So .5L is like cast(real).5. Not the solution you were looking for. --bb 2010/3/27 so : > On Sat, 27 Mar 2010 15:28:22 +0200, bearophile > wrote: > >> so: >>> >>> One thing i can think of now is a

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 15:28:22 +0200, bearophile wrote: so: One thing i can think of now is adding another float literal, maybe 'r', for "real"!, See here, Unfortunately it's called "L" not "r": http://www.digitalmars.com/d/2.0/lex.html FloatSuffix: f F RealSuffix:

Re: generic + numeric + literals = abomination

2010-03-27 Thread bearophile
so: > One thing i can think of now is adding another float literal, maybe 'r', > for "real"!, See here, Unfortunately it's called "L" not "r": http://www.digitalmars.com/d/2.0/lex.html FloatSuffix: f F RealSuffix: L bearophile

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 13:52:28 +0200, bearophile wrote: Can you explain better what the problem is and what kind of solution you would like? Bye, bearophile One thing i can think of now is adding another float literal, maybe 'r', for "real"!, Which means you are free to cast this to any

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 13:32:24 +0200, Robert Clipsham wrote: On 27/03/10 10:20, so wrote: In C++! I have a type defined in the core library like.. typedef float scalar; //typedef double scalar; // <-- whole framework is now double precision alias float scalar; //alias double scalar; Next

Re: generic + numeric + literals = abomination

2010-03-27 Thread so
On Sat, 27 Mar 2010 13:52:28 +0200, bearophile wrote: so: Since D is superb, i like to know how you do it in D. If you got a better idea in C++, i would like to hear that too! You know there are float literal too, in C++/D, like: 5.5f I don't think D can help you more than C++ here. Can yo

Re: generic + numeric + literals = abomination

2010-03-27 Thread bearophile
so: > Since D is superb, i like to know how you do it in D. > If you got a better idea in C++, i would like to hear that too! You know there are float literal too, in C++/D, like: 5.5f I don't think D can help you more than C++ here. Can you explain better what the problem is and what kind of solu

Re: generic + numeric + literals = abomination

2010-03-27 Thread Robert Clipsham
On 27/03/10 10:20, so wrote: In C++! I have a type defined in the core library like.. typedef float scalar; //typedef double scalar; // <-- whole framework is now double precision alias float scalar; //alias double scalar; Next i instantiate vectors, matrices etc... from templates. typedef

generic + numeric + literals = abomination

2010-03-27 Thread so
In C++! I have a type defined in the core library like.. typedef float scalar; //typedef double scalar; // <-- whole framework is now double precision Next i instantiate vectors, matrices etc... from templates. typedef vector_t vector; typedef matrix_t matrix; Until now everything cool, here pa