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
4
Question by Afropenguinn · Sep 05, 2013 at 05:03 AM · variablesfloat

Why do I need the "f" when using a float?

The variable "speed", near the bottom when I subtract .5 from it it requires that "f", why is that? It was already declared a float in the beginning of the code.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     float speed = 0;
     float max_speed = 5;
     float accel = 1;
     
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetKey (KeyCode.W))
         {
             if (speed < max_speed) { speed += accel * Time.deltaTime; } else { speed = max_speed; }
             transform.Translate (0, 0, speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.S))
         {
             transform.Translate (0, 0, -3 * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.A))
         {
             transform.Translate (-3 * Time.deltaTime,0 , 0);
         }
         if (Input.GetKey (KeyCode.D))
         {
             transform.Translate (3 * Time.deltaTime,0 , 0);
         }
         
         if (speed > 0) { speed -= .5f * Time.deltaTime; }
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by whydoidoit · Sep 05, 2013 at 05:04 AM

You need it because in C# .5 is a double not a float and the compiler will complain that you are losing precision. .5f indicates a float and so it is the same type.

Comment
Add comment · Show 5 · 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 robhuhn · Sep 05, 2013 at 07:03 AM 1
Share

Just in addition - Here is a table for all value types and its suffix: http://msdn.microsoft.com/en-us/library/bfft1t3c.aspx

avatar image Afropenguinn · Sep 05, 2013 at 01:56 PM 0
Share

So is there a point in declaring it a float then? It seems redundant to declare it one type and declare it again later on. I apologize, I only have experience in C++. And do I need to use that for any decimal number then?

avatar image Jamora · Sep 05, 2013 at 02:05 PM 1
Share

The compiler accepts speed as a float, because you declared it as one. The problem is that .5 and .5f represent different types, which take up different amounts of memory (thus offering different precision). If you mix and match these types the compiler wants to make sure you know what you're doing by giving you a warning.

The float literal is also used in C++ to explicitly state a number to be a float.

You have to do it for every hard-coded decimal number you want to be considered a float. An alternative (which I've never tried) is to use the more cumbersome (Float).5;.

avatar image robhuhn · Sep 05, 2013 at 02:10 PM 0
Share

This issue doesn't occur because the compiler doesn't know the type of speed - it knows that speed is a float. The point here is that you try to assign a double to a float and an implicit cast is not possible in this case. Some examples where speed is declared as float:

 float myFloat = speed / 5; //float devided by an int will return a float

 float myFloat = speed / 0.5; //float devided by a double will return a double - that will be an issue

 float myFloat = speed / 0.5f; //float devided by a float will return a float
avatar image Afropenguinn · Sep 05, 2013 at 02:12 PM 0
Share

I see what your saying. I have a square container (float) and I am trying to fit a round peg into it (double). Thanks for the help everyone!

avatar image
3

Answer by Nexum1 · Sep 05, 2013 at 02:06 PM

Your declaration that the variable speed is of type float. It assigns the value 0.5 to this variable.

According to the C# specification, a number containing a decimal point that doesn't have a suffix is interpreted as a double.

So we now have a double value that we want to assign to a variable of type float. In order to do this, there must be an implicit conversion from double to float. There is no such conversion, because you may (and in this case do) lose information in the conversion.

The reason is that the value used by the compiler isn't really 0.5, but the floating-point value closest to 0.5, which is 0.4999999999999978655962351581366... for double and exactly 0.499999946057796478271484375 for float.

Strictly speaking, the f is not required. You can avoid having to use the f suffix by casting the value to a float:

 float speed= (float)0.5;
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

20 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

Related Questions

Scaled Variables 2 Answers

HOW TO ADD AND REMOVE VARIABLES ALREADY DECLARED IN A LIST 1 Answer

How do I add something on to a float. 1 Answer

random respawn and respawn delay 1 Answer

Float randomly being set to 0 spontaneously 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