468,535 Members | 1,528 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,535 developers. It's quick & easy.

Stupid or clever hack?

Is this a stupid hack or a clever hack?
Is it a "hack" at all?

====================

#include <stdio.h>

/* !!HACK!! */
/* no parenthesis on the #define'd expression */
#define MY_FLOAT_CONSTANT_HACKED 15 / 4

int main(void)
{
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED); /* !!
HACK!! */
return 0;
}

================

Is it so much better to do it the proper way, which implies a few type
conversions, implicit and explicit?

#define MY_CONSTANT 3.75
printf( "%d\n", (int)(100 * MY_CONSTANT) );

Thanks
Jun 27 '08 #1
7 1959
ba******@gmail.com wrote:
Is this a stupid hack or a clever hack?
Is it a "hack" at all?

====================

#include <stdio.h>

/* !!HACK!! */
/* no parenthesis on the #define'd expression */
#define MY_FLOAT_CONSTANT_HACKED 15 / 4

int main(void)
{
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED); /* !!
HACK!! */
return 0;
}

================

Is it so much better to do it the proper way, which implies a few type
conversions, implicit and explicit?

#define MY_CONSTANT 3.75
printf( "%d\n", (int)(100 * MY_CONSTANT) );
Why not
#define MY_FLOAT_CONSTANT (15.0/4.0)

Bye, Jojo
Jun 27 '08 #2
On Jun 10, 7:25 pm, badc0...@gmail.com wrote:
#define MY_FLOAT_CONSTANT_HACKED 15 / 4

100 * MY_FLOAT_CONSTANT_HACKED
( 100 * MY_FLOAT_CONSTANT_HACKED ) != ( MY_FLOAT_CONSTANT_HACKED *
100 )

which is contrary to what most people would expect, and so is a very
bad idea if anyone else has to read the code, or even if you want to
be able to read it in six months time.

That is not to say that rational number multiplication is not a good
substitute for floating point multiplication in some circumstances,
but I would suggest that you do it like this:

#define MY_FLOAT_MULT(i) ( ( (i) * 15 ) / 4 )

printf( "The value is %d.\n", MY_FLOAT_MULT(100) );

HTH

viza
Jun 27 '08 #3
On Jun 10, 11:25*am, badc0...@gmail.com wrote:
Is this a stupid hack or a clever hack?
Is it a "hack" at all?

====================

#include <stdio.h>

/* !!HACK!! */
/* no parenthesis on the #define'd expression */
#define MY_FLOAT_CONSTANT_HACKED 15 / 4

int main(void)
{
* printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED); /* !!
HACK!! */
* return 0;

}

================

Is it so much better to do it the proper way, which implies a few type
conversions, implicit and explicit?

#define MY_CONSTANT 3.75
printf( "%d\n", (int)(100 * MY_CONSTANT) );
My opinion is that it is an utter piece of garbage.

An unparenthesized macro is a disaster waiting to happen.

This is ambiguous and mildly misleading as to what is intended (after
all, MY_FLOAT_CONSTANT_HACKED is NOT a float constant and a float
constant would promote the 100 and hence %d would be the wrong
printf() specifier):
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED);
This is clear:
printf("The value is %d.\n", 100 * 15 / 4);
so what is achieved by the macro is minor obfuscation and the
introduction of a dangerous construct.

Don't do that.
IMO-YMMV
Jun 27 '08 #4
viza wrote:
On Jun 10, 7:25 pm, badc0...@gmail.com wrote:
#define MY_FLOAT_CONSTANT_HACKED 15 / 4
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED);
[...]
#define MY_FLOAT_MULT(i) ( ( (i) * 15 ) / 4 )

printf( "The value is %d.\n", MY_FLOAT_MULT(100) );

HTH
It helps a lot. Thank you.

Thank you for your and the other posters comments too.
Jun 27 '08 #5
ba******@gmail.com writes:
Is this a stupid hack or a clever hack?
Is it a "hack" at all?

====================

#include <stdio.h>

/* !!HACK!! */
/* no parenthesis on the #define'd expression */
#define MY_FLOAT_CONSTANT_HACKED 15 / 4

int main(void)
{
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED); /* !!
HACK!! */
return 0;
}

================
It's a stupid hack.
Is it so much better to do it the proper way, which implies a few type
conversions, implicit and explicit?

#define MY_CONSTANT 3.75
printf( "%d\n", (int)(100 * MY_CONSTANT) );
With your version of the macro, the expression

100 * MY_FLOAT_CONSTANT_HACKED

expands to

100 * 15 / 4

which is really

(100 * 15) / 4

Using a macro to violate operator precedence is a bad idea.

What would make sense is to put the entire expression into a macro:

#define WHATEVER (100 * 15 / 4)

Or, if you want to parameterize it, perhaps something like this:

#define WHATEVER(x, y) (100 * (x) / (y))

BTW, here's my example of How Not To Use Macros (designed for Douglas
Adams fans):

================================
#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}
================================

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
user923005 wrote:
My opinion is that it is an utter piece of garbage.

An unparenthesized macro is a disaster waiting to happen.

This is ambiguous and mildly misleading as to what is intended (after
all, MY_FLOAT_CONSTANT_HACKED is NOT a float constant and a float
constant would promote the 100 and hence %d would be the wrong
printf() specifier):
printf("The value is %d.\n", 100 * MY_FLOAT_CONSTANT_HACKED);
This is clear:
printf("The value is %d.\n", 100 * 15 / 4);
That *is* clear.
I've written code that way.
so what is achieved by the macro is minor obfuscation and the
introduction of a dangerous construct.

Don't do that.
IMO-YMMV

--
pete
Jun 27 '08 #7
On Jun 11, 12:26 am, Keith Thompson <ks...@mib.orgwrote:
>
BTW, here's my example of How Not To Use Macros (designed for Douglas
Adams fans):

================================
#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;}

================================
:-)
Ahh.. now I realise. There was this same bug in Deep Thought's code.

Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by Xah Lee | last post: by
16 posts views Thread by Justin Hoffman | last post: by
36 posts views Thread by Hoopster | last post: by
30 posts views Thread by Skybuck Flying | last post: by
14 posts views Thread by UJ | last post: by
5 posts views Thread by Nmx | last post: by
6 posts views Thread by Malvolio | last post: by
reply views Thread by Xah Lee | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.