javascript - How do I calculate an JavaScript expression called inside a string? - calculator

I am creating a simple calculator in HTML and I've created an input field. You can type there an sum and it must be calculated. Only, I don't know how...
Example:
I have a string:
234-231*Math.sqrt(9)
Is there a way to calculate this sum inside the string using javascript?

Use the eval() function.
Like so:
eval("234-231*sqrt(9)");

Related

Call a kdb function passing another function as argument using sendSync method of qpython(kdb)

In the KDB server, we have two functions defined as
q)t:{0N!x[`min]; 0N!x[`max];}
q).up.map:{[keyList; valueList] keyList!valueList}
The KDB server, does not allow to pass dict()!() as an argument directly to a function, rather one has to use .up.map.
Calling t function from kdb would be like
q)t[.up.map[`min`max;10 20]]
I want to call the t function from qpython sendSync() method passing another function .up.map[`min`max;10 20] as an argument to t.
Unfortunately, I cannot find a solution in the qptyhon doc - https://qpython.readthedocs.io/en/latest/qpython.html#qpython.qconnection.QConnection.sendSync
Error -
When I tried sendSync() method, below error is raised -
qpython.qtype.QException: b'['
The KDB server, does not allow to pass dict()!() as an argument directly to a function, rather one has to use .up.map.
May I know why this is so? It's not a bad idea to challenge the original design before looking for workarounds. If dictionary were allowed as its parameter, it could have been as simple as
params = QDictionary(qlist(numpy.array(["min", "max"], dtype=numpy.string_), qtype=QSYMBOL_LIST),
qlist(numpy.array([10, 20], dtype=numpy.int64), qtype=QLONG_LIST))
with qconnection.QConnection(host='localhost', port=5000) as q:
q.sendSync("t", params)
If you want to do what you can do in q console via qpython, it's actually also simple: you pass the same string over. Effectively it's the same mechanism as a q client passing a string via IPC to the server, where the string is parsed and evaluated. Here you need to convert the input to the given string format in your Python code, thus not as clean as the above (although it looks more verbose).
with qconnection.QConnection(host='localhost', port=5000) as q:
q.sendSync("t[.up.map[`min`max;10 20]]")
Maybe you can use a lambda for this. That way it's just the arguments that need be serialized:
q.sendSync("{t[.up.map[x;y]]}", qlist(["min", "max"], qtype=QSYMBOL_LIST), [10, 20])
If that's not permitted, you could create it as a named wrapper function on the kdb side, which could be.
Alternatively, you could format your call with arguments as a string. A bit hacky; but workable for simple input.
q.sendSync(f"t[.up.map[`{'`'.join(['min', 'max'])};{' '.join(['10', '20'])}]]")

kdb/q: apply the function, pass the return value to the function again, multiple rounds

I have a list of symbols, say
`A`B`C
. I have a table tab0; A function that takes in a table plus a string as arguments.
tab1: f[tab0;`A]
tab2: f[tab1;`B]
tab3: f[tab2;`C]
I only care about the final values. But my list of symbols can be long and can have variable length, so I don't want to hardcode above. How do I achieve it?
I think it has something to do with https://code.kx.com/q/ref/accumulators/ but I really struggle to figure out the syntax.
This is exactly the use case for the binary application of over (/) (https://code.kx.com/q/ref/accumulators/#binary-application)
So you should use:
f/[tab0;`A`B`C]

simple math issue

I'm trying to modify an existing JasperReports template for an invoice. I would like to do some simple math in one field, i.e. calcultate the tax from an existing gross value. The gross value is represented by a parameter $P{prevAdvanceLine1Sum} and in the next field I would like to simply divide this value by a number (in my case 1.23).
Can you give me the expression I should use and what expression class to set?
You can either use simple arithmetic operators (like " / " in this case. See example here: EL Basic Arithmetic), or use the pre-defined methods provided by iReports. Which means, right-click upon the TextField and select Edit Expression. Then select the divide() method.
So the two methods would be:
1. $P{prevAdvanceLine1Sum / 1.23}
2. $P{prevAdvanceLine1Sum}.divide( 1.23 )

Using a parameter value as a CDate() function input

I have a parameter {?Calendar Year} that takes on year values (2014,2015, etc.) that I would like to use in a formula. I would like it to function how one might think this would work:
{table.date}>CDate({?Calendar Year},01,01).
Does anyone know how I can accomplish this?

Using varargin when passing argument into a function in Matlab?

I have a function called viewcsi(varargin) and I want to pass in three variables at most.
The first is a MBSspectrum class I made and then a string and also a number.
viewcsi is a call back, it gets called like this:
...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi)');
sp_viewcsi is the MBSspectrum class I made and is in the workspace. I want to be able to add another argument called counter which is integer of type double.
I want to do something like this:
...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi, counter)');
or
...'ButtonDownFcn', {#viewcsi, 'pickvox_cb', 'sp_viewcsi', counter)');
But when I do the last two thing these do not work since they do not preserve 'sp_viewcsi' as a class but treats it like a string. What can I do to fix this? I have a feeling its something easy but I havent been able to figure it out.
The ButtonDownFcn will only ever pass it two arguments. You can cheat it by saying
...'ButtonDownFcn',#(a,b)viewcsi(a,b, counter));
so that the callback will pass it a and b, while Matlab will hand it the current value of counter.
See also the doc on passing extra parameters.