Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 27, 2010 generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
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<scalar, 3> vector;
typedef matrix_t<scalar, 3, 3> matrix;
Until now everything cool, here pain comes...
const scalar a = scalar(0.2) * math::consts<scalar>::pi; // can't drop cast, since 0.2 is double
const scalar b = a * scalar(0.9) + scalar(5); // " "
const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error
...
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!
Thanks!
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
|
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | 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 vector_t<scalar, 3> vector; > typedef matrix_t<scalar, 3, 3> matrix; alias Vector!(scalar, 3) vector; // Presuming you have defined // the Vector!() template somewhere alias Matrix!(scalar, 3, 3) matrix; // Presuming Matrix!() is defined > Until now everything cool, here pain comes... > > const scalar a = scalar(0.2) * math::consts<scalar>::pi; // can't drop > cast, since 0.2 is double const scalar a = 0.2 * PI; // PI is defined in std.math > const scalar b = a * scalar(0.9) + scalar(5); // " " const scalar b = a * 0.9 * 5.0; > const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error const vector v = vector(8.0) * 3.0; > ... > > 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! > > Thanks! > There are a few vector implementations for D out there, or you can roll your own. I'm pretty sure there's matrices out there too, I haven't checked though (I'm pretty sure D's built in arrays will do the trick, I'm not an expert though). |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | 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 solution you would like?
Bye,
bearophile
|
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sat, 27 Mar 2010 13:52:28 +0200, bearophile <bearophileHUGS@lycos.com> 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 you explain better what the problem is and what kind of solution you would like? > > Bye, > bearophile You are right, i guess i wasn't clear enough. In 5.5f, f is the float literal, i am trying to write generic code. Say i changed the type to f64(double), or f128 by just changing a line. typedef f128 scalar; // or in D alias f128 scalar; So each cast above will be like f128(5.5f)... What have i gained by increasing precision to 64 or 128 bit here? Clear enough? :) Thanks! -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | On Sat, 27 Mar 2010 13:32:24 +0200, Robert Clipsham <robert@octarineparrot.com> 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 i instantiate vectors, matrices etc... from templates. >> typedef vector_t<scalar, 3> vector; >> typedef matrix_t<scalar, 3, 3> matrix; > > alias Vector!(scalar, 3) vector; // Presuming you have defined > // the Vector!() template somewhere > alias Matrix!(scalar, 3, 3) matrix; // Presuming Matrix!() is defined > >> Until now everything cool, here pain comes... >> >> const scalar a = scalar(0.2) * math::consts<scalar>::pi; // can't drop >> cast, since 0.2 is double > > const scalar a = 0.2 * PI; // PI is defined in std.math > >> const scalar b = a * scalar(0.9) + scalar(5); // " " > > const scalar b = a * 0.9 * 5.0; > >> const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error > > const vector v = vector(8.0) * 3.0; > >> ... >> >> 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! >> >> Thanks! >> > > There are a few vector implementations for D out there, or you can roll your own. I'm pretty sure there's matrices out there too, I haven't checked though (I'm pretty sure D's built in arrays will do the trick, I'm not an expert though). Even the example looks/sounds simple, i am not that new in C++/D programming :) If one comes to D from C++, he is most likely pushing limits of the C++ templates/numerics. PI was just an example, i could find another constant, but then you would point me to a SI unit implementation. :) For : const scalar a = 0.2 * PI; // both double implicit cast to scalar, when it is float, this must produce a warning? const scalar b = a * 0.9 * 5.0; // same const vector v = vector(8.0) * 3.0; // 3.0 is a double, when scalar is float, this line must not compile I think you should try it first, DMD should also give warnings/errors here. Thanks! -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sat, 27 Mar 2010 13:52:28 +0200, bearophile <bearophileHUGS@lycos.com> 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 floating point suitable. Some kind of template, i don't know! So when i have something like this : scalar m = 0.99999999999999999999999999r; when scalar is f32 : scalar m = 0.99999999999999999999999999f; when scalar is f64 : scalar m = 0.99999999999999999999999999; ... And it is easy to detect for the compiler i guess. scalar m = 0.2r; // just cast it to scalar, user wants to assign to a scalar scalar n = m * 4.5r; // again just cast it to scalar, user wants to multiply by a scalar. ... Thanks :) -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | 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 |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sat, 27 Mar 2010 15:28:22 +0200, bearophile <bearophileHUGS@lycos.com> 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: > L > > bearophile Yes, it says : "Floating literals with no suffix are of type double. Floats can be followed by one f, F, or L suffix. The f or F suffix means it is a float, and L means it is a real." Problem remains, i think you either lost me or just didn't read what i wrote, or well.. most probably i am still not clear enough. -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | On Sat, 27 Mar 2010 12:20:38 +0200, so <so@so.do> 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<type>::function() exists. Which makes a generic and *clean* numeric code in C++ nonexistent. Don! Please enlighten us! (especially me...) *begs* -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 27, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | 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 <so@so.do>:
> On Sat, 27 Mar 2010 15:28:22 +0200, bearophile <bearophileHUGS@lycos.com> 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:
>> L
>>
>> bearophile
>
> Yes, it says :
> "Floating literals with no suffix are of type double. Floats can be followed
> by one f, F, or L suffix. The f or F suffix means it is a float, and L means
> it is a real."
>
> Problem remains, i think you either lost me or just didn't read what i
> wrote,
> or well.. most probably i am still not clear enough.
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
>
|
Copyright © 1999-2021 by the D Language Foundation