Lua How to tell 1 from 1.0

I have a script configuration where the user can enter values ​​as an absolute value or a percentage value.

Absolute values ​​are written as a value between 0.0 and 1.0, while a percentage value is written as from 0 to 100.

How can I distinguish 1 from 1.0? If I used strings, then this is not a problem for sure ... I would like this configuration to be simple and not have to rely on strings.

Is this even possible?

REFERENCE:

a = 1
b = 1.0

How to say that adoes not belong to the type b.

EDIT The configuration file looks something like this:

local config = {}

-- A lot of comments describing how to configure

config.paramA = 1
config.paramB = 1.0

return config

In my script processing, I read the configuration files as follows:

config = require 'MyConfigFile'

config.paramA
config.paramB
+4
4

Lua 5.3 , . math.type - .

local x = 1.0
print(math.type(x)) -- float
x = 1
print(math.type(x)) -- integer

, William : " - ". , , , . 2 , .

+7

PIL , , 1 1.0 ,

, , :

config.paramA = { 1, 'i' }
config.paramB = { 1.0, 'd' }

:

config.paramA = '1'
config.paramB = '1.0'
+4

Lua 5.1 5.2 , luac (luac -i), local a, b = 1, 1.0 :

main <1.lua:0,0> (3 instructions, 12 bytes at 007D04E8)
0+ params, 2 slots, 0 upvalues, 2 locals, 1 constant, 0 functions
    1   [1] LOADK       0 -1    ; 1
    2   [1] LOADK       1 -1    ; 1
    3   [1] RETURN      0 1

Lua 5.3 , math.type, .

, , , :. 1.0000001 1.0

+2

:

1)

2) ,

config.paramA = percent(1.9)
config.paramB = portion(1)

these functions can create tables with type information or simply convert the value to a consistent type

+1
source

Source: https://habr.com/ru/post/1606426/


All Articles