Using parentheses in formulas of "calculated question" containing a variable

Using parentheses in formulas of "calculated question" containing a variable

by jack captch -
Number of replies: 8
Hello.

I'm having a problem with saving the formula x ={=({n3}+3)*100} in the Question text area. (moodle version 2.5.3)
I get the following error:  Illegal formula syntax starting with '({n3}+3)*100'
Could you please advise me how to enter the formula?
Thanks in advance.
Average of ratings: -
In reply to jack captch

Re: Using parentheses in formulas of "calculated question" containing a variable

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Try n*3 instead of n3

To quote from the docs

"

  • There is no implicit multiplication. To you, the human editor, "5(23)" or "5x" may seem perfectly obvious. To the server doing the math, it's crazy talk and won't be understood. Always use the "*" for multiplication.


(I love the use of the expression crazy talk)

Average of ratings: Useful (2)
In reply to Marcus Green

Re: Using parentheses in formulas of "calculated question" containing a variable

by jack captch -

Hi.

Thanks for your answer!

The "n3" is a variable therefore it is written this way.

I'm getting the correct calculation in case I use formula y ={=10*{n1}} (the n1 is variable as well)

In my example to make the function work I have to open parentheses: x ={={n3}*100+3*100} which is not logical and not useful.

I trued all possible ways to enter the formula containing parentheses and each time I got the syntax error.

It seems like the round parentheses are those that cause the problem. I thought maybe there should be some special latex character to create the complex formulas?

For example ## was used in 1.9 version  and there was an ability to enter my formula without opening the parentheses;

Could you please help me with it?


Thanks.

In reply to jack captch

Re: Using parentheses in formulas of "calculated question" containing a variable

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I just tested this case, and it works fine:

Question text: An equation: x = {=({n3}+3)*100}. That should be OK.

Answer: {n3}

(This is a stupid question, I realise, but I was just testing.) I am using Moodle 2.8.1 here.

I suspect the change that made this work was https://moodle.org/mod/forum/discuss.php?d=264266. You need to upgrade.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Using parentheses in formulas of "calculated question" containing a variable

by jack captch -

Thanks for your answer.

I use  2.7.1 (Build: 20140714)  - the version that is marked as fixed; and it is still reproducible there.

So you suggest to update my moodle version?


In reply to jack captch

Re: Using parentheses in formulas of "calculated question" containing a variable

by Patrick Cheeseman -

Jack, a year has past since you final post fell on deaf ears - I'm hoping you may have managed to find a solution? As you might expect, I'm stuck at present trying to get around the same error message for a formula that worked fine until I substituted an explicit value (15) with a variable {A1}. Can you spot anything obvious I might be doing wrong?

Illegal formula syntax starting with '((((((-(0.45))/6)*(pow((9.539),3)))+({A1}*(pow((9.539),2))))+((0)*(9.539)))+(0))'

In reply to Patrick Cheeseman

Re: Using parentheses in formulas of "calculated question" containing a variable

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you remove redundant brackets from that, I think you get to

-0.45/6*pow(9.539,3)+{A1}*pow(9.539,2)+0*9.539+0

which is more readable, and seems fine.

I don't know why that is failing to validate. I just checked the code, and it seems fine.

Can you try some alternate variable names, like a1 or aa, to see if they work any better? (A1 should be an allowed variable name, but I think it is worth trying some others to see if it makes a difference.)


In reply to Tim Hunt

Re: Using parentheses in formulas of "calculated question" containing a variable

by Pierre Pichet -

However I got

Set 1  10*{A1}


-0.45/6*pow(9....
10*5.8 = 58.00
Correct answer : 58.00 inside limits of true value
Min: 57.419999999999 --- Max: 58.580000000001
-0.45/6*pow(9.539,3)+5.8*pow(9.539,2)+0*9.539+0 = 462.66
Correct answer : 462.66 inside limits of true value
Min: 458.03171448893 --- Max: 467.28488043822
on Moodle 3.0dev (Build: 20151008)


In reply to Tim Hunt

Re: Using parentheses in formulas of "calculated question" containing a variable

by Patrick Cheeseman -

Hi Tim,

Thanks for the advise about removing redundant brackets. They had been 'brought across' with a Respondus import. 

We found we were able to prevent the error message being displayed by reverting back to the constant value originally used in the formula. In other words, we decided against substituting a value for a variable as it seemed highly likely that introducing a variable after the question had been created could be problematic.... particularly as we had un-intentionally created a shared dataset for the variable, making it a global variable not declared at the outset. Does that seem like a reasonable assumption to you?