• 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
Question by Freznosis · Jan 21, 2019 at 12:56 AM · mathmathf.lerpmultiplydivision

Simple math always returns 0

Hello all, I'm trying to do some simple math in a script and then output those numbers. However, the value that is output is always 0 for some reason.

I've triple checked the math and there seems to be no problem with it so I am stumped. Below is the code.

 float carMaxRPM = 8000f;
 int numberOfPoints = 20;
 List<float> listOfPoints;
 
 void CalcPoints(){
 for (int i = 0; numberOfPoints > i; i++)
             {
                 //RPM point is generated based on amount of points for the graph.
                 var rpmPoint = carMaxRPM * (i / numberOfPoints);
                 listOfPoints.Add(rpmPoint);
             }
 }

Any help? I've also tried using Mathf.Lerp(0, carMaxRPM, i/numberOfPoints); but it still returns 0, so I feel I'm doing something very wrong.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by A5B · Jan 21, 2019 at 01:11 AM

The problem is that your code is performing an integer division, rather than floating point division (see here for details). As a result, when you're dividing (i / numberOfPoints), it's going to round down to 0, which is why you're getting zero. You can force it to perform floating point division by adding "(float)" in front of your division:

 var rpmPoint = carMaxRPM * ((float)i / numberOfPoints);


Comment
sean244
Freznosis
kaarloew
Bunny83
KISP

People who like this

5 Show 3 · 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 Freznosis · Jan 21, 2019 at 01:19 AM 1
Share

Oh! This was it. I feel very dumb for missing this.. you learn something new everyday. Thank you for the help.

avatar image Captain_Pineapple · Jan 21, 2019 at 07:09 AM 1
Share

An even smoother way would be to just remove the paranthesis in the calculation. Since carMaxRPM is a float the calculation should then also be executed as a float.

avatar image Naayte · Mar 09, 2020 at 06:48 AM 0
Share

My math is much different, but this just solved such a huge problem for me. I had no idea this was a thing.

THANK YOU so damn much.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

107 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

Related Questions

Lerp that wraps around a variable x 1 Answer

Multiply by float (Double, Decimal) 0 Answers

Unable to fully rotate GameObject on tap 2 Answers

Mathf.Lerp like User Define Function 2 Answers

Mathf repeat at the end? help! 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