floor(x) vs int(x)

0 favourites
  • 4 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • All,

    Just out of curiosity, what is the difference (if any) between using floor(x) and int(x)? I noticed they were listed in different sections in the manual.

    Thanks

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Wouldn't floor() round down while int() would do rounding both ways? Int() would also be used to convert other types into integers.

  • int() probably doesn't guarantee that the rounding will necessarily be rounded up or down. I would usually expect an int() function to simply truncate the value -- effectively rounding down -- but it also wouldn't be out of the question for it to round to the nearest integer value instead, and given the behaviour isn't specifically listed in the manual it's also possible that this implementation detail might be changed at some point and should not be relied on to always stay the same.

    However, let's assume that there's no technical difference and that both functions will have the same effect: each function still expresses a different intent, and you should use whichever more accurately expresses what you intend on doing. You int() when you want to convert a value to an integer, round() if you want to round to the nearest integer value, and floor() or ceil() if you'd like to round up or down.

    By choosing a function that clearly expresses your intent you increase readability of your code and reduce the need for explanatory comments.

    <img src="smileys/smiley1.gif" border="0" align="middle" />

  • I think they're identical. The reason they both exist is mainly for consistency. There are three convert-to-type operators: int(), float() and string(). There are also three rounding functions: floor(), ceil() and round(). If you've come from other programming languages or tools you might expect both sets of three to be present. It just so happens int() is identical to floor() in implementation, but I kept them so people don't wonder why the int() conversion operator is missing or why there's no floor() when there's ceil().

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)