I like the name too :8)
Monster: evaluate math expressions in strings
Started by
Laszlo
, Feb 28 2007 12:06 AM
87 replies to this topic
AHK version 1.0.46.12 introduced the long awaited scientific number formats (1.23e7). They allow using full 64 bit precision in intermediate results, so there will be no loss of accuracy when some values are too small.
Monster is digitsequence oriented, that is, each partial result is converted to a string of digits, replacing the subexpression just computed. This is why scientific number representations are so important. In fix point representation small numbers get truncated, and lose precision. Even if later they get multiplied by a large number, some least significant digits are lost.
Unfortunately, scientific number representations introduce complications in the script. Now e is not necessarily a variable or operator, and the operators + and  have 4 different meanings, dependent on the context:
 sign of a number (2)
 sign of the exponent (1.23e7)
 unary operator ( x )
 binary operator ( xy )
In version 1.1 of Monster these are handled by distinguishing early on between the different roles of + and . Internally, they get replaced by ± and ¬, respectively, when they are operators. To avoid humongous regular expressions, in the preprocessing phase scientific numbers are enclosed between ‘ and ’, which are later removed for speed and simplicity. The AHK requirement that scientific numbers must contain decimal points are lifted: 1e3 is valid.
For the users' perspective the only changes are improved precision and the support of scientific number notation, also in the output, by $6g or $9e type directives. Again, there are a lot of significant internal changes, so be careful and report problems!
Monster is digitsequence oriented, that is, each partial result is converted to a string of digits, replacing the subexpression just computed. This is why scientific number representations are so important. In fix point representation small numbers get truncated, and lose precision. Even if later they get multiplied by a large number, some least significant digits are lost.
Unfortunately, scientific number representations introduce complications in the script. Now e is not necessarily a variable or operator, and the operators + and  have 4 different meanings, dependent on the context:
 sign of a number (2)
 sign of the exponent (1.23e7)
 unary operator ( x )
 binary operator ( xy )
In version 1.1 of Monster these are handled by distinguishing early on between the different roles of + and . Internally, they get replaced by ± and ¬, respectively, when they are operators. To avoid humongous regular expressions, in the preprocessing phase scientific numbers are enclosed between ‘ and ’, which are later removed for speed and simplicity. The AHK requirement that scientific numbers must contain decimal points are lifted: 1e3 is valid.
For the users' perspective the only changes are improved precision and the support of scientific number notation, also in the output, by $6g or $9e type directives. Again, there are a lot of significant internal changes, so be careful and report problems!
#32

Posted 25 April 2007  03:00 PM
How about adding this math function for Arithmetic Sequences:
/* Example MsgBox,, nado, % nado(5, 5, 5) ; 25 */ nado(n, a, d) ; a + (n1)d { Return a+(n1)*d }
#33

Posted 30 October 2007  10:53 PM
Threeoperand functions are not (yet) supported. If you need to compute the entries of many arithmetic sequences, you can add your own construct for it, like a+n__d (where __ is an operator, returning n*dd), but it is not so common that justifies a new reserved symbol.
#34

Posted 31 October 2007  12:43 AM
Am I missing something or do PI and e give a result of 0 in the current version? Also, the binary conversions don't work for me.
Everything else I tried works.
These were evaluated in line:
pi = 0
e = 0
TOBIN(35) = 35
FROMBIN(10010) = 10010
Everything else I tried works.
These were evaluated in line:
pi = 0
e = 0
TOBIN(35) = 35
FROMBIN(10010) = 10010
#35

Posted 07 March 2008  08:37 PM
Pi and e work for me. I ran the script as it is in the first post in this thread. Select somewhere the two letters "pi" and hit the hotkey CtrlWin=. You will see "pi" changed to "pi = 3.14159".
ToBin and FromBin are internal functions. They are not exported. If you want to convert binary numbers to decimal, use "'10010". It will turn to "'10010 = 14". The opposite direction: "$b 35" becomes "$b 35= 0100011".
ToBin and FromBin are internal functions. They are not exported. If you want to convert binary numbers to decimal, use "'10010". It will turn to "'10010 = 14". The opposite direction: "$b 35" becomes "$b 35= 0100011".
#36

Posted 07 March 2008  09:05 PM
I decided to try it again in it's own script instead of within Autohotkey.ini and everything worked just fine. Something else must have gotten in the way.
Thanks for the help Lazlo.
Thanks for the help Lazlo.
#37

Posted 07 March 2008  09:53 PM
Hi Laszlo,
I got to know of a small bug in the function where hex numbers are being evaluated. Have a look:
0x12 > 18 [OK]
0xE1 > 225 [OK]
0x1E > 1 [ERROR]
From here.
Thanks,
Rajat
I got to know of a small bug in the function where hex numbers are being evaluated. Have a look:
0x12 > 18 [OK]
0xE1 > 225 [OK]
0x1E > 1 [ERROR]
From here.
Thanks,
Rajat
#38

Posted 24 May 2008  08:09 AM
CleanNews.in : Bite sized latest news headlines from India with zero bloat
Thanks, Rajat! I fixed the bug in the first post. The order of converting hex to decimal, and adding a missing decimal point to scientific numbers was swapped in the Eval(x) function. (Only this function needs to be patched.)
#39

Posted 24 May 2008  07:36 PM
Thanks a lot Laszlo for the prompt fix, and ofcourse for the great function itself! I replaced the function in nDroid, and the new one is available.
#40

Posted 24 May 2008  09:56 PM
CleanNews.in : Bite sized latest news headlines from India with zero bloat
Nice script! THX
I've one question and 2 suggestions^^
First the question: How to square? I didn't find the function somehow...I normally would try 2^2, but this is used in another way.
* it would be great if all brackets which are open at the end of the calculation would be closed automatically. Example: "sin(0.5" to "sin(0.5)"
* another thing would be the automatic multiplication. for example 3a should be the same as 3*a, or 3pi the same as 3*pi.
I've one question and 2 suggestions^^
First the question: How to square? I didn't find the function somehow...I normally would try 2^2, but this is used in another way.
* it would be great if all brackets which are open at the end of the calculation would be closed automatically. Example: "sin(0.5" to "sin(0.5)"
* another thing would be the automatic multiplication. for example 3a should be the same as 3*a, or 3pi the same as 3*pi.
#41

Posted 27 May 2008  03:13 PM
Monster uses the AHK notation for the power operator: 5**2 = 25, because ^ is used for XOR. There is also the shorthand, [email protected] for powers.
We can correct a few syntactic errors in the expressions, like adding missing parentheses, but it might cause more harm than good. If you type “sin(x + 1” and meant “sin(x)+1”, you get unexpected results. But, the result is wrong in the current version of Monster, too. Decent error handling could be more useful, which is planned for a future release.
3a in place of 3*a is another controversial issue. Clearly we cannot do it with numbers, because 32 is not the same a 3*2. There are also problems with hex numbers: 0x1 is 1, but could mean 0*x1. It is hard to tell what you want with 20x1. It can be 2*0x1 or 20*x1. Also 0x1a could mean 0x1*a or the number 26 written in hex. We can forbid hex numbers from this shorthand, and variable names starting with x, but it makes the syntax to be remembered more complex.
We can correct a few syntactic errors in the expressions, like adding missing parentheses, but it might cause more harm than good. If you type “sin(x + 1” and meant “sin(x)+1”, you get unexpected results. But, the result is wrong in the current version of Monster, too. Decent error handling could be more useful, which is planned for a future release.
3a in place of 3*a is another controversial issue. Clearly we cannot do it with numbers, because 32 is not the same a 3*2. There are also problems with hex numbers: 0x1 is 1, but could mean 0*x1. It is hard to tell what you want with 20x1. It can be 2*0x1 or 20*x1. Also 0x1a could mean 0x1*a or the number 26 written in hex. We can forbid hex numbers from this shorthand, and variable names starting with x, but it makes the syntax to be remembered more complex.
#43

Posted 27 May 2008  03:44 PM
it seems to be a sound and useful script, however, I think a simple patch to the ahk source code (allowing recursive expressions) would be much easier to use.
for example:
for example:
x = 2 y = 4 var := "x + 3y" ans := %var% (which logically is equivalent to ans := x + 3y)currently, a derefed variable in an expression is always considered by ahk to be the name of another variable, not part of the expression, which would allow continued support for dynamic variable and partially dynamic variables while introducing expression compatibility.
#44

Posted 04 July 2008  07:07 AM
problems := bugs + errors + glitches
(problems != 0) ? code := debug(code) : celebrate()

humor for: 1. Programmers 2. Everyone
(problems != 0) ? code := debug(code) : celebrate()

humor for: 1. Programmers 2. Everyone
* another thing would be the automatic multiplication. for example 3a should be the same as 3*a, or 3pi the same as 3*pi.
the reason this wont work is because there would be no way for the script to tell the difference between the multiplication and another variable name. 3 * pi would be 3pi, which is a valid name. also a * pi would be api, another valid name.
#45

Posted 04 July 2008  07:10 AM
problems := bugs + errors + glitches
(problems != 0) ? code := debug(code) : celebrate()

humor for: 1. Programmers 2. Everyone
(problems != 0) ? code := debug(code) : celebrate()

humor for: 1. Programmers 2. Everyone