Proektsoftbg / Calcpad

Free and open source software for mathematical and engineering calculations.
https://calcpad.eu
MIT License
379 stars 46 forks source link

Conflict Between String Variable and Macro Parameter #386

Open strsthapa opened 1 week ago

strsthapa commented 1 week ago

string and macro variable name

  1. when the string variable from last letter is similar to macro parameter it considers macro parameter instead of string variable.
  2. can we provide code/math within while calling function? while calling function it is only passing the last value (in this case 3 instead of 6)
  3. Can we assign output results from macro results to variable? Generally, calcpad assigns first value within macro line and shows error for non-number value.
Proektsoftbg commented 1 week ago

Hi$

  1. Unlike normal variables, macro variables and parameters are allowed to be concatenated to other text without spaces. So, it is not always obvious where the variable starts from. For the end, there is no problem because all are required to end with $. So if you have two macro variables/parameters that are ends in the same way, e.g.: b$ and ab$, it is not disambiguous what tab$ is: ta + b$ or t + ab$.

What Calcoad actually does in these cases: it parser the variable from right ($) to left and returns the first variable it finds (the fhortest one). So, just avoid variables that match letters at the end. Please use different names. There was a solution to this problem - to introcude a leading special symbol, like _b$, but it would require a lot of additional typing.

  1. Macros are perprocessed before calculations, just like C macros. As a result, the final (unwrapped) code is obtained by rewriting the variables with the actual text and only string processing is involved at this stage. So, if you pass 2*3 to x$, x$^2 first becomes 2*3^2. You can see this in the unrwapped code. Then, we calculate it using PEMDAS rule and get 2*3^2 = 2*9 = 18. To avoid that, just put brackets in code: (x$)^2. If you need this only for calculation purposes, the best way is to use actual functions, not macros: f(x) = x^2. Then you will have f(2*3) = 6^2 = 36. Macros are not intended for that.

  2. As we discussed above, macros are preprocessed by string rewriting. So, if the unwrapped code you obtain after that represents a valid computable expression, you can assing it to a variable, e.g. y = f$(x$). But this is not completely true. You will not actually assign the macro, but the code behind the macro. I think that you think about macros as equivalent to functions, but they are not. They are just a simple way to reuse repeated code. Not only equations, but also text, Html, etc.