Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jul 05, 2014 at 01:09 PM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
6
Question by Tofudude624 · Mar 08, 2012 at 02:21 AM · mathoafa

Determining if a variable is even or odd?

Hello. If I have a variable is a whole number, how can I set up and if statement to see of the variable is even or odd? such as:

 if (TEST==odd ){something};
 if (TEST==even){something else};

Thanks

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

  • Sort: 
avatar image
20
Best Answer

Answer by Tseng · Mar 08, 2012 at 02:28 AM

Use modulo operator

 if(test%2==0) // Is even, because something divided by two without remainder is even, i.e 4/2 = 2, remainder 0
 if(test%2==1) // Is odd, because something divided by two with a remainder of 1 is not even, i.e. 5/2 = 2, remainder 1
Comment
Add comment · Show 13 · 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 YunLee · Jan 24, 2014 at 12:44 PM 0
Share

what if a dividend is negative? He said the variable is a whole number.

avatar image MrVerdoux · Jan 24, 2014 at 12:46 PM 0
Share

Then just use $$anonymous$$athf.Abs before.

avatar image YunLee · Jan 24, 2014 at 12:54 PM 0
Share

Very simple, thanks.

if(test%2==1||test%2==-1) Can I use this too?

avatar image Tseng · Jan 24, 2014 at 01:01 PM 0
Share

Just use test%2==0 for even or test%2!=0 for uneven. However, this only works in this special case or if you want deter$$anonymous$$e if a number is fully dividable through another number or not.

avatar image MrVerdoux · Jan 24, 2014 at 01:35 PM 0
Share

in the strange case anyone wanted to see if a float is odd or even, first I would cast it to an int. It makes more sense to me, but some people might still prefer the strange case in which a float is perfectly dividable.

avatar image andibuss23 MrVerdoux · Feb 24, 2021 at 06:30 PM 0
Share

Hi, in my case its very useful because im making a runner where the player can only move to certain positions. Those positions are even coordinates and i only want the player to move when they are in an even position, otherwise they could move in the middle of the lines.

Show more comments
avatar image
3

Answer by firemyst · Jan 16, 2014 at 01:12 PM

Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even.

Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:

 for (int x = 0; x < NumberOfNumbers; x++)
 {
         if (x % 2 == 0)
                total += 1; //even number
         else
                total -= 1; //odd number
 } 

Definitely worth a read for those that are curious.

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

10 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

Related Questions

How to convert Python in to C#? Maths or find Shortest distance between two skew lines in 3D space. 1 Answer

Minimap rotating nodes 1 Answer

Extending a vector? 4 Answers

Convert from m/s to m/fixedFrame 1 Answer

faulty randomness (uniform RNG) 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