Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DeadlyGoat · Oct 12, 2017 at 07:30 PM · transformpositionprogramming

Does it make any difference whether position is an integer or a float?

Does it matter whether the position, rotation, scale or anything else is set to 1 or 0.23452345...? I'm wondering this since the number '1' uses less memory to store, and thus it requires less time to get the information when the scripts are run. I usually try to restrict the use of floats, or at least round the number "0.259999" up to "0.26". Does this actually make any difference in any way? Or am I just wasting my time doing it?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bobisgod234 · Oct 13, 2017 at 03:37 AM 0
Share

Floats in C# are 32 bits, therefore both the number "0.23452345" uses 32 bits of memory, and the number "1" also uses 32 bits of memory.

Ints in C# are also 32 bits, so the number "0" as an Int, is still 32 bits.

Perfor$$anonymous$$g operations on Int's is generally faster than floats, but then it only works if you are actually operating on int types, just rounding a float to a whole number and storing the result as a float won't do anything.

See here for more details

avatar image unit_nick · Oct 13, 2017 at 03:52 AM 0
Share

Short answer. Probably just wasting your time.

Position, rotation, & scale all use floats so I can't see what you might gain from this.

If your project is complete and working and you need to optimize it then sure you could go back and look to see if some numbers could be rounded out. But remember that the process of rounding is extra code.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by space928 · Oct 12, 2017 at 07:39 PM

Well, floating point numbers have 32 bit precision, meaning in memory they take up 32 bits (+ a bit of overhead). Rounding the number just means that the rest of digits are 0s (so rounding 0.259999 results in 0.260000...). Unless some very clever memory management is used, then it will take up the same amount of space. But either way unless you need to store like millions of floats (which would still only be in the megabytes/mebibytes) so worrying about this isn't always important. The performance difference difference between floats and ints on the CPU (not the GPU which happens to be the other way round) is noticeable.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DeadlyGoat · Oct 12, 2017 at 07:44 PM 0
Share

Alright, so to optimize the scripts and the game I should stick to ints, correct?

avatar image space928 DeadlyGoat · Oct 13, 2017 at 04:40 AM 2
Share

Yeah, but modern CPUs are quite fast with both floats and ints so the difference is only small. When trying to optimise your game, try and focus on things like redundant iteration, which will take up much more time than floats versus ints.

avatar image Bunny83 DeadlyGoat · Oct 13, 2017 at 07:48 AM 3
Share

No. You seem to have a very strange view on how a number is actually stored in memory. The actual value of the number doesn't matter at all. Only the used variable format dictates how much memory is needed. A normal "int" is a 32 bit integer value. It always requires 4 bytes of memory no matter if you store the number "0", "42" or "2147483647" it will always use those 4 bytes.

Likewise a single precision float also uses 32 bits and also requires 4 bytes at all times, no matter what value you store in it. It can be "0", "42.12345", "1.984565E45", "2.5E-32" or "+inf"

The next thing is certain floating point values can't be rounded to a better representation. For example the value 0.26 can't be represented as a binary number. The closest value below 26 is "0.2599999904632568359375" and the next larger number would be "0.2600000202655792236328125". Typically the first value will show as "0.26" as it's rounded after the 8th or 9th place.

If you're interested in how the IEEE 754 format works, here's an interactive converter and here's the wikipedia article

Choosing which datatype you want to use is mainly a matter of what you're going to do with it. For example representing a position with an integer wouldn't make much sense unless you don't want smooth movement but only jump from one grid position to the next.

However under the hood Unity uses floats for almost everything. So using a different data type in your own structures makes no sense as in the end it has to be converted into a float anyways. Also your GPU almost exclusively works on 32bit floating point and fix point values.

@space928: What kind of "overhead" do you mean? 32 bits are 32bits nothing more, nothing less. It uses 23 bits as mantissa, 8 bits as exponent and 1 sign bit which makes up 32 bits.

avatar image
3

Answer by DaDonik · Oct 13, 2017 at 06:44 AM

Float and int use the same amount of memory (32bit).

If you are working with transforms, the position, rotation and scale already are floats. That means that you need to constantly cast between int and float (happens implicitly), which is guaranteed to be slower than using floats only.

I like the fact that you think about performance, but this won't save you even a single CPU cycle.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Moving object with transform.position ignore other objects even if they collided 1 Answer

Need enemy to follow only active character 0 Answers

Drawing a (debug) line between anchored UI element and mouse? 0 Answers

Using Dictionaries to store and retrieve GameObjects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges