dividing Doubles

This is a discussion on dividing Doubles within the C++ Programming forums, part of the General Programming Boards category; What Approach is there to cout the elements in this array that are even I have already got an idea ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    dividing Doubles

    What Approach is there to cout the elements in this array that are even
    I have already got an idea but no yet complete
    Please Helpp

    Code:
    double d_array[10] = {3.4, 12.8, 9.5, 2.0, 1.7, 3.8};
    
    for(int i=0; i<6; ++i)
    if(d_array[i]%2==0)
    cout<<d_array[i];

    I know this produces an error but i also know its in the right direction, if somebody can shed some light on this would be greatly appreciated;

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Only integers can be even. So you need to check if an element is an integer before you check if it is even. Then you need to cast the element to an integer to apply &#37;.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    is 12.8 even or odd?

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    a non-integer number has simply no even/odd parity.

    int(12.8) is even

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by yukapuka View Post
    What Approach is there to cout the elements in this array that are even
    Code:
    double d_array[10] = {3.4, 12.8, 9.5, 2.0, 1.7, 3.8};
    Only the 4th element is even, everything else is a floating point number and as such is neither odd nor even.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    Code:
    if(fmod(d_array[i], 1.0) != 0.0) { 
         // not an integer
    } else if ((((int) d_array[i]) &#37; 2 == 0)) {
         // even integer
    } else {
         // odd integer
    }
    This assumes all your integral doubles are representable with infinite precision ( < ~2^50), and fits in an int.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    But of course you can define your own parity for doubles with a given precision (!), by simply considering only the first decimal value for example.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Only integers can have parity, regardless of whether the number can be represented as an integer without loss of precision. even the element 2.0 is nto even because it is by definition not an integer, but a double. parity only has meaning in the context of interger arithmetic. Its like saying whats the remainder of 5.7 divided by 3.14.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    Its like saying whats the remainder of 5.7 divided by 3.14.
    2.56? What's wrong with that?

    Only integers can have parity, regardless of whether the number can be represented as an integer without loss of precision.
    What I meant is they can be converted to integers and then have their parity checked.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by abachler View Post
    Only integers can have parity, regardless of whether the number can be represented as an integer without loss of precision. even the element 2.0 is nto even because it is by definition not an integer, but a double. parity only has meaning in the context of interger arithmetic. Its like saying whats the remainder of 5.7 divided by 3.14.
    By that logic int(2) is not an integer either, because int has finite bounds. The type the programmer uses to encode a number need not relate to the type of number it represents.

    As for the mathematics, I'm pretty sure that integers are defined to be a subset of reals. As such 2.0, which represents a real number, is equal to 2 the integer. But regardless of the formal semantics, there is something particular that the OP wants to do. Even if it is technically incorrect to describe the process as finding the even numbers doesn't change what the OP wants to do.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  2. help with dividing doubles into integers
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2008, 03:57 PM
  3. Passing array of doubles to ATL COM dll
    By wakeup in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2006, 02:53 AM
  4. Please help - dividing doubles
    By hunterdude in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 12:06 AM
  5. Dividing int numbers by 256, but fast!
    By Boksha in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 02:44 PM

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