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 ina · Apr 09, 2012 at 03:57 AM · c#floatdoubledecimaltypecasting

C# CS0266 error for multiplying by a decimal

I'm not sure why the function below yields this error. It seems to happen because of the *0.5 ... Is it not possible to multiply the sum by a decimal?

error CS0266: Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)

 /*
  * Function: sliderPercentage
  *     returns percentage on transform *t* that we hit with point *p*
  */
 float sliderPercentage(Transform t,Vector3 p,Vector3 dir){
     
     return (Vector3.Dot(dir,p) - Vector3.Dot(dir,t.localScale)*0.5 + Vector3.Dot(t.position, dir)) / Vector3.Dot(dir,t.localScale);
     
 }
Comment
Add comment · Show 6
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 Kleptomaniac · Apr 09, 2012 at 04:05 AM 2
Share

You forgot the f after 0.5. Not sure if that solves the problem though. :)

avatar image fafase · Apr 09, 2012 at 07:03 AM 0
Share

It seems your return is a double while the function returns float. Try with

return (float)(Vector3.Dot(dir,p) - Vector3.Dot(dir,t.localScale)*0.5 + Vector3.Dot(t.position, dir)) / Vector3.Dot(dir,t.localScale);

avatar image Kleptomaniac · Apr 09, 2012 at 07:37 AM 0
Share

@fafase Remember the f! :D

avatar image fafase · Apr 09, 2012 at 11:29 AM 0
Share

you don't really need the f. Putting the .f just tells the compiler that you want a float, not mentioning, the compiler casts to the most appropriate type.

avatar image Kleptomaniac · Apr 09, 2012 at 12:57 PM 0
Share

Right, thanks, slowly learning C# by answering questions wrong and getting corrected ... :D I'm a JS programmer at heart!

Show more comments

1 Reply

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

Answer by Paulius-Liekis · Apr 09, 2012 at 02:55 PM

Change:

  1. to 0.5f or

  • return type from float to double

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 ina · Apr 09, 2012 at 07:56 PM 0
Share

Is it always necessary to append an f to declare a decimal a float?

avatar image by0log1c · Apr 09, 2012 at 08:15 PM 1
Share

C# compiler see any decimal number as a double unless you specify its a float. So yes you need either the f or a (float) casting. Integers will be correctly interpreted as so, as long as you keep it to 1 and not 1.0, for example.

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

8 People are following this question.

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

Related Questions

Convert double to float for Vector3 1 Answer

On the accuracy of floats and doubles: Where/when do rounding errors usually occur, and how can I avoid them? 1 Answer

Extremly long space journey via time stamp 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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