Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Responsive] Set macro issues - += 0.1 adds 0.09999999999999 instead

edited August 2014 in Help! with 1.x
I'm not sure how to properly verbalise this issue, but my current project involves a variable called $PlayerHeight and I am having trouble adding sub 1 decimal places to it.

Specifically, say I start with a $PlayerHeight of 6.0 and I want to increase it by 0.1 every time the player enters a particular zone.

For the most part, the player character will turn into 6.1 the first visit and 6.2 on the second.

However, the third visit and onward will leave me with values like 6.2999999999999 and 6.3999999999999.

I've tried testing it with dummy variables, and this occurs across all my game files. I've started with 0 and constantly added 0.1 to it and each time I will eventually land up with something like 0.699999999999.

So, is there anything I can do to keep all my variables at a single decimal place? Or if this is a known issue, is there anyway around it? I've tried searching through the forum and have yet to find anything similar.

Comments

  • Welcome to the exciting world of floating-point math.  JavaScript (and that's really what you're dealing with under the hood with Twine) has no fixed-point real type, only floating-point.  Trying to use floating-point math for what you're doing isn't a particularly good idea for various reasons (noted in the paper linked above), most notably because of the inherent rounding issues present in floating-point math.

    Anyway.  You're probably better off using integers here and simply dividing by 10 when printing to get your desired result.  For example:

    /% Set initial $PlayerHeight to 60. %/
    <<set $PlayerHeight to 60>>

    /% Add 1 to it several times over the course of entering your zones. %/
    <<set $PlayerHeight += 1>>
    <<set $PlayerHeight += 1>>
    <<set $PlayerHeight += 1>>

    /% Simply divide by 10 whenever you need to (e.g. when printing it for the player). %/
    <<print $PlayerHeight / 10>> /% Should output: 6.3 %/
    Ideally, I'd suggest recording height in inches/centimeters, not feet/meters/whatever, and converting to the appropriate user-friendly form whenever you need to.  However, just going with the above example will, at least, get you what you wanted.
  • TheMadExile wrote:

    Welcome to the exciting world of floating-point math.  JavaScript (and that's really what you're dealing with under the hood with Twine) has no fixed-point real type, only floating-point.  Trying to use floating-point math for what you're doing isn't a particularly good idea for various reasons (noted in the paper linked above), most notably because of the inherent rounding issues present in floating-point math.

    Anyway.  You're probably better off using integers here and simply dividing by 10 when printing to get your desired result.  For example:

    /% Set initial $PlayerHeight to 60. %/
    <<set $PlayerHeight to 60>>

    /% Add 1 to it several times over the course of entering your zones. %/
    <<set $PlayerHeight += 1>>
    <<set $PlayerHeight += 1>>
    <<set $PlayerHeight += 1>>

    /% Simply divide by 10 whenever you need to (e.g. when printing it for the player). %/
    <<print $PlayerHeight / 10>> /% Should output: 6.3 %/
    Ideally, I'd suggest recording height in inches/centimeters, not feet/meters/whatever, and converting to the appropriate user-friendly form whenever you need to.  However, just going with the above example will, at least, get you what you wanted.


    Unfortunately the game I'm making requires feet to be used (I'm a metric person myself). However your solution is flawless! Thank you so much!  ;D
Sign In or Register to comment.