ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how can the compiler tell if "a" is integer or not
The compiler knows it's an integer for sure, because you told it so in your code. (by using int literals while not telling it to treat it as a floating point number).
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
C being a language where all variables must be declared and typed, your test doesn't make sense, a is either already, or never will be an integer (i.e. declared as an int).
Perhaps are you asking for some algorithm that will tell is some double precision number is also equal to an integer, in that case look at the ceil and floor functions, but beware that due to rounding errors, you cannot be totally sure.
Also you cannot print a double with %d, you must cast your number to an int, or use %ld as a format.
The modulus operator (%) returns the remainder left after division. So if you have num1 % num2 and the result is 0, then num2 divides evenly into num1.
Well, basicly I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
Originally posted by Mercurius Well, basicly I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
Oh Really?
A number not divedable by 2 is an odd number. Not a prime.
15 is not a prime and not dividable by 2.
You cannot decide if a number is prime without knowing the prime numbers smaller than this one.
All you can do is start computing the prime numbers and check if this one is one of the list or something like that, but you cannot decide without knowing the smaller prime numbers.
It's already proven that this is an unsolvable problem back in the 40's or 50's.
Last edited by perfect_circle; 10-19-2005 at 04:25 PM.
Originally posted by Mercurius
Well, basically I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
Prime? You test woul only show that the number (if an integer) was odd not prime. "Prime" means the the only divisors of the number are 1 and the number itself.
There are many ways to test a number for "primeness," but the number of operation required to do so increases exponentially with the size of the number being tested. (It's an "NP-complete" problem.)
Try "google" for details.
Oh, a quick test for the parity of an integer is to look at its last bit. If it's zero, the number is even, if one, it's odd. That is, for an integer, just use
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.