bueler / p4pdes

C and Python examples from my book on using PETSc and Firedrake to solve PDEs
MIT License
180 stars 69 forks source link

initializers should not include function calls #16

Closed bueler closed 8 years ago

bueler commented 8 years ago

Constantine reports plap.c does not compile in clang. Problem is in these lines in plap.c:

static double zq[3][3] = { {0.0,NAN,NAN},
                           {-1.0/sqrt(3.0),1.0/sqrt(3.0),NAN},
                           {-sqrt(3.0/5.0),0.0,sqrt(3.0/5.0)} },
              wq[3][3] = { {2.0,NAN,NAN},
                           {1.0,1.0,NAN},
                           {5.0/9.0,8.0/9.0,5.0/9.0} };

Two changes here, and in similar: remove sqrt() function call, and (minor) irrelevant static, which I do not want to explain anyway.

bueler commented 8 years ago

After reading up at https://en.wikipedia.org/wiki/Static_(keyword), I will keep the static keyword.

Note all PETSc example codes use it for the help string, at the same scope. Indeed the issue is scope, as pointed out by @ckhroulev. That is, the wiki says

Other uses ... Static global variable: A variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage").

ckhroulev commented 8 years ago

Regarding initialization: according to the C language standard, section 6.7.9

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

And according to section 6.6 of the same document

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.

bueler commented 8 years ago

Yes, all clear. I will fix the issue here, but I will also keep the static keyword because it is harmless here.