Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Monster: evaluate math expressions in strings


  • Please log in to reply
87 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
I like the name too :8)
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
AHK version 1.0.46.12 introduced the long awaited scientific number formats (1.23e-7). 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 digit-sequence oriented, that is, each partial result is converted to a string of digits, replacing the sub-expression 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.23e-7)
- unary operator ( -x )
- binary operator ( x-y )

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 pre-processing 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!

trik
  • Members
  • 1317 posts
  • Last active: Jun 11 2010 11:48 PM
  • Joined: 15 Jul 2007
How about adding this math function for Arithmetic Sequences:

/* Example
MsgBox,, nado, % nado(5, 5, 5) ; 25
*/

nado(n, a, d)              ; a + (n-1)d
{
   Return a+(n-1)*d
   }


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Three-operand 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*d-d), but it is not so common that justifies a new reserved symbol.

Gerf
  • Guests
  • Last active:
  • Joined: --
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

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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 Ctrl-Win-=. 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".

Gerf
  • Guests
  • Last active:
  • Joined: --
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.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
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

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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.)

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
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. :)

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Salkin
  • Guests
  • Last active:
  • Joined: --
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.

Rajat as guest
  • Guests
  • Last active:
  • Joined: --
Squaring is simple. 3**2 (3 to the power 2) gives 9

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Monster uses the AHK notation for the power operator: 5**2 = 25, because ^ is used for XOR. There is also the shorthand, 5@2 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.

apocalypse~r
  • Members
  • 41 posts
  • Last active: Sep 24 2009 02:14 PM
  • Joined: 21 Jun 2007
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:
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.
problems := bugs + errors + glitches
(problems != 0) ? code := debug(code) : celebrate()
--
humor for: 1. Programmers 2. Everyone

apocalypse~r
  • Members
  • 41 posts
  • Last active: Sep 24 2009 02:14 PM
  • Joined: 21 Jun 2007

* 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.
problems := bugs + errors + glitches
(problems != 0) ? code := debug(code) : celebrate()
--
humor for: 1. Programmers 2. Everyone