lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On Apr 23, 2015, at 12:59 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> 
>> What about relational operators? Consider this:
>> 
>> a = (1<<60) + 180
>> b = (1<<60) + 200
>> bf = b + 0.0
>> print(a < b)					-> true
>> print(a < bf)					-> false (!)
>> printf(a < math.tointeger(bf))		-> true
>> 
>> Of course, I carefully chose those numbers, but the truncating effect of integer->float will give strange results when doing (say) table.sort() on mixes of floats and integers.
> 
> Morever, I guess (but may be wrong) that a better comparsion function
> (e.g., that compares the "true" mathematical values) would be
> expensive. Unlike equality, that we seldom use for floats, mixed order
> comparisons can be quite frequent (e.g., "if f > 0 then" instead of
> "if f > 0.0 then"), so we do not want something expensive here.
> 
> -- Roberto
> 

Agreed, I don’t see a cheap way to do this. I know you are wary of bloat in the libraries, but I wonder if a “math.le()” function that did the alternate form would be useful? 

function math.le(a, b)
	local ia, ib = math.tointeger(a), math.tointeger(b)
	if ia and ib then return ia <= ib else return a <= b end
end

I suspect a C version of the above would be significantly more efficient and justified given that a typical use would be sorting, where it may be called many times.

—Tim