Can AHK do simple maths programs? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
daithy
Posts: 11
Joined: 30 May 2018, 18:13

Can AHK do simple maths programs?

31 May 2018, 12:34

I'd like to make a basic math windows script 100/(A/B+1) into which I can quickly type in values and get a result.

Thanks.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Can AHK do simple maths programs? | Evaluate math expressions in strings  Topic is solved

31 May 2018, 18:34

Code: Select all

A := 100
B := 2

Result := 100/(A/B+1)
MsgBox % Result
Updated: https://autohotkey.com/boards/viewtopic ... 40#p224260.

Code: Select all

InputBox n ,,,,,,,,,, 2*PI
MsgBox % Eval(n)
ExitApp

;MsgBox % Eval("PI") . "`n" . Eval("2>3") . "`n" . Eval("3>2") . "`n" . Eval("abs(-100)*2+50") . "`n" . Eval("143**143")
Eval(Expression)
{
    Expression := RegExReplace(Expression, "\s")
    Expression := StrReplace(Expression, ",", ".")
    Expression := RegExReplace(StrReplace(Expression, "**", "^"), "(\w+(\.*\d+)?)\^(\w+(\.*\d+)?)", "pow($1,$3)")    ; 2**3 -> 2^3 -> pow(2,3)
    ;Expression := StrReplace(Expression, "PI", ACos(-1))
    Expression := RegExReplace(Expression, "=+", "==")    ; = -> ==  |  === -> ==  |  ==== -> ==  |  ..
    Expression := RegExReplace(Expression, "\b(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)\b", "Math.$1")
    Expression := RegExReplace(Expression, "\b(abs|acos|asin|atan|atan2|ceil|cos|exp|floor|log|max|min|pow|random|round|sin|sqrt|tan)\b\(", "Math.$1(")

    (o := ComObjCreate("HTMLfile")).write("<body><script>document.body.innerText=eval('" . Expression . "');</script>")
    o := StrReplace(StrReplace(StrReplace(InStr(o:=o.body.innerText, "body") ? "" : o, "false", 0), "true", 1), "undefined", "")
    Return o ;InStr(o, "e") ? Format("{:f}", o) : o
} ; https://autohotkey.com/boards/viewtopic.php?f=6&t=15389
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Can AHK do simple maths programs?

01 Jun 2018, 13:44

daithy wrote:I'd like to make a basic math windows script 100/(A/B+1) into which I can quickly type in values and get a result.

Thanks.
There is a pretty slick way to handle this using an ActiveX control that already knows how to evaluate math expressions that are represented as a string.

Code: Select all

Gui, Add, ActiveX, vpwb, Shell.Explorer
pwb.Navigate("about:<!DOCTYPE HTML><meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"">")
Math := pwb.Document.parentWindow

MsgBox, % Math.Eval("2+3")

A := 100
B := 2
MsgBox, % Math.Eval(100/(A/B+1)) ; this is actually being evaluate by AHK and is not a good example as pointed out by Flipeador

Exp = 100/(%A%/%B%+1)
MsgBox, % Exp " = " Math.Eval(Exp)
FG
Last edited by FanaticGuru on 01 Jun 2018, 14:08, edited 2 times in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Can AHK do simple maths programs?

01 Jun 2018, 13:56

FanaticGuru wrote:There is a pretty slick way to handle this using an ActiveX control that already knows how to evaluate math expressions.
I think it should be Math.Eval("100/(" . A . "/" . B . "+1)") instead of Math.Eval(100/(A/B+1)).

interesting way to do it :)
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Can AHK do simple maths programs?

01 Jun 2018, 13:58

Flipeador wrote:
There is a pretty slick way to handle this using an ActiveX control that already knows how to evaluate math expressions.
I think it should be Math.Eval("100/(" . A . "/" . B . "+1)") instead of Math.Eval(100/(A/B+1)).

interesting way to do it :)
Yea, right after I posted, I noticed I did not do very good examples. Editted while you were posting to do better. But hopefully I got the basic technique across.

You can pass the ActiveX a totally math expression string but you can not pass it an AHK variable in a string. Any AHK variables have to be resolved by AHK before sending the math expression string to the ActiveX control.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Can AHK do simple maths programs?

01 Jan 2020, 05:24

FanaticGuru wrote:
01 Jun 2018, 13:44
daithy wrote:I'd like to make a basic math windows script 100/(A/B+1) into which I can quickly type in values and get a result.

Thanks.
There is a pretty slick way to handle this using an ActiveX control that already knows how to evaluate math expressions that are represented as a string.

Code: Select all

Gui, Add, ActiveX, vpwb, Shell.Explorer
pwb.Navigate("about:<!DOCTYPE HTML><meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"">")
Math := pwb.Document.parentWindow

MsgBox, % Math.Eval("2+3")

A := 100
B := 2
MsgBox, % Math.Eval(100/(A/B+1)) ; this is actually being evaluate by AHK and is not a good example as pointed out by Flipeador

Exp = 100/(%A%/%B%+1)
MsgBox, % Exp " = " Math.Eval(Exp)
FG

In some languages, the thousand separator "." sign is the "," sign in some languages. How can we do the correct calculation in any case?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Can AHK do simple maths programs?

01 Jan 2020, 15:03

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Can AHK do simple maths programs?

01 Jan 2020, 16:59

jNizM wrote:
01 Jan 2020, 15:03
With (e.g.) GetNumberFormat / GetNumberFormatEx function
Thanks. Very good. Autohotkey is a very rich language thanks to you and other developers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], inseption86, peter_ahk, william_ahk and 186 guests