View Full Version : C hacker needed for easy quesiton
IronManX
03-14-2002, 03:23 PM
How do I check to make sure a number is an integer?<br />For a condition?<br /><br />clock is ticking on my homework :/
Hornication.com
03-14-2002, 03:47 PM
Truncate it a then check for a remainder. Sorry, I'm not a C programmer, but I can do it in Perl which should be very similar.<br /><br />$x = 5.3;<br />$y = int($x);<br />if ($y == $x){<br />execute something<br />}<br /><br />The if block will only be executed if $x is a whole number. Alternatively, you could route to an error message if $x is not whole.<br /><br />$x = 5.3;<br />$y = int($x);<br />if ($y != $x){<br />error code<br />}
Hornication.com
03-14-2002, 03:53 PM
Sorry, I meant to say truncate it and then compare to the original to see if they're equal. It's not really checking for a remainder.
AgentCash
03-14-2002, 04:11 PM
Hey Horni, Why not do it the correct way?<br /><br />$x = 5.5;<br />print "Variable is an integer!\n" if ($x =~ /^[+-]?\d+$/);<br /><br /> <img border="0" alt="[Finger]" title="" src="graemlins/finger.gif" />
IronManX
03-14-2002, 05:23 PM
Na I don't have that integer function in c
AgentCash
03-14-2002, 05:33 PM
I would tell you IronMan, but I stopped doing other people's homework when they stopped being able to beat me up <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" />
IronManX
03-14-2002, 05:46 PM
okay here is what I've come up with<br /><br />(number % 2 == 0)<br /><br /> <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" />
IronManX
03-14-2002, 06:06 PM
At least I can get even numbers <img border="0" title="" alt="[Smile]" src="smile.gif" />
Hornication.com
03-14-2002, 06:22 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by AgentCash:<br /><strong>Hey Horni, Why not do it the correct way?<br /><br />$x = 5.5;<br />print "Variable is an integer!\n" if ($x =~ /^[+-]?\d+$/);<br /></strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">Yup, that works, too. There's always more than one way to do it. We could probably think of a few dozen more, as well. But since yours is the "correct way" I guess I'm missing what was incorrect about mine. Works fine on my computer.<br /><br />Oh well.
AgentCash
03-14-2002, 06:30 PM
Hmm... you mustn't be a true perl hacker.... you know it's the correct way if it's the shortest, most obfuscated code around <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" />
reyes
03-14-2002, 09:01 PM
Sorry but Im not sure to understand your question.. There is an huge difference in variable declarations of c and perl..<br /><br />You can't assign a float to a variable declared as int, and an int to a variable declared as float..<br /><br />If you're looking to get an int from a float, you can cast it to an int, you will lost the fractionnal part..<br />==================<br />int x<br />float y=3.1416;<br /><br />x=(int)y;<br />==================<br />x is now 3..<br /><br />Best Regards
vBulletin® v3.7.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.