GM:S 1.4 [SOLVED] draw_string draws "0.50" instead of "0.5"

Discussion in 'Programming' started by marasovec, May 29, 2019.

  1. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    304
    Code:
    draw_text(x, y, string(0.5)); // 0.5
    
    var num = 0;
    num += 0.5;
    draw_text(x, y, string(num)); // 0.50
    
    Is there a way to fix this?
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,283
    Repeat after me: I will NOT expect perfection under the decimal point when dealing with floating-point arithmetic.

    Use string_format() to restrict the number of decimal places.
     
  3. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    304
    It works nicely. Thanks!
     
  4. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Except that you do expect perfection when adding 0.5 to 0, as both are exactly representable as floating point numbers and their sum is exactly 0.5.
    This is NOT an issue with floating point arithmetic.

    Actually, you've got it the other way around in your title.
    As the documentation states (https://docs.yoyogames.com/source/dadiospice/002_reference/strings/string.html):
    It is expected to see 0.50 in both cases.
    I currently can't test this myself, but if you're right, it is the result of GameMaker trying to optimize your program by calculating the result of string in a preprocessing step, before running the program, and making a mistake in this calculation.

    If you know beforehand the exact amount of digits you want, then that's indeed the perfect solution.
     
  5. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    304
    I've tested it. (GMS 1.4.9999)
    Also sometimes it shows "-0.0" which is quite interesting :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice