this seems to lost (complex norm, abs)
Theodore Papadopoulo
Theodore.Papadopoulo@sophia.inria.fr
Thu Sep 20 11:10:00 GMT 2001
lfarkas@mindmaker.hu said:
> hmm, it seems I forget my numerical experiences. but the original
> problem with norm still apply (there cant be overflow and will be much
> faster).
Well, I do not understand why it cannot overflow... The original
report is for integral types which can overflow as well.
Indeed, the current implementation will not work with integer types.
That's true, and there are two problems, none being related to abs in
my opinion (I assume that abs is defined or can be defined in some
manner for all integer types):
First the norm of a complex<T> is not a T when T is integer. Would it
be allowed to change the return type with respect to the standard ??
Then T (or _Tp in the implementation) is also used internally as the
computation type, and that's why it does not work since the divisions
will almost always yield 0 (sometimes 1).
The simpler way to get something that is correct for all standard
numeric types, is to convert the return types and the last 3 line of
the current implementation to use doubles (long double would be
better but so less efficient), but that's dirty.
Another ugly solution, is to use overloading for creating norm only
for the complex<one of {float,double,long double,char,int,short,unsigned,..}>.
The better solution would be to create some traits to handle the type
promotion and use it for the return type and the computation types.
The advantage is that it would be somewhat expandable to user defined
types. But that's also more complicated... For example, what should
be the return type of norm(complex<int>) a float, a double ???
Which solution is the best with respect to the standard, I do not
know....
C arithmetic rules sometimes hurt when used with generic code...
--------------------------------------------------------------------
Theodore Papadopoulo
Email: Theodore.Papadopoulo@sophia.inria.fr Tel: (33) 04 92 38 76 01
--------------------------------------------------------------------
--
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Exmh version 2.2 06/23/2000
iD8DBQE7qjD+IzTj8qrxOU4RAtZPAJ9qnNlA7ldsWBPSZWK4/MlGok5ZKgCghod3
tKB+taddCWfKxenNRKRGoVM=
=8sd7
-----END PGP SIGNATURE-----
More information about the Libstdc++
mailing list