r/C_Programming • u/bricxy • Jun 09 '26
Preprocessor macros for math SUM()
Hi,
I'm a student that tries to do some linear algebra in C and trying to make a library for fun, and I tried to do a macro for summations in C, to make it simpler for me to write from my courses to my code
I'm not sure if it is the exact subreddit to post on (quite new to post on reddit), but I wanted to know if my macro could be a good representation of summations in mathematics and if it could have edge cases where this macro could fail
usually I'm using for loops whenever I see the need to use summations in mathematics, but I wanted to try making a macro for it since I'm starting to see summations more and more in my courses. And I couldn't find a SUM() macro or function in the math.h library
Here is what it looks like :
#define SUM(var, start, end, step, func) ( \
{ \
float total = 0; \
for (size_t var = start; var <= end; var += step) \
total += func; \
total; \
})I'm using var as the summation index, start as the value of the starting index, end as the final index and func as the expression of the summation
I'm using it for basic summations with arrays like for magnitude of vectors (algebra)
This is how I use it for magnitude of vectors with any cardinality :
typedef float vec;
float vec_magnitude(vec *vector, size_t cardinality)
{
return sqrtf(SUM(i, 1, cardinality, 1, powf(vector[i - 1], 2.0f)));
}What do you think ?
3
u/SmokeMuch7356 Jun 09 '26 edited Jun 09 '26
That should be a function, not a macro:
Instead of using
<=with doubles, create a comparison functionleqthat will returntrue(1) ifiis stricly less than or within some epsilon ofendthat's small enough to consider them equal,false(0) otherwise.fis a pointer to a function that evaluates the expression; for example, to sum the expression x2 from 1 to 10, you'd create a squaring function:then call
sumpassing the function name as a parameter:8
u/dmc_2930 Jun 09 '26
Ummm that should not be a macro. Make it a function if anything.
2
u/un_virus_SDF Jun 09 '26
Why did you put parenthesis around your braces in the macro?, they're often wrapped in q do {}while(0) to enforce semicolon but I never saw it with parenth (except when returning a value, what you're not doing).
Then c is not a functional programming language, the
totalat the end of your macro is useless. And it's not possible to do sum as you intend without making it a function (with maybe a macro wrapper to autocomplete extra info if needed)You can for exemple change it in
And if you really need closure as you did before, you can have a static inlined function. This would omprove code readability and debuging.
Instead of wrappinc a for loop in a macro, write it, it will me clearer.
On a other side do
x*xinstead ofpowf(x, 2.)it avoid a useless function call.Also avoid weird tricks to start arrays at 1. They start at 0, so instead of aving all those offset by one (in the SUM macro call) do
SUM(i, 0, n, f(i))by putting exclusive ranges on end (< rather than <=)That's because it would be a really heavy function where a written for loop would be way faster and modulable.
If you just want to do linalg take a look at fortran. There are a lot of builtins for that, including
sumnormand a lot of array operations (pythons numpy is written in fortran). Despite being a bit old and having some pitfalls, it can be easier to use that c.A way to make your macro work would be
And then you could do
1
u/spocchio Jun 09 '26
I think without the macro would be more clear
1
u/markuspeloquin Jun 10 '26
It might not seem natural at first, but half-open intervals are a better way of expressing ranges. So
var < endin your case. It makes sense when you consider that the length is (end-begin), and that you can specify an empty interval by having begin=end.To reference other libraries, c++ has the slice type to represent a range within a valarray. It has members start, size, stride. (End would be start + size*stride.)
Python's stride indexing is different: start, end, stride. (Size would be (end-start)/stride.)
I prefer python's slice, but I see the sense in C++: you can avoid division when computing the end/size.