image_xscale becomes decimal

Discussion in 'Programming' started by krugen, Aug 15, 2019.

  1. krugen

    krugen Member

    Joined:
    Jan 24, 2019
    Posts:
    156
    I setup an array of value for setting the image_xscale start from 1 to 0.1, decrement of 0.1.

    image_xscale = array[index];

    so what I expect is that as the loop continue, the image_xscale should follow the array value, but what I get is decimal slightly smaller than the value I set it to, why is that?

    Is there a way to stop this from happening?
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,985
    Why do you think that is happening and what does your code look like?
     
  3. ThePropagation

    ThePropagation Member

    Joined:
    May 16, 2017
    Posts:
    120
    Why do you need an array? just say if image_xscale > 0.1 image_xscale -= 0.1; else image_xscale = 1;
     
  4. krugen

    krugen Member

    Joined:
    Jan 24, 2019
    Posts:
    156
    I tried this before the array and I get decimal numbers.
    I then used array but I still get decimal numbers.
    It's not that I did not did what you suggested.
    I did.
     
  5. ThePropagation

    ThePropagation Member

    Joined:
    May 16, 2017
    Posts:
    120
    so do you want decimal or not? I don't understand what you want if you're subtracting by decimal values you're going to get a decimal value
     
  6. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,155
    You subtract a decimal value from another value and your result is a decimal? I can't possibly imagine why? :p

    Maybe you should tell us what your interpretation a decimal number is?
     
  7. krugen

    krugen Member

    Joined:
    Jan 24, 2019
    Posts:
    156
    Sorry, what I mean is:

    originally:
    image_xscale = 1;

    image_xscale -= 0.1; (give me 0.90000133433434)

    and becoz of that, if image_xscale == 0.9 does not work

    is there a way to prevent getting 0.900000010203020?
     
  8. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,985
  9. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,155
    That's how 'double precision' works. Which is how computers work.

    I'd never recommend comparing against exact values as they will no doubt be stored differently internally. I'd compare with greater or less than.
     
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,791
    You start with a value of 1. You subtract 0.1 from that value, you expect 0.9. so traffic is 0.1 again you expect 0.8. the problem is you are not actually working with decimals, you are working with floating points. Most of those values are not compatible with floating points; you can get a damned near close approximation, but rarely that exact number. that is likely what you are seeing in the discrepancies.

    If for whatever reason you need more precision, set your values to whole numbers and then divide by 10.
     
  11. I'm lost

    I'm lost Member

    Joined:
    Jul 11, 2018
    Posts:
    19

    Youre doing HTML export, aren't you?
    Beacuse this html export is having this issue for a long time where 1 + 1 is not equal to 2, but to 2,00000001961561

    Rounding seems like the only solution
     
  12. krugen

    krugen Member

    Joined:
    Jan 24, 2019
    Posts:
    156
    Got it. You're a lifesaver!
     
    I'm lost likes this.
  13. I'm lost

    I'm lost Member

    Joined:
    Jul 11, 2018
    Posts:
    19
    You're welcome.

    Good luck with your game ;)
     

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