Ternary help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bad husband
Posts: 54
Joined: 21 Oct 2017, 13:38

Ternary help

18 Jun 2020, 20:07

I build a simple GUI calculator a while back and I entered some ternary code. I believe I tried to enter some additional lines but I got an error message about too many values so I reduced the number of lines. I entered a range from 10.1 to 13.1.

My max value I put was 13.1 and I thought that range would be great, but the other day we received a value 13.3 and of course the calculator gave a result of zero because it was not setup to exceed the value of 13.1.

I’m wondering if there is an improvement to my code to increase the range?

Thanks for looking.

Code: Select all


brixx:= Round(brix,1)

data:= (brixx="10.1")?("10.40")
      :(brixx="10.2")?("10.40") 
      :(brixx="10.3")?("10.41")
	  :(brixx="10.4")?("10.41")
	  :(brixx="10.5")?("10.41")
	  :(brixx="10.6")?("10.42")
	  :(brixx="10.7")?("10.42")
	  :(brixx="10.8")?("10.43")
	  :(brixx="10.9")?("10.43")
	  :(brixx="11.0")?("10.43")
	  :(brixx="11.1")?("10.44")
	  :(brixx="11.2")?("10.44")
	  :(brixx="11.3")?("10.45")
	  :(brixx="11.4")?("10.45")
	  :(brixx="11.5")?("10.46")
	  :(brixx="11.6")?("10.46")
	  :(brixx="11.7")?("10.46")
	  :(brixx="11.8")?("10.47")
	  :(brixx="11.9")?("10.47")
	  :(brixx="12.0")?("10.48")	
	  :(brixx="12.1")?("10.48")
	  :(brixx="12.2")?("10.49")
	  :(brixx="12.3")?("10.49")
	  :(brixx="12.4")?("10.49")
	  :(brixx="12.5")?("10.50")
	  :(brixx="12.6")?("10.50")
	  :(brixx="12.7")?("10.51")
	  :(brixx="12.8")?("10.51")
	  :(brixx="12.9")?("10.52")
	  :(brixx="13.0")?("10.52")
	  :(brixx="13.1")?("10.52")
      :("Else")

;MsgBox % data

ans:= Round(lbs/data,3)

 ;MsgBox, % ans 
 

ImpGal := ans
GuiControl,, Final, %ImpGal%


return
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Ternary help

18 Jun 2020, 21:09

One way would be to use a range instead of discrete points:

Code: Select all

data:= (brixx>10.05 && brix<10.25)?("10.40")
      :(brixx>10.25 && brix<10.55)?("10.41")
	  :(brixx>10.55 && brix<10.75)?("10.42")
	  ; etc.
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Ternary help  Topic is solved

18 Jun 2020, 21:17

Better:

Code: Select all

brixx := Round(brix,1)

brixx := 11.8 ; for test purposes

pairs := {10.1: 10.40, 10.2: 10.40, 10.3: 10.41, 10.4: 10.41, 10.5: 10.41, 10.6: 10.42, 10.7: 10.42, 10.8: 10.43, 10.9: 10.43
	, 11.0: 10.43, 11.1: 10.44, 11.2: 10.44, 11.3: 10.45, 11.4: 10.45, 11.5: 10.46, 11.6: 10.46, 11.7: 10.46, 11.8: 10.47, 11.9: 10.47
	, 12.0: 10.48, 12.1: 10.48, 12.2: 10.49, 12.3: 10.49, 12.4: 10.49, 12.5: 10.50, 12.6: 10.50, 12.7: 10.51, 12.8: 10.51, 12.9: 10.52
	, 13.0: 10.52, 13.1: 10.52}

data := pairs[brixx]
MsgBox, % data
return
Bad husband
Posts: 54
Joined: 21 Oct 2017, 13:38

Re: Ternary help

20 Jun 2020, 11:43

Thanks boiler. It’s works great
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Ternary help

20 Jun 2020, 13:55

If you have ranges like this, switch might work better:

Code: Select all

brixx:= 12.6

switch 
	{
	case (brixx >= 10.3 and brixx < 10.6) : res := 10.41
	case (brixx >= 10.6 and brixx < 10.8) : res := 10.42
	case (brixx >= 10.8 and brixx < 11.1) : res := 10.43
	case (brixx >= 11.1 and brixx < 11.3) : res := 10.44
	case (brixx >= 11.3 and brixx < 11.5) : res := 10.45
	case (brixx >= 11.5 and brixx < 11.7) : res := 10.46
	case (brixx >= 11.7 and brixx < 11.9) : res := 10.47
	case (brixx >= 11.9 and brixx < 12.1) : res := 10.48
	case (brixx >= 12.1 and brixx < 12.3) : res := 10.49
	case (brixx >= 12.3 and brixx < 12.5) : res := 10.50
	case (brixx >= 12.5 and brixx < 12.7) : res := 10.51
	}
msgbox % res
14.3 & 1.3.7
Bad husband
Posts: 54
Joined: 21 Oct 2017, 13:38

Re: Ternary help

20 Jun 2020, 22:16

Hi, flyingDman thanks for you answer. It will also work for my application. I learned something new that I will use for something else that I was planning on using AHK to streamline.

Always useful people here :D
Rohwedder
Posts: 7648
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Ternary help

21 Jun 2020, 03:10

Hallo,
or:

Code: Select all

brixx:= 12.6

switch 
	{
	case (brixx >= 10.3 and brixx < 10.6) : res := 10.41
	case (brixx >= 10.6 and brixx < 10.8) : res := 10.42
	case (brixx >= 10.8 and brixx < 11.1) : res := 10.43
	Default : res := Round(10.43+(brixx-11)/20, 2)
	}
msgbox % res

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], OrangeCat and 187 guests