I know how to validate input, but how can I check if
is an integer and not a float?Code:int ans = num1 / num2; // all integers
This is a discussion on check value within the C++ Programming forums, part of the General Programming Boards category; I know how to validate input, but how can I check if Code: int ans = num1 / num2; // ...
I know how to validate input, but how can I check if
is an integer and not a float?Code:int ans = num1 / num2; // all integers
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
It will be an integer no matter what, because integer division always results in an integer. To check to see if there would be any remainder, you could use the modulus operator %. Any non-zero result from num1 % num2 means there would be a remainder after the division.
Check if num1 is perfectly divisible by num2.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
And check num2 isn't zero before doing the division.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thx for the replies! I used it for a source code to calculate prime numbers. The user can input a number where to start searching, everytime the program encounters a prime number: it says "number" is a prime number. However, I want to search veeeeeeery big numbers.
But the max. integer value is 2147483647. Is there any way I can exceed that limit?
This is the source code:
Code:#include <iostream> #include <windows.h> using namespace std; void prime(int pm) { system("cls"); int ans = 0; bool prime = true; if(pm == 1) { prime = false; } for(int i = 2; i < pm; i++) { if(pm % i > 0) { continue; } else { prime = false; break; } } if(prime) { cout << pm << " is a prime number!\n\n"; system("PAUSE"); } } int main() { int i = 0; cout << "insert start number (integer, cannot be larger than " << INT_MAX << "): "; cin >> i; while(i < 1 || i > INT_MAX) { cout << "Number cannot be smaller than 1 and cannot be larger than " << INT_MAX << "! Please re-enter: "; cin.clear(); cin.ignore(); cin >> i; } for(i; i < INT_MAX; i++) { prime(i); } Sleep(5000); return 0; }
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Use unsigned int, but then the max would probably be 4294967295 for you, which is still pretty small. You could give the GMP library a try.But the max. integer value is 2147483647. Is there any way I can exceed that limit?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
on some implementations, unsigned long long will give you a 64-bit int.
Originally Posted by laserlight
GMP:
Hmm...Originally Posted by http://www.swox.com/gmp/guestaccount.html
I also made a prime calculator once, but it stuck to the 2^32-1 limit. You can make your own simple class (struct if you use C) to handle bigger numbers, though.
Your code is damn slow, it runs at O(n^2) (at best reduced to O(n^2 / log n) ) where n is the prime size, which is very very bad. You will never reach 2147483647 in any case within a week of running that, so don't bother.
The following code only tests divisions up to the number's square root, which is sufficient. It also uses primes calculated previously instead of dividing by every integer from 2 onwards. Another minor improvement is testing only odd numbers (after 2 that is).
This calculates a set number of primes, not primes up to a limit, but I'm assuming you can adapt this to your own needs
This will perform 1,000s of times faster (no kidding!) than yours at high numbers, say primes up to 10,000,000.
Code:cout << "How many primes? "; cin >> num; //allocate enough memory unsigned int* primelist = new unsigned int[num]; //init first prime primelist[0] = 2; //number of primes int index = 1; //first test number unsigned int test = 3; //largest prime calculatable, used to determine limit unsigned int maxprime = 4294967291; //loop to get primes while(test <= maxprime && num > index) { int i = 0; //while each existing prime in list does not divide evenly into test number //only tests up to square root of test number while(test % primelist[i] && test >= primelist[i] * primelist[i]) { //go to next prime to test i++; } //if we've completed test above up to the square root, then test number is new prime if(primelist[i] * primelist[i] > test) { //add to number of primes index index++; //add number to primes list primelist[index] = test; } //only need to test odd numbers test += 2; //reset i to test new number }
Last edited by jafet; 03-18-2006 at 03:14 AM. Reason: big-o stuff
Installing a new library might be hassle for a beginner. You can always use a 64-bit integer by using whatever type(s) supported by your compiler which could be long long, int64, __int64 or something completely different. Unsigned 64-bit integers have a ceiling of 18446744073709551615 which is pretty neat. If that's not enough you could try a 128-bit int if your compiler has support for it.
Thx for all the replies!
@ jafet:
I modified my source, everytime the program runs, it asks for a number where to begin searching.
I'll try your way too!
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Your program crashes often! And mine goes faster, I started yours and 30 secs after it I started mine (from number 2). Mine cathed up with yours.
If I input large numbers your program crashes.
Edit:
When I input 6, your program crashes too. And it skippes 2, but 2 definitly is a primenumber!
Edit2:
I improved my program:
checks if unsigned int i is even or odd. The program has become faster.Code:for(i; i < UINT_MAX; i++) { if(i % 2 > 0) { prime(i); } else if(i == 1) { continue; } }
Last edited by Ideswa; 03-18-2006 at 07:47 AM. Reason: add things
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
jafet, the GMP library can be used on MS Windows, just that it isnt developed on MS Windows. That said, OnionKnight is right in that installing it may not be that easy (but there's a devpak at devpaks.org, I think. Could always upload my own if needed, but then compiling the library may be better than relying on a devpak).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
KurtOriginally Posted by Ideswa
You're right! I changed it.
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc