Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This question was closed Sep 29, 2017 at 02:59 PM by whaleinthesea for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by whaleinthesea · Aug 10, 2015 at 12:22 PM · mathdebug.logcalculate0answer

Why say Unity that 320* (1 / 320) = 0?

When I run the code below, I got this "Answer is 0" while it have to be 1. Can anybody tell me how to fix this? Thanks in advance. Code: Debug.Log("Answer is " + 320* (1 / 320));

Even when I use this code, the answer is still 0 Code:` float y = 320 * (1 / 320); Debug.Log("Answer is " + y);`

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

5 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Demonicdaron · Aug 10, 2015 at 12:33 PM

To actually make it work you gotta convert the "1" must be a float, it doesn't matter if the "320" multiplying is float or not. So the correct form is:

 Debug.Log("Answer is " + 320 * (1f/320));

If "1/320" is not a float, you will get a integer result (rounded if necessary), which is what you were getting there:

1/320 = 0.0... which rounded to integer = 0, which multiplied by anything gives you 0

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 Hellium · Aug 10, 2015 at 12:37 PM 1
Share

It works if casting 320 to float.

avatar image
2

Answer by Hellium · Aug 10, 2015 at 12:34 PM

@allenallenallen , you were nearly right !

 Debug.Log( "Answer is " + 320 * ( 1 / (float) 320 ) ); // OR
 //Debug.Log( "Answer is " + 320 * ( (float) 1 / 320 ) );

Other possibility :

 Debug.Log( "Answer is " + 320 * ( 1 / (float) 320f ) );

(float) ( 1 / 320 ) ) converts the result of the integer division (0) to a floating value : 0.0; while ( 1 / (float) 320 ) will give a floating result of the floating division 1/320.0

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
avatar image
1

Answer by allenallenallen · Aug 10, 2015 at 12:24 PM

Because you didn't specify it to be a float so it automatically thinks it's an integer.

320 x (1/320) = 320 x 0 = 0

If you want this to work the way you wanted it to:

 Debug.Log("Answer is " + 320 * (1f/320)); // C# script, not sure about JS

Edited: This one works now.

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 whaleinthesea · Aug 10, 2015 at 12:26 PM 0
Share

Thank for answering so fast! But unfortunately it doesn`t work the answer is still 0.

avatar image Demonicdaron · Aug 10, 2015 at 12:27 PM 0
Share

Nope, your code doesn't work :/ it still outputs zero

avatar image whaleinthesea · Aug 10, 2015 at 12:29 PM 0
Share

How do you add syntax highlighting to your code?

avatar image allenallenallen · Aug 10, 2015 at 12:31 PM 1
Share

To add syntax highlighting, you highlight your code and then click on the 101010 button.

And I tried my own code and didn't work, but I found a way to make it work.

 Debug.Log("Answer is " + 320 * (1f / 320));

Just add 'f' to either the 1 or 320 which would mark that number as a float and force the calculation answer to be a float.

avatar image whaleinthesea · Aug 10, 2015 at 12:35 PM 0
Share

Thank you allenallenallen now it works! :) But if I click the 101010 button, the only I get is: Debug.Log("No syntax highlighting");

avatar image
1

Answer by NeverHopeless · Aug 10, 2015 at 12:33 PM

You should try like this:

 Debug.Log("Answer is " + 320.0f * (1.0f/320.0f));

Specifying f ensures that the number should be treated as a float. So it will preserve the remaining part.

Hope it helps!

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 Demonicdaron · Aug 10, 2015 at 12:35 PM 0
Share

Don't need to make the 320 floats

avatar image NeverHopeless · Aug 10, 2015 at 12:41 PM 0
Share

@Demonicdaron, i personally prefers to keep numbers in a same number system when being manipulate to avoid unusual conditions (e.g., loss of precision) but you are possibly correct that it may not necessary for some cases.

avatar image
0

Answer by RLin · Aug 10, 2015 at 12:31 PM

This likely occurs because floats attempt to round themselves off to be faster and more efficient, but rounding will obviously cause some miscalculations; hence the name floating point precision errors. You could try using doubles instead of floats, or rewriting your statement to (320 * 1) / 320;

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

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

Find position of pixel on sphere given position of pixel on texture 1 Answer

Calculating triangle side length based on hypotenuse? 1 Answer

Subtraction gives completely wrong answers 1 Answer

Math and game development 4 Answers

Calculate Position of Rigidbody after applying Force 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