How safe is comparing numbers in lua with equality operator?

In my engine I have a Lua VM for scripting. In the scripts, I write things like:

stage = stage + 1
if (stage == 5) then ... end


objnum = tonumber("5")
if (stage == objnum)

According to the Lua sources, Lua uses a simple equality operator when comparing doubles, the internal number type it uses.

I am aware of precision problems when dealing with floating point values, so I want to know if the comparison is safe, that is, will there be any problems with simply comparing these numbers using Lua's default '==' operation? If so, are there any countermeasures I can employ to make sure 1+2 always compares as equal to 3? Will converting the values to strings work?


I can employ to make sure 1+2 always compares as equal to 3?

You needn't worry. The number type in Lua is double, which can hold many more integers exactly than a `long int``.

You may be better off by converting to string and then comparing the results if you only care about equality in some cases. For example:

> print(21, 0.07*300, 21 == 0.07*300, tostring(21) == tostring(0.07*300))
21      21      false   true

I learned this hard way when I gave my students an assignment with these numbers (0.07 and 300) and asked them to implement a unit test that then miserably failed complaining that 21 is not equal 21 (it was comparing actual numbers, but displaying stringified values). It was a good reason for us to have a discussion about comparing floating point values.

Need Your Help

detect distance scrolled from top jquery


How can I detect the number of pixels scrolled in a browser window?

Magento - update all products inventory with sql


I want to update the inventory of all My Magento products.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.