This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Problem comparing numbers?

Started by mrpoopy345, 01 May 2015 - 09:50 AM
mrpoopy345 #1
Posted 01 May 2015 - 11:50 AM
I am writing a code to test if the area of a circle is (circumfrence/2)*radius. My code is:


pi = 3.1415926535
for i = 1, 200 do
area = (i^2)*pi
circ = (i*2)*pi
if area == (circ/2)*i then
 print("Worked")
else
    print(area..": "..(circ/2)*i)
end
end
But this produces a weird result, as shown here:
http://gyazo.com/4d744346bdc1f17146f8eb804cdbf1bc

Most of them work, but some say they don't work, even though clearly (circ/2)*pi is the same as area.
What is wrong with this code?
Bomb Bloke #2
Posted 01 May 2015 - 12:24 PM
Generally, when you want to store a number in memory, you'll define it as a specific type of number. For example, a "unsigned byte"-type is generally used when you want to store integral values ranging from 0 to 255. A two-byte "signed int" is suitable for the range of −32,768 to 32,767.

Exactly how much memory is assigned to a given type varies a bit by language (generally it goes up as time has gone one - a byte's always a byte, though). In the case of Lua, however, you don't assign a specific type for your numbers - they're just referred to as "numbers", no matter what. In the background… they're all float types.

Floats can handle fractions, and they can do a pretty good job of it, but they still only have a limited, static amount of memory available with which to store numbers. This leads to a problem - there are a LOT of potential fractions that can go alongside any given int. So, floats fudge things a bit.

Usually this inaccuracy doesn't matter, but sometimes it can lead to odd results. This being a case in point. Here's another, and another

For more info, take a read through this.