473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preprocessor macros

Hi,

If I define a macro as follows

#define COSTHETA cos(45.0*3.1415 9256/180.0)

I believe that any time COSTHETA appears it is simply replaced by the
defined text, which results in the cos(...) being calculated in my program
every time. Is there a way to get the preprocessor to calculate the cosine
and just replace COSTHETA with its final number? I know I could define a
const variable but I just want the final number.

Thanks for any help.

Jay
Jul 23 '05 #1
4 1963
"Jay Wolfe" <ja*****@adelph ia.net> wrote in message
news:WY******** ************@ad elphia.com...
Hi, Hi.
If I define a macro as follows

#define COSTHETA cos(45.0*3.1415 9256/180.0)

I believe that any time COSTHETA appears it is simply replaced by the defined text, which results in the cos(...) being calculated
in my program every time. Is there a way to get the preprocessor to calculate the cosine and just replace COSTHETA with its final
number? I know I could define a const variable but I just want the final number.


In C++, it would better to write something like:
static const double CosineEightTurn = cos(45.0*3.1415 9256/180.0);
Using macros for constants is bad form in C++.

--
--Larry Brasfield
email: do************* **********@hotm ail.com
Above views may belong only to me.
Jul 23 '05 #2
Jay Wolfe wrote:
Hi,

If I define a macro as follows

#define COSTHETA cos(45.0*3.1415 9256/180.0)

I believe that any time COSTHETA appears it is simply replaced by the
defined text, which results in the cos(...) being calculated in my program
every time. Is there a way to get the preprocessor to calculate the cosine
and just replace COSTHETA with its final number? I know I could define a
const variable but I just want the final number.

The preprocessor knows nothing about cos and can't really do anything
with floating point. There's not even a guarantee that the expression that
is the arg to your cos call is going to be evaluated at compile time
(the language makes no constraints on being able to do floating point
math, just integral math).

The const variable is the way to go, hoping that the compiler is smart
enough to do something optimal with it. Of course if the above is literally
what your interested in, you don't need to call cos at all. The answer is .7071..
Jul 23 '05 #3
Jay Wolfe wrote:


If I define a macro as follows

#define COSTHETA cos(45.0*3.1415 9256/180.0)

I believe that any time COSTHETA appears,
it is simply replaced by the defined text
which results in the cos(...) being calculated in my program every time.
Not necessarily.
Is there a way to get the preprocessor to calculate the cosine
and just replace COSTHETA with its final number?
No. But a good optimizing compiler should be able to do it for you.
I know I could define a const variable but I just want the final number.
Notice that cos(45.0*3.1415 9256/180.0) == sqrt(0.5).
If your compiler won't calculate cos(45.0*3.1415 9256/180.0),
it may still calculate sqrt(0.5):
cat costheta.cc #include <math.h>

double costheta(void) {
return sqrt(0.5);
}
g++ -Wall -ansi -pedantic -O3 -S costheta.cc
cat costheta.s

.file "costheta.c c"
.section .rodata.cst8,"a M",@progbits ,8
.align 8
.LC0:
.long 1719614413
.long 1072079006
.text
.align 2
.p2align 4,,15
.globl _Z8costhetav
.type _Z8costhetav, @function
_Z8costhetav:
.LFB5:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
popl %ebp
fldl .LC0
ret
.LFE5:
.size _Z8costhetav, .-_Z8costhetav
.section .note.GNU-stack,"",@progb its
.ident "GCC: (GNU) 3.4.1"
Jul 23 '05 #4

"Jay Wolfe" <ja*****@adelph ia.net> wrote in message
news:WY******** ************@ad elphia.com...
Hi,

If I define a macro as follows

#define COSTHETA cos(45.0*3.1415 9256/180.0)

I believe that any time COSTHETA appears it is simply replaced by the
defined text, which results in the cos(...) being calculated in my program
every time. Is there a way to get the preprocessor to calculate the cosine
and just replace COSTHETA with its final number? I know I could define a
const variable but I just want the final number.

Thanks for any help.


#include <math.h>

#define COSTHETA (M_SQRT2 / 2)

should also work (even though M_SQRT2 is not part of the Standard, it is
commonly defined). BTW, the value of pi you're using is inaccurate and will
give poor results. It also has the last two digits transposed. Use as many
digits as the precision allows, this one will work a bit better:
3.1415926535897 9323846, or even better, use M_PI if your math.h has it.

Best is to use a precalculated PI that is accurate to within one bit:
0x1.921fb54442d 1846ap+1L has 64 bits of mantissa.

If you want to use angles other than 45 degrees, an easy solution is to
write a simple C program that calculates the values, and then prints the
results out in a .c program which is then compiled into the application. I
use this all the time to generate complicated, but fixed, tables that are
way beyond what the preprocessor can do.

-Walter
www.digitalmars.com free C, C++, D compilers
"code of the nerds"
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5711
by: Bram Stolk | last post by:
Hi there, What could I use to parse CPP macros in Python? I tried the Parnassus Vaults, and python lib docs, but could not find a suitable module. Thanks, Bram
25
9131
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
4
1698
by: Jakob Simon-Gaarde | last post by:
Some project includes files from different libraries lib1,lib2 and lib3 all having each there own version header file. I need to be able to pick up these values in a single define value DEP_PROJECT. pseudo preprocessor code would look like this: 1. Lib1 version header is included: #define PROJ_NAME "Lib1" #define PROJ_VERSION 102
205
10686
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
4
1951
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
13
3587
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the amount of preprocessor directives used in these code. Can anyone please tell me how to actually use these preprocessor directives in a more professional way like selecting particular lines of code suited for some particular hardware etc...
3
2020
by: Avin Patel | last post by:
Hi, I have written C# code. And I have used preprocessor / macro in it. I want to get the C# code after processing preprocessor, I have defined durcng compile time. How can I get this code? Is there any csc compiler option, that can produce the code after processing the macros/preprocessors? Thank you,
2
1624
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We now compile file1.c , make it's object file. and use this object file in the other file i.e file2.c. Then can we use the macro TEST1 in file2.c as well. As I understand about macros, they are replaced by their corresponding values , but are the...
6
2151
by: Nicola Mezzetti | last post by:
Greetings, I write to ask information about how to disable the preprocessor checks on macros when compiling with command line Borland C++ compiler. Waiting for a reply, i thank you all for the kindness and availability. Best regards, nicola --
5
4001
by: _dwin | last post by:
Hi, Does anyone know how to implement your own preprocessor directive? For instance, I would like to have a directive which goes like: #<directive_name<parameters, ...> ie. #compress input_file output_file Thanks.
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.