Page 1 of 1

Be aware of floating point arithmetic

Posted: Thu May 22, 2008 10:04 pm
by kattle87
Sorry but I was quite nervous when I wrote this post first time...
This is the matter:
frac(12.4) = 0.3999...
frac(12.4001) = 0.4
AAAAAAAARRRRRRGGGGGGGGGGHHHHHHHHH!!!!!!!!!!!!

I SPENT 2 HOURS trying to understand what was going wrong :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

i was learned at school you could trust the frac() function... what's going on? Floating point arithmetic i guess...
At this point, I really do need a modulus operator (in ansi C it is %) because it's fast and exact, and when you need precision you are requested to write a very long expression...
Plus, i really don't get how the frac() works with negative numbers... I would really appreciate explanations, or a working floor and ceiling function :P

EDITED: the way I wrote it was really deprecated... Sorry!

Posted: Fri May 23, 2008 9:05 am
by myraneal
Try and be a litte calm kattle87. I'm sure that Ville can explain what's going on in a reasonable time frame. No need for extra exclamation marks or not so nice topic titles.

-Yılmaz

Posted: Fri May 23, 2008 11:33 am
by kattle87
I edited it... it was not so nice indeed... I'm really sorry: I've really gone mad yesterday evening because of it :(

Posted: Fri May 23, 2008 11:51 am
by VilleK
Ha ha, I know it is very frustrating when computer software does not work the way you expect. But it is often better to try to calm down a bit before posting on forums ;)

ZGE simply use the standard frac-function in the pascal library which should work the same as in C. But as you notice yourself, limited floating point precision (floats in zge are 32-bit) may cause unexpected results sometimes.

Frac on negative numbers give a negative result: frac(-1.2) == -0.2

Perhaps if you give us some more context about the problem you want to solve with the frac function and maybe someone can help with another technique of achieving the same results.

Posted: Fri May 23, 2008 12:29 pm
by kattle87
Actually I have solved but avoiding usage of modulus (%) means writing a looooong expression. :D However tomorrow or Sunday you will probably see an untextured version of the project I've started (and almost finished since it's a really simple clone of a very simple old game ;) ) then you all will be called to arms for writing levels :P

BTW, I'm working on some clones because I think it's going to be a good way to catch up attention, I plan to try spreading the word (maybe not spamming but posting link everywhere they accept them and things like that) in 1 or 2 months... And I would like to get some material finished since that date ;)

I feel your pain,. .

Posted: Fri May 23, 2008 1:50 pm
by jph_wacheski
ahwww Dang-it! I missed the first title to this post,. Drat. oh well, it must have been funny,. as other ppls frustratins often are, . you know from a distance. sorry but it's true.
I have actually had a similer problem with the lack of a floor() and ceil() function,. in fact this frac() function is, I think, what forced me out of frustration to abandon my first effort for the PGC compo,.
One thing I can tell you for sure, is when you are using tools built by other ppl, even great ppl like VilleK, you will always find mis-understandings to how things are working,. This is why as they say 'a master carpenter (or game maker in theis case!) builds her own tools'! you would then, conceptualy anywhy, have an understanding of how things are implemented. Just so you know, we all feel your pain,. and have often 'been there',. and will no doubt be there again, unfortunatly! So we can chukkle about it, a bit,. at least when it is not us banging our heads against a stack of code that SHOULD work,. but don't.
Sometimes the best thing to do to gain an understanding of a function you did not write yourself is to run a series simple tests on the function and take note of the results,. best to do this in a separte new proj. that just uses the function and shows you the results in the clearest way. At leat you will find out how the function works. and even if it does not do what you wanted it to, you can still use it for what it does do.