check value

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; // ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    check value

    I know how to validate input, but how can I check if

    Code:
    int ans = num1 / num2; // all integers
    is an integer and not a float?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Check if num1 is perfectly divisible by num2.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    But the max. integer value is 2147483647. Is there any way I can exceed that limit?
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    on some implementations, unsigned long long will give you a 64-bit int.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Quote Originally Posted by laserlight
    You could give the GMP library a try.


    GMP:

    Quote Originally Posted by http://www.swox.com/gmp/guestaccount.html
    People sometimes offer us accounts on Windows machines; unfortunately, that isn't useful since we don't develop GMP for such arcane platforms.
    Hmm...



    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.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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 02:14 AM. Reason: big-o stuff

  10. #10
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    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.

  11. #11
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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

  12. #12
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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:
    Code:
    for(i; i < UINT_MAX; i++)
      {
        if(i % 2 > 0)
        {
          prime(i);
        }
        else if(i == 1)
        {
          continue;
        }
      }
    checks if unsigned int i is even or odd. The program has become faster.
    Last edited by Ideswa; 03-18-2006 at 06:47 AM. Reason: add things
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Ideswa
    Code:
    for(i; i < UINT_MAX; i++)
      {
        if(i % 2 > 0)
        {
          prime(i);
        }
        else if(i == 1)  // this test just slows things down. the test will always be false
        {
          continue;
        }
      }
    Kurt

  15. #15
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You're right! I changed it.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21