if-condition doesn't recognize certain float additions?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Twain
Newbie
Posts: 7
Joined: Fri Aug 12, 2016 4:24 am
Contact:

if-condition doesn't recognize certain float additions?

#1 Post by Twain » Wed Jun 06, 2018 9:18 am

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"

User avatar
xavimat
Eileen-Class Veteran
Posts: 1433
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: xavimat
Location: Italy
Contact:

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

#2 Post by xavimat » Wed Jun 06, 2018 9:32 am

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
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Twain
Newbie
Posts: 7
Joined: Fri Aug 12, 2016 4:24 am
Contact:

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

#3 Post by Twain » Wed Jun 06, 2018 9:49 am

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.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

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

#4 Post by kivik » Wed Jun 06, 2018 9:51 am

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?

User avatar
Ocelot
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

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

#5 Post by Ocelot » 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.
< < insert Rick Cook quote here > >

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

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

#6 Post by kivik » Wed Jun 06, 2018 12:06 pm

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!

User avatar
xavimat
Eileen-Class Veteran
Posts: 1433
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: xavimat
Location: Italy
Contact:

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

#7 Post by xavimat » Thu Jun 07, 2018 6:29 am

Twain wrote:
Wed Jun 06, 2018 9:49 am
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.
kivik wrote:
Wed Jun 06, 2018 9:51 am
So 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.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: No registered users