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"
if-condition doesn't recognize certain float additions?
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.
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.
- xavimat
- Eileen-Class Veteran
- Posts: 1460
- 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: pilgrimcreations
- Location: Spain
- Contact:
Re: if-condition doesn't recognize certain float additions?
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:
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!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Re: if-condition doesn't recognize certain float additions?
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?
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?
So would you normally just factor stuff up by 10 or 100 to avoid decimal places?
- Ocelot
- Lemma-Class Veteran
- Posts: 2384
- Joined: Tue Aug 23, 2016 10:35 am
- Github: MiiNiPaa
- Discord: MiiNiPaa#4384
- Contact:
Re: if-condition doesn't recognize certain float additions?
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 > >
Re: if-condition doesn't recognize certain float additions?
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.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.
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!
- xavimat
- Eileen-Class Veteran
- Posts: 1460
- 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: pilgrimcreations
- Location: Spain
- Contact:
Re: if-condition doesn't recognize certain float additions?
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 ).
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!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Who is online
Users browsing this forum: No registered users