vickerps Posted November 30, 2004 Share Posted November 30, 2004 How can i get autoit to know whether a number is odd or even? Link to comment Share on other sites More sharing options...
erebus Posted November 30, 2004 Share Posted November 30, 2004 (edited) First of all try by reading the help file.. save these forums as the last resort (as someone else has already said...) From the Mod() function's help reference: $n = 18 If mod($n, 2) = 0 Then MsgBox(0,"", $n & " is an even number.") Else MsgBox(0, "", $n & " is an odd number.") EndIf $x = mod(4, 7);$x == 4 because the divisor > dividend $y = mod(1, 3/4);$y == 0.25 because the divisor is a float Edited November 30, 2004 by erebus Link to comment Share on other sites More sharing options...
the_lord_mephy Posted November 30, 2004 Share Posted November 30, 2004 (edited) I'm not exactly sure what Mod() does, but You could write it in your own function, Func OddEven($num) If StringInStr($num, ".") = 0 Then $lastdig = StringTrimRight($num, 1) If $lastdig = 1 or $lastdig = 3 or $lastdig = 5 or $lastdig = 7 or $lastdig = 9 Then Return 1 EndIf Else Return 0 EndIf EndFunc I'm not sure what decimals are considerred lol but that should return 1 if the number is odd and 0 if the number is either even or containts a decimal =\ I didn't test but it should work Edit: Ah after looking at the helpfile, I know what mod does =\ That works too. Edit2: Added EndFunc =-o Edited December 1, 2004 by the_lord_mephy My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote] Link to comment Share on other sites More sharing options...
killaz219 Posted November 30, 2004 Share Posted November 30, 2004 (edited) Your funcs a little messed up, you put endif instead of endfunc (or you just forgot the endfunc) to start Also for me, it just dosnt work. Try this and you'll see what i meen MsgBox (0, "", Oddeven("1")) Func OddEven($num) If StringInStr($num, ".") = 0 Then $lastdig = StringTrimRight($num, 1) If $lastdig = 1 or $lastdig = 3 or $lastdig = 5 or $lastdig = 7 or $lastdig = 9 Then Return 1 Else Return 0 EndIf EndIf EndFunc Edited November 30, 2004 by killaz219 Link to comment Share on other sites More sharing options...
JSThePatriot Posted November 30, 2004 Share Posted November 30, 2004 Since that wouldnt be totally accurate as a number with a . in it isnt even you should do it as follows... MsgBox (0, "", OddlyEven("1")) Func OddlyEven($num) If Not(StringInStr($num, ".")) Then $lastdig = StringTrimRight($num, 1) If $lastdig = 1 or $lastdig = 3 or $lastdig = 5 or $lastdig = 7 or $lastdig = 9 Then Return 1 Else Return 2 EndIf ElseIf StringInStr($num, ".") Then Return 0 EndIf EndFunc Hope this helps some, JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008Â Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
killaz219 Posted November 30, 2004 Share Posted November 30, 2004 JS, your script still returns the wrong number for odd values Link to comment Share on other sites More sharing options...
Matt @ MPCS Posted November 30, 2004 Share Posted November 30, 2004 Why would anyone write an Odd/Even function unless it used mod? erebus is entrily correct. @the_lord_mephy - The modulus (mod()) function returns the remainder of the division of the two parameters (only real numbers accepted). In erebus's example, if you divide the number by 2 and the remainder is 0 the number is even, if the number is greater than 0 then the number is odd. This can also be used to find multiples. (e.g. 6 is a multiple of 3 because mod(6, 3) = 0) I hope this helps clear things up a bit. *** Matt @ MPCS Link to comment Share on other sites More sharing options...
CyberSlug Posted November 30, 2004 Share Posted November 30, 2004 Why would anyone write an Odd/Even function unless it used mod?<{POST_SNAPBACK}>Why not? Func isEven($x) If $x/2 = Int($x/2) Then Return 1 EndFunc ;Example For $i = 0 to 10 If isEven($i) Then MsgBox(4096,"", $i & " is even") Else MsgBox(4096,"", $i & " is odd") EndIf NextMod would be my first choice, though. Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Wolvereness Posted December 1, 2004 Share Posted December 1, 2004 (edited) Func wOddEven($wNum) ; -1 = ERROR, 1 = ODD, 2 = EVEN If $wNum = 0 OR StringInString($wNum,'.') <> 0 Then Return -1 ElseIf StringInString($wNum / 2,'.') <> 0 Return 1 Else Return 2 EndIf EndFunc Edited December 1, 2004 by Wolvereness Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote] Link to comment Share on other sites More sharing options...
the_lord_mephy Posted December 1, 2004 Share Posted December 1, 2004 Yeah matt I realized what it did after I posted my func, you must've not seen my edit =\ My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now