Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I shorten a function name I use repeatedly?

Tags:

c

formula

I have a long formula, like the following:

float a = sin(b)*cos(c)+sin(c+d)*sin(d)....

Is there a way to use s instead of sin in C, to shorten the formula, without affecting the running time?

like image 655
Ben Avatar asked Jan 23 '19 14:01

Ben


People also ask

Should function name be long?

A function name is too long when it starts to either over-describe what it does, or when it hinders readability of the code. Well put. Keep the names clear and concise but still descriptive.

How long should a function name be Python?

By convention, variable names start with a lower case character. The easiest way to make sure you are not adding lines longer than 80 characters wide is to always work inside a window that is exactly 80 characters wide. If your line starts wrapping around to the next line, its too long.

How do you know when a function is too long?

Essentially, if your function is larger than what fits on your screen at one time then your function is too long. There should be no scrolling required. There are some benefits to keeping your functions at this length: It is easier to see the algorithm as a whole when you can see it all together.

What happens if the JavaScript function is too long?

Solution(By Examveda Team) The client-side JavaScript functions must not run too long: otherwise they will tie up the event loop and the web browser will become unresponsive to user input.


3 Answers

There are at least three options for using s for sin:

Use a preprocessor macro:

#define s(x) (sin(x))
#define c(x) (cos(x))
float a = s(b)*c(c)+s(c+d)*c(d)....
#undef c
#undef s

Note that the macros definitions are immediately removed with #undef to prevent them from affecting subsequent code. Also, you should be aware of the basics of preprocessor macro substitution, noting the fact that the first c in c(c) will be expanded but the second c will not since the function-like macro c(x) is expanded only where c is followed by (.

This solution will have no effect on run time.

Use an inline function:

static inline double s(double x) { return sin(x); }
static inline double c(double x) { return cos(x); }

With a good compiler, this will have no effect on run time, since the compiler should replace a call to s or c with a direct call to sin or cos, having the same result as the original code. Unfortunately, in this case, the c function will conflict with the c object you show in your sample code. You will need to change one of the names.

Use function pointers:

static double (* const s)(double) = sin;
static double (* const c)(double) = cos;

With a good compiler, this also will have no effect on run time, although I suspect a few more compilers might fail to optimize code using this solution than than previous solution. Again, you will have the name conflict with c. Note that using function pointers creates a direct call to the sin and cos functions, bypassing any macros that the C implementation might have defined for them. (C implementations are allowed to implement library function using macros as well as functions, and they might do so to support optimizations or certain features. With a good quality compiler, this is usually a minor concern; optimization of a direct call still should be good.)

like image 126
Eric Postpischil Avatar answered Oct 03 '22 21:10

Eric Postpischil


if I use define, does it affect runtime?

define works by doing text-based substitution at compile time. If you #define s(x) sin(x) then the C pre-processor will rewrite all the s(x) into sin(x) before the compiler gets a chance to look at it.

BTW, this kind of low-level text-munging is exactly why define can be dangerous to use for more complex expressions. For example, one classic pitfall is that if you do something like #define times(x, y) x*y then times(1+1,2) rewrites to 1+1*2, which evaluates to 3 instead of the expected 4. For more complex expressions like it is often a good idea to use inlineable functions instead.

like image 39
hugomg Avatar answered Oct 03 '22 21:10

hugomg


Don't do this.

Mathematicians have been abbreviating the trigonometric functions to sin, cos, tan, sinh, cosh, and tanh for many many years now. Even though mathematicians (like me) like to use their favourite and often idiosyncratic notation so puffing up any paper by a number of pages, these have emerged as pretty standard. Even LaTeX has commands like \sin, \cos, and \tan.

The Japanese immortalised the abbreviations when releasing scientific calculators in the 1970s (the shorthand can fit easily on a button), and the C standard library adopted them.

If you deviate from this then your code immediately becomes difficult to read. This can be particularly pernicious with mathematical code where you can't immediately see the effects of a bad implementation.

But if you must, then a simple

static double(*const s)(double) = sin;

will suffice.

like image 32
Bathsheba Avatar answered Oct 03 '22 22:10

Bathsheba