number conversion

Discuss the future of the AutoHotkey language
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

number conversion

26 Nov 2016, 18:09

i'm working on upgrading the v2 script converter, and i'm hoping for some help. this is how Frankie originally handled the conversion of a number:

Code: Select all

ToExp(Text)
{
   ;...
   if Type(Text+0) != "String"
   {
      TOut := Text+0
      ;msgbox %text%`n%TOut%
      return TOut
   }
}
however, that is producing these results:

Code: Select all

;before
  VERSION = 2.4
  DEBUG = 1
  WM_HSCROLL = 0x114
  WM_VSCROLL = 0x115
  INI_EXCLUDE = ServerSettings
  
;after
  VERSION := 2.3999999999999999
  DEBUG := 1
  WM_HSCROLL := 276
  WM_VSCROLL := 277
  INI_EXCLUDE := "ServerSettings"
the inputvar "Text" to the ToExp() func is always going to be a string since we're parsing lines from the script file.

can you advise the correct way to handle this? is it safe to remove the "+0" in TOut := Text+0 ?

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: number conversion

26 Nov 2016, 18:31

hrmmm
https://lexikos.github.io/v2/docs/commands/Type.htm

Code: Select all

var3 := 3
var4 := "4"

msgbox, % "Type(var3)=" Type(var3)
msgbox, % "Type(var4)=" Type(var4)

msgbox, % "var3 is " . ((var3 is "number") ? "number" : "not number")
msgbox, % "var4 is " . ((var4 is "number") ? "number" : "not number")
so is this ok?

Code: Select all

ToExp(Text)
{
   if (Text is "number")
   {
      TOut := Text
      ;msgbox %text%`n%TOut%
      return TOut
   }
}

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: number conversion

26 Nov 2016, 21:49

IMO, var = value should always assign a string. It is safe to convert to var := value when value + 0 "" == "" value (i.e. conversion to number and back to string produces the same string), but even then, it's only a guess that the value is intended to be a number, and could be wrong. In a script written for v1, I think the format of a number (such as VERSION = 2.4) is more likely to matter than the type.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: number conversion

29 Nov 2016, 10:22

thanks. what about in these other cases?

1. function params default values? ie: MyFunc(var=3) when changing that to := should that change to string as well or stay as number?

2. traditional-if? if var = 3 should that change to if (var = "3") ?

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: number conversion

29 Nov 2016, 23:00

Strings are always quoted with function parameter default values. If it's not quoted, it's a number.

However, Func(var=03) and var:=03 in v1 assign both the number 3 and the character string "03". If you use var with an object, it will be treated as a number and the character string may be ignored; but if you use it as a string you will get "03", not "3". All numeric literals in expressions behave this way in v1 (except floating-point literals, which are really just strings).

if var = value in v1 performs a numeric comparison if var and value are both numeric strings or numbers, whereas v2 requires that at least one of them is a pure number. So (3 = "0x3") is true in v2, but ("3" = "0x3") is false. There is no simple way to convert if var = 3 while retaining its behaviour, without knowing what var may contain or the author's intent.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: number conversion

30 Nov 2016, 23:33

lexikos wrote: if var = value in v1 performs a numeric comparison if var and value are both numeric strings or numbers, whereas v2 requires that at least one of them is a pure number. So (3 = "0x3") is true in v2, but ("3" = "0x3") is false. There is no simple way to convert if var = 3 while retaining its behaviour, without knowing what var may contain or the author's intent.
well i have to choose some direction.. in my basic unit tests, obviously both get executed the same:
https://github.com/mmikeww/AHK-v2-scrip ... s.ahk#L418

i think the intent is more likely to be a numeric comparison if the code is written if var = number, and so to ensure v2 compares numerically i would convert to if (var = number) without the quotes so that 'number' is pure. would you agree thats preferable?


Return to “AutoHotkey Development”

Who is online

Users browsing this forum: OpalMonkey and 63 guests