Thread: Is double number even or no

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    21

    Is double number even or no

    Hi
    I have to write programm to check if double number is even or no
    Number can be negative positive, double or int.
    Double number is uneven 0 is even.
    Code:
    bool is_even(double n)
    {
      int number=n/2;
      if(number*2==n)
        return true;
      return false;
    }
    I have passed all test except one and I don't know where is mistake can you help me.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    693
    What test didn't pass?

    And you should ask yourself if, for example, 0.5 is even or odd.
    It doesn't make any sense asking if a fraction, resulting in a non integer value, is even or odd...

    Another thing... 'long long' is more precise (63 bits) then 'double' (53 bits), if you are dealing only with integers.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    long int solve the problem thank you

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    57
    Hallo gawiellus

    I think it may be better you compare the two double values with given number of decimals where the values are compared.
    example:
    Code:
    #include <stdio.h>
    #include <math.h>
    
     
    /* usage: arg1=first double vbalue  arg2= second double value arg3= precision decided
     * Return value if integer not equal (example 345.456789 and 123.456789) is -1
     * Return value if integer are equal but not decimals (example 123.456789 and 123.456897) 3
     * 
     */
    
    
    int compdouble(double da, double db, int stellen)
    {
     int rewer = -1, nachint_a = 0, nachint_b = 0, i, faktor = 1, vka, vkb;
     double nachkomma_a, vorkomma_a;
     double nachkomma_b, vorkomma_b;
     
      
     nachkomma_a = std::modf(da, &vorkomma_a);
     nachkomma_b = std::modf(db, &vorkomma_b);
     
     vka = vorkomma_a;
     vkb = vorkomma_b;
     
     printf("decimals from first double.: %10lf\n", nachkomma_a);
     printf("decimals from second double: %10lf\n", nachkomma_b);
     
     if(vka == vkb)
     for (i = 0; i <= stellen; i++)
      {
       faktor *= 10;
       nachint_a = nachkomma_a * faktor;
       nachint_b = nachkomma_b * faktor;
       /* to use this function in other sources please remove this line or set it to a comment */
       printf("%d decimals from first double =%d\n%d decimals from second double=%d\ni=%d\n", i, nachint_a, i, nachint_b, i);
       if(nachint_a != nachint_b) break;
      }
       else return rewer; 
      rewer = i;
     
     return rewer;    
    }
    
    int main(int argc, char **argv)
    {
        int erge = 0;
        double da = 123.456789, db=123.4568997;
        
        /*
        printf("Please enter a double-value.......: ");
        scanf("%lf", &da);
        printf("Please enter a second double-value: ");
        scanf("%lf", &db);
        */
        printf("starting compdouble\n");
        erge = compdouble(da, db, 4);  /* compares double values 4 decimals (after decimal point) */
        printf("end of compdouble\n");
        
        printf("decimals were da and db are equal: %d\n", erge);
    
    
      printf("End of Program\n");
     return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    57
    Second example. Tells you when the digits before decimal points are not equal:
    Code:
    int compdouble(double da, double db, int stellen)
    {
     int rewer = -1, nachint_a = 0, nachint_b = 0, i, faktor = 1, vka, vkb;
     int vorkommastellena = -1, vorkommastellenb = -1; 
     double nachkomma_a, vorkomma_a;
     double nachkomma_b, vorkomma_b;
     
     vorkommastellena = (int)log10(da) + 1;
     vorkommastellenb  = (int)log10(da) + 1;
     
     if (stellen >= 1) // must set number of digits afte decimal point where compared 
      { 
       // separate digits before and after decimal point 
       nachkomma_a = std::modf(da, &vorkomma_a);
       nachkomma_b = std::modf(db, &vorkomma_b);
     
       /* convert digits before decimal to int */
       vka = vorkomma_a;     
       vkb = vorkomma_b;     
     
       /*
       std::cout << "Vorkommastellen da: " << vorkommastellena << std::endl;
       std::cout << "vorhkommastellen bb: " << vorkommastellenb << std::endl;
       std::cout << "Nachkomma_a: " << nachkomma_a << std::endl;
       std::cout << "Nachkomma_b: " << nachkomma_b << std::endl;
       */
       
       if(vka == vkb) // if digits before decimal point equal
       for (i = 0; i <= stellen; i++)
        {
         faktor *= 10; 
         nachint_a = nachkomma_a * faktor;
         nachint_b = nachkomma_b * faktor;
         /*
         std::cout <<"nachint_a: " << std::setw(8) << nachint_a<< "   nachint_b: " << std::setw(8) <<  nachint_b << "   i:" << i << std::endl;
          */ 
         if(nachint_a != nachint_b)
          {
           rewer = i;  // tells you how much digits after decimal point are equal
           break;
          }
        }
           else // when digits before decimal point are not equal
         {
          for (i = vorkommastellena - 1; i >= 0; i--)    
           {
            faktor = pow(10, i);
            vka = vorkomma_a / faktor;
            vkb = vorkomma_b / faktor;
            /*
             Format of evaluation:
             example 321.12345
             -3-2-1   decimal_point  1 2 3 4 5 
             */
            rewer =  i * -1;  
            /*
             * std::cout <<"vka: " << std::setw(8) << vka << "   vka: " << std::setw(8) <<  vkb << "   rewer: " << rewer << "   faktor: " << faktor << std::endl;  
            */
             return rewer;
           }
         }
      }
     else rewer = -111; // when arg3 is < 1
     
     return rewer;    
    }

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    77
    nice code rusyoldguy
    could work with x&1 with integer and knowing how double has its bits maybe with the right &
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double precision number help
    By Magi in forum C Programming
    Replies: 3
    Last Post: 10-16-2012, 04:59 PM
  2. Validate double number
    By guaro555 in forum C Programming
    Replies: 38
    Last Post: 01-15-2008, 07:08 AM
  3. Using a double number in while statement
    By Daesom in forum C++ Programming
    Replies: 6
    Last Post: 10-31-2006, 06:51 PM
  4. restrict float(double) number
    By SuperNewbie in forum C Programming
    Replies: 3
    Last Post: 05-13-2004, 04:35 AM
  5. floating and double number
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2003, 09:52 PM

Tags for this Thread