Page 1 of 1

if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 9:18 am
by Twain
I was doing some float additions when I noticed some if-conditions weren't triggered and I can't figure out why. What am I missing here? I used renpy 7.0 Script below.

label start:

"starting from 0.5"
$ fnumber = 0.5

$ fnumber += 0.1
if fnumber == 0.6:
"0.6"

$ fnumber += 0.1
if fnumber == 0.7:
"0.7"

$ fnumber += 0.1
if fnumber == 0.8: #------------------ this if isn't triggered
"0.8"

$ fnumber += 0.1
if fnumber == 0.9: #----------------- this if isn't triggered
"0.9"


"done"

Re: if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 9:32 am
by xavimat
Floats have internal issues in computer math.
Don't use floats if it's not strictly necessary. Also, use ranges instead of precise floats in the if/elif statements.
See: https://docs.python.org/2/tutorial/floatingpoint.html
Python surprise:

Code: Select all

>>> 1.1 + 2.2 == 3.3
False
>>> 1.1 + 2.2
3.3000000000000003

Re: if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 9:49 am
by Twain
Thanks for your explanation and link xavimat. I see the problem now. I'll make sure to be very careful around floats if I must use them.

Re: if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 9:51 am
by kivik
This is super interesting! I knew floating point can cause issues (Excel anyone?) but I hadn't considered it happening in Python like this!!

So would you normally just factor stuff up by 10 or 100 to avoid decimal places?

Re: if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 11:22 am
by Ocelot
Strictly speaking it is not a problem with Excel or Python, but with IEEE 754 (commonly used format of floating point number representation) and in fact of math itself. There is no way to precisely store every possible value in a finite amount of data. Also computers store numbers in binary. We might thing decimal number as easy to represent, but for computer is like writing 3/7 in decimals for us.

Re: if-condition doesn't recognize certain float additions?

Posted: Wed Jun 06, 2018 12:06 pm
by kivik
Ocelot wrote: Wed Jun 06, 2018 11:22 am Strictly speaking it is not a problem with Excel or Python, but with IEEE 754 (commonly used format of floating point number representation) and in fact of math itself. There is no way to precisely store every possible value in a finite amount of data. Also computers store numbers in binary. We might thing decimal number as easy to represent, but for computer is like writing 3/7 in decimals for us.
Hehe I know it's not the software itself but the floating point, but it just affects software in unexpected places. My first experience being Excel when I started working in an office - I had take a pen and paper to explain binary to a colleague and why her numbers were messed up.

I hadn't considered it'd be an issue in programming languages because I guess I've always worked in integers! But of course it makes sense that it'd face the same issue!

Re: if-condition doesn't recognize certain float additions?

Posted: Thu Jun 07, 2018 6:29 am
by xavimat
Twain wrote: Wed Jun 06, 2018 9:49 amThanks for your explanation and link xavimat. I see the problem now. I'll make sure to be very careful around floats if I must use them.
kivik wrote: Wed Jun 06, 2018 9:51 amSo would you normally just factor stuff up by 10 or 100 to avoid decimal places?
Well, I've answered very quickly, considering this is a forum for narrative games. I can't see a use-case where floats are really really necessary. But, if someone is using renpy for complex calculations where high precision math is necessary, python has the "decimal" module (say, an educational VN to learn some complex math :roll: ).
In VNs and the like, I never user floats and I think simply factoring up as kivik suggest is enough.