How Lua handles both integer and float numbers?

As far as I remember myself programming I was taught not to compare floating point numbers for equality. Now, while reading Programming in Lua about Lua number type, I found following:

The number type represents real (double-precision floating-point)
numbers. Lua has no integer type, as it does not need it. There is a
widespread misconception about floating-point arithmetic errors and
some people fear that even a simple increment can go weird with
floating-point numbers. The fact is that, when you use a double to
represent an integer, there is no rounding error at all (unless the
number is greater than 100,000,000,000,000). Specifically, a Lua
number can represent any long integer without rounding problems.
Moreover, most modern CPUs do floating-point arithmetic as fast as (or
even faster than) integer arithmetic.

Is that true for all languages? Basically if we don’t go beyond floating point in doubles, we are safe in integer arithmetic? Or, to be more in line with question title, is there anything special that Lua does with its number type so it’s working fine as both integer and float-point type?

2

Lua claims that floating point numbers can represent integer numbers just as exactly as integer types can, and I’m inclined to agree. There’s no imprecise representation of a fractional numeric part to deal with. Whether you store an integer in an integer type, or store it in the mantissa of a floating point number, the result is the same: that integer can be represented exactly, as long as you don’t exceed the number of bits in the mantissa, + 1 bit in the exponent.

Of course, if you try to store an actual floating-point number (e.g. 12.345) in a floating point representation, all bets are off, so your program has to be clear that the number is really a genuine integer that doesn’t overflow the mantissa, in order to treat it like an actual integer (i.e. with respect to comparing equality).

If you need more integer precision than that, you can always employ an arbitrary-precision library.

Further Reading
What is the maximum value of a number in Lua?

7

Doubles are stored as a mantissa and an exponent. See the format for more information. Basically, all numbers are of the form: mantissa * 2exponent. For any integer smaller than 252, the exponent will be zero, making the mantissa bit-for-bit equivalent to a 52-bit unsigned integer. A separate sign bit is used to indicate negative numbers.

In fact, even some integers larger than 252 can be represented exactly, as long as all the digits past the 52nd are zeros. Also, some fractions, like 0.5, can be represented exactly. It’s only when the fraction is continuously repeating (like 1/3) in base 2, or otherwise requires too many bits past the radix point that you lose precision.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật