Search Unity

:int and :float

Discussion in 'Scripting' started by Shonysky, Dec 22, 2014.

  1. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    why should i use a float variable instead of an int variable?
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    194
    Simply put, integers are whole numbers (1, 2, 3, 4 etc). Floats aren't required to be whole numbers (1.1, 1.2, 1.3, etc), but they can be treated so.

    Floats take more work to compute, but it's probably not something anybody would ever notice. If you know your number is always going to be whole, use an integer. If your number isn't always going to be whole, use a float.
     
  3. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    Sometimes the decision is made for you: even if you think the variable will only hold whole numbers, if the only thing you ever do with it is pass it into some functions that all require floats (Physics functions for example), then it might be simpler to just store it in your class as a float as well.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Quick thoughts on comparison

    int
    • Can only store whole numbers
    • Has perfect precision across entire range
    • Limited range -2,147,483,648 to 2,147,483,647
    • Perfect for counts, indexes and the like.
    float
    • Can store decimals
    • Precision is related to magnitude. 7 digits only.
    • Large, but still limited, range -3.4 × 10^38 to +3.4 × 10^38
    • Almost all unity functions accept floats
    That said there are many other data types out there. decimal, double, long, half... They all have slightly different advantages and disadvantages.