LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to test if a decimal is even or odd in labview

i made a VI that tests for even and odd integers using the quotient/remainder function and the Select function but when i test a decimal it does not work properly.

 

i have my VI testing to see if the input diveded by 2 gives you a remainder equal to 0, and if it does then it is even and the VI displays a messege saying "Even", but when i put in a decimal ( for example 7.2) the remainder is spitting out 1.2 which is not equal to 0 making my VI not work properly, if anybody can help me with this i would greatly appreciate it and if you could attached a photo that would help me understand what my mistake is, Thanks i have attached my VI for you to look at

0 Kudos
Message 1 of 6
(7,118 Views)

Decimal number cannot be odd or even.  A number has to be an interger in order to be even or odd.  

 

http://www.mathsisfun.com/definitions/even-number.html

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 2 of 6
(7,110 Views)

As has been already said, odd/even is only defined for integers, so you need to tell us what you expect for e.g. 7.2.

 

  • Do you want to know if the last decimal digit is even or odd?
  • Do you want to know if the nearest integer is even or odd?

 

While you are at it, you might also think a bit more about your program design:

  • What is the purpose of your "select" primitive? What do you think would be different if you just leave it out completely?
  • Why do you test for "equal zero" and "not equal zero"? One is just the inversion of the other and knowing one also determines the other automatically.
  • Why do you use two dialog boxes? Do one comparison and use "select" to switch between the two messages. Only one express dialog needed.
  • Don't mix blue and orange. Select the correct representation for your diagram constants.
  • There is a primitive for "equal zero"
  • Never use equal and not equal comparison with orange numeric data (DBL). You might not get expected results.

 

Message 3 of 6
(7,104 Views)

yes having it test to see if the last decimal is even or odd will work but im not sure how i would go about doing that ?

0 Kudos
Message 4 of 6
(7,092 Views)

Again, the "last decimal" has no meaning unless you decide on a given decimal resolution. If your number is accurate to exactly one decimal digit, you could multiply it by 10, round to the nearest integer, then check for even/odd. See how far you get.

 

Message 5 of 6
(7,090 Views)

Check the vi that I have attached. It will tell you whether the last digit in a number (integer or decimal) is even or odd. You don't need to worry about the length of the decimal fraction. It utilizes strings to separate the fractional part and then finds the last non-zero digit in the fractional part. It will work up to 15 digits after the decimal but as you know, with floating point (doubles), as the significand value increases, the precision of the fractional part decreases.

 

Hope this helps!

 

Cheers

Mani

 

Kudos are the best way to say'thank you'!

Message 6 of 6
(7,086 Views)