Re: Use a suffix or a type cast?
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Sat, 08 Jul 2006 14:27:25 +0200
Ben C schrieb:
On 2006-07-06, Hamish M <hammer1234@xxxxxxxxxxxxxx> wrote:
Hi I am interested in opinions on this topic.
I have heard that a suffix is not a good solution and type casts are
much better
for example.
-----------------------------------------------------------------
#define MAX_UWORD (T_UWORD)65535
or
#define MAX_UWORD 65535u
-------------------------------------------------------------------
Where UWORD is unsigned short int.
What is your opinion? or why would someone have said using a suffix is
no good?
I once worked on a library where we used a lot of doubles, and wrote
constants of the kind:
#define ONE 1.0
We already had a typedef:
typedef double Real;
Then we ported to a machine with fast single precision fp, but slow
software-only doubles, so we changed the typedef:
typedef float Real;
So far so good. But whenever we used the constants, since they were
double precision, we ended up with everything being promoted to double
and a lot of slow software double-precision computation which we didn't
want.
What we needed of course was:
#define ONE 1.0f
But much easier than faffing with macros to try and achieve that was:
#define ONE ((Real) 1.0)
Now all the constants automatically pick up the same type as the
typedef.
So, I'd say, when you want a constant of a type that you want to
typedef, it works well to use a cast in the macro rather than a suffix.
The alternative: Whenever you create a typedef for a numeric type,
also provide the appropriate <TYPE>_C macro.
With
typedef float Real;
#define REAL_C(constant) constant##F
your symbolic constant is defined as
#define ONE REAL_C(1.0)
If you need the printf() family, appropriate conversion
and length modifier plus conversion macros should be defined
as well.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- References:
- Use a suffix or a type cast?
- From: Hamish M
- Re: Use a suffix or a type cast?
- From: Ben C
- Use a suffix or a type cast?
- Prev by Date: Re: Time to ask?
- Next by Date: Re: Function declarations - why are they needed?
- Previous by thread: Re: Use a suffix or a type cast?
- Next by thread: Easy question about redefining a function
- Index(es):
Relevant Pages
|
Loading