On 01/09/2018 00:56, Egor Skriptunoff wrote:

> On Sat, Sep 1, 2018 at 1:09 AM, Albert Chan wrote:

>

>>> But it appears that math.huge as argument also raises this error.

>>> IMO, the following use cases should be considered as being valid:

>>> string.sub("abc", 2, math.huge)

>>> string.sub("abc", -math.huge, -2)

>>> Yes, I know we could use -1 and 1 values respectively to get the same

>> results,

>>> but infinite values are suitable for the semantic of the function.

>>

>> I just felt uneasy using infinity as end-point ...

>> Especially bad if the infinity value is hidden behind a variable.

>>

>> What is gain by not using 1 for first location, -1 for last ?

>>

>

> local function get_suffix_of_length(str, len)

> return str:sub(-len)

> end

>

> get_suffix_of_length(s, math.huge) -- Easily understandable

> get_suffix_of_length(s, -1) -- What does "the suffix of length (-1)" mean?

>

> So, the gain is readability.

> We have expected behavior for the case when variable suffix_length =

> math.huge

> We don't need to introduce magic number in a documentation: "(-1) means

> maximal length"

> Infinity is very natural value, every user is aware of its meaning.

>

The problem is that math.huge it is not guaranteed to be, semantically,

infinity. The docs says:

"The float value HUGE_VAL, a value larger than any other numeric value. "

Which doesn't even guarantee that it is and IEEE754 "inf" value: you may

be working on a non-IEEE754 compliant machine or you may have compiled

Lua with different float support, or whatever.

This may lead to program that behave differently on different

platforms/machines/Lua versions (if you care about that).

As Sean Conner said in another message, you may use math.maxinteger,

which is more explicit.

Cheers!

-- Lorenzo