• 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 /
  • Help Room /
avatar image
0
Question by Arkshija · Oct 26, 2017 at 08:34 PM · operatordoublecalculations

Problem operating with doubles

Probably will sound a stupid question but I have:

 double value = 0;
 value = 2 - (10 / 20);
 return value ; // return 2

and testign with d

 double value = 0;
 value = 2 - (10 / 20d);
 return value ; // return 1.5

How should I work with doubles? have to add "d" next to a value with a possible decimal result??? How this exactly works? I dont want to check AAAAALLLL my code and testing each operation!

Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MacDx · Oct 26, 2017 at 09:19 PM

Yes, working with doubles is the same as working with floats, you need to be careful with how you write the numbers in your operations.

The fact is that in your first version:

 double value = 0;
 value = 2 - (10 / 20);
 return value ; // return 2

You have a double variable however your entire operation, ( 2 - ( 10 / 20)) is working only with integers, since you aren't specifying a "f" for float or a "d" for double. 10 / 20 is an integer division not a float, nor a double division, and from an integer point of view, 20 cannot divide 10, there's no way you can fit a 20 inside a 10, so you get 0 as a result. 2 - 0 = 2 so you get a 2 that is an integer and then C#, with enormous kindness and understanding, automatically casts your result int into a double so it can be stored inside your variable of type double, instead of throwing an type error.

In your second version when you add the "d":

 double value = 0;
 value = 2 - (10 / 20d);
 return value ; // return 1.5

You are making the 20 be represented as a double, that means that C# will cast the 10 to a double so it can perform the division. The result of the division will now be a double (which is 0.5 since you can only fit half of a 20 inside a 10) and that means that the result of the subtraction will also be turned into a double.

So, to sum up and answer your question:

Do I have to add "d" next to a value with a possible decimal result???

The answer is yes. You have to get into the hobby of adding a "d" (or an "f" if you want to use floats instead) when you are dealing with numbers that you expect to have decimal places.

Comment
Add comment · Show 1 · 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 Arkshija · Oct 26, 2017 at 10:06 PM 0
Share

Haha ty, then I was right Q_Q I willl have to check everything now...

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

118 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 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

does unity work with linux (ubuntu) 0 Answers

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

How do I my operators properly in Unity in c# 0 Answers

Can't seem to round to 2 DP 0 Answers

parsing a double to bigint also getting playerprefs bigint 0 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