Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 naka88sushi_ · Sep 28, 2014 at 04:53 PM · syntax

what is the difference between "0.2f" and "0.2"?

I've seen this in a lot of tutorials, i mean, what is it actually? and when is the right time to use it?

Comment
Add comment · Show 1
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 Symphony · Sep 28, 2014 at 04:59 PM 0
Share

The F is for float..it's a numerical variable type. You use floats when whole numbers won't work.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Owen-Reynolds · Sep 29, 2014 at 03:13 PM

The real reason is a throwback to the 60's, when we had to be obsessed with how much space everything took. Many "types" were just alternate ways to specific an integer or a floating point number, using just barely enough space as we needed.

For example, regular integers use four bytes, going from about -2 billion to +2 billion. Characters are stored in only 1 byte, 0-255. So, in the old days, if you had a number going from, say, 0 to 150, you wrote unsigned char n;, and saved three bytes. You were abusing a 0-255 character slot, to store a number, like selling mini-donuts in an egg carton.

If you knew a number would only be +/- a few thousand, you used short int n;. That said to store n in only 2 bytes. Or if you knew it would be only positive 0 to less than 60K, you used unsigned short int n;. Unsigned means the negative numbers were repurposed as positives, and it could count twice as high.

Originally, floating-point (decimal place) numbers were stored in 2 bytes, giving 7ish significant digits. In other words, 1/3 was 0.3333333, and then trailed off as either zeros or random numbers. This was considered pretty accurate. We called them floats: float n; But, just in case, a double-precision floating point type was made, using 4 bytes, and 12ish(?) significant digits.

When memory suddenly became a thousands times cheaper, all those tricks became mostly pointless. Everyone was taught to use double n; to get the most accurate results. And float n; became a thing of the past. By the time C# came along, they decided that a regular written-out number should count as type double.

In other words, 1.0 is really four btyes: 1.00000000000, and 1.0/3.0 is four bytes: 0.333333333333.

They decided that if, for some insane reason, you cared about saving 2 bytes and wanted a float, you could put dot-f at the end. So 1.0f is really two bytes holding 1.0000. And 1.0f/3.0f is merely 0.333333.

Unity comes along, with C# for games. In games, getting a decent frame rate is much more important than accuracy, and less memory used means a little more speed. Plus memory is important in cell phones. So Unity breaks out the old, obscure float to store everything, with that dot-f on the end which we thought no one would ever need anymore.

So, here's the error: float n; n = 1.0;. Now, that 1.0 is really a four-byte 1.0000000000. Since n is a float, it can only hold 1.00000. The computer would have to chop off the last six zeros to make it fit. You know they are only zeros, so won't matter, but the computer doesn't. It could be chopping off 4's or 9's, so it gives you all those "can't put double in float" errors.

Comment
Add comment · Show 2 · 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 Dave-Carlile · Sep 29, 2014 at 03:39 PM 0
Share

Good information, but some is inaccurate for C#. Floats are 32 bits (4 bytes), Doubles are 64 bits (8 bytes).

avatar image TheInterloper · Sep 09, 2017 at 03:33 AM 0
Share

This is one of the best explanations I've read on these threads that can be interpreted by new users interested in the differences between floats and doubles in Unity

avatar image
0

Answer by Louis Watson · Sep 29, 2014 at 03:36 PM

If your talking in the context of c# and sending a parameter to a method/function 0.2 is implicitly cast to a double at compile time, whilst the f on 0.2f instructs the compiler to cast the number as a float.

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

6 People are following this question.

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

Related Questions

Public 2D array, accessed through the editor ?? 3 Answers

What is a good Javascript editor, with Syntax highlighting and checking? 5 Answers

3 errors do not know how to fix... 1 Answer

Teleporter 2 Answers

iTween Path (NOT visual editor) 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