473,513 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2245
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
2246
by: Xah Lee | last post by:
is it possible to write python code without any indentation? Xah xah@xahlee.org http://xahlee.org/PageTwo_dir/more.html
16
2059
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
36
2814
by: Hoopster | last post by:
Hello, I know nothing about C++ but want to get started. Is there any good free C++ program that I can try to see if I like programming? I also need a good free compiler. I don't want to...
30
2149
by: Skybuck Flying | last post by:
I was just trying to figure out how some C code worked... I needed to make a loop to test all possible values for a 16 bit word. Surprise Surprise... C sucks at it... once again :D lol... C is...
14
1398
by: UJ | last post by:
What's the easiest way (without setting up a style sheet) to set the font for an area of text. I need to be able to specify the exact font and font size - not the 1-7 numbers IE uses. I want to be...
5
2223
by: Nmx | last post by:
Hi everyone, I'm writing a patch to a search engine (aspseek http://www.aspseek.org/) compile under gcc 3.4.4 on FC3. At some point, I found this piece of code: -- // Dirty hack to avoid...
6
1504
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the...
0
2072
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
0
3683
by: freehackers | last post by:
FreeHackers Group : Only 6 Steps to get cracked your target password 1- Fill in the E-Mail Cracking order form , to the best of your knowledge “contact us to freehackers.007gmail.com with...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.