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

Weird "bug" with math?

Started by jag, 20 January 2013 - 10:09 PM
jag #1
Posted 20 January 2013 - 11:09 PM
So I am trying to make a simple program.
It's just with some selection and stuff.

But while I was testing I noticed a weird "bug":
(">" means simply output from the computer while in the lua prompt)


sel = 3.2
>

sel - math.floor(sel)
> 0.2

sel - math.floor(sel) == .2
> false

sel - math.floor(sel) == 0.2
> false

sel % 1
> 0.2

sel % 1 == .2
> false

sel - math.floor(sel) == sel - math.floor(sel)
> true

sel % 1 == sel % 1
> true

sel - math.floor(sel) == sel % 1
> true

sel - math.floor(sel) == .2
> false

So in some way either the "==" operator is disfunctional or the math of my computer is collapsing…

EDIT: There might be some bug with the server that I am playing on…
EDIT 2: Nope we restarted the server but it stills does this weird thingy… This might still be a bug with the server, but I'm not sure.
theoriginalbit #2
Posted 20 January 2013 - 11:16 PM
- snip -
math.floor ALWAYS rounds the number down making it 3.0 … just like math.ceil always rounds up making it 4.0 … i think the one you are after is math.modf

http://lua-users.org/wiki/MathLibraryTutorial
Edited on 20 January 2013 - 10:18 PM
jag #3
Posted 21 January 2013 - 12:23 AM
-snip-
No because I do "sel [MINUS] floor'ed sel":


sel - math.floor(sel)
> 0.2

So its:
3.2 - 3.0 = 0.2
simple
Orwell #4
Posted 21 January 2013 - 12:35 AM
Indeed, he is certainly using the correct calculations. Though, it might be worth looking into math.modf. In c++ at least, the % operator is only usable for integers (probably not true for Lua), and fmod() is what you'd want for floating point numbers. But even if it works with math.modf, it doesn't explain why your statements don't.
theoriginalbit #5
Posted 21 January 2013 - 12:41 AM
Indeed, he is certainly using the correct calculations. Though, it might be worth looking into math.modf. In c++ at least, the % operator is only usable for integers (probably not true for Lua), and fmod() is what you'd want for floating point numbers. But even if it works with math.modf, it doesn't explain why your statements don't.
fmod and modf are different in Lua…

math.modf

Return the integral and fractional parts of the given number.
> = math.modf(5)
5 0
> = math.modf(5.3)
5 0.3
> = math.modf(-5.3)
-5 -0.3
Eric #6
Posted 21 January 2013 - 12:56 AM
Welcome to floating point numbers! In the same way that you can't represent 1/3 accurately in decimal form, computers cannot represent 1/5 accurately in binary form. This very simple test fails:
assert(1.2 - 1 == 0.2)

Because:

lua> print((1.2 - 1) - 0.2)
-5.5511151231258e-17

Take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic
jag #7
Posted 21 January 2013 - 03:01 AM
Oh god I am confused…

How do I get this to work?
jag #8
Posted 21 January 2013 - 03:11 AM

The only way you can understand everything on that site is if you wrote it got damn by yourself..
Eric #9
Posted 21 January 2013 - 04:47 AM
That wasn't the article I remembered it to be. This one any better?
Orwell #10
Posted 21 January 2013 - 05:46 AM
Basically you can sum it up as 'rounding errors'. It seems that I was mistaken about the functionality of 'math.modf', from what TheOriginalBit posted about it, it seems like it should do the job just fine.

Edit: tested it, still got the same problems. Try out Eric's article. It should do the job. Alternatively, if you want a less elegant (and less robust) solution, use tostring() to compare them :P/>
jag #11
Posted 21 January 2013 - 06:20 AM
Yay thank you!
That simple but inefficient way of converting and comparing it as a string works!
My program wooooorks! ;D