array confusions

This is a discussion on array confusions within the C++ Programming forums, part of the General Programming Boards category; Can Somebody explain this????????????????? program outputs : 4 1 Press Any Key........... Code: double d_array[10] = {3.4, 12.8, 9.5, 2.0, ...

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

    array confusions

    Can Somebody explain this?????????????????

    program outputs :

    4
    1
    Press Any Key...........

    Code:
    double d_array[10] = {3.4, 12.8, 9.5, 2.0, 1.7, 3.8};
    char letters[26];
    int i;
    double j;
    int k;
    int l;
    
    
    
    for(i=0; i<1; ++i)
    {
    k=d_array[i];
    
    j=d_array[i]-k;
    
    j*=10;
    cout<<j<<endl;
    l=j;
    
    cout<<(int)j%2<<endl;
    
    if((int)j%2==0)
    cout<<d_array[i]<<endl;
    
    }

    Heres the question, why does the number when coverted from double to int result in 1?????
    and not 4???????

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Becuase floating point (e.g. double) is not exact - in this case, 3.4 - 3 * 10 makes 3.999999999999. This rounded to int makes 3. You need to add a small offset. I changed your code to:
    Code:
    	j*=10;
    	j+=0.0001;
    	cout << (int)j<<endl;
    It now shows 3.4 as "even" (although by my definition, as explained in the previous post, floating point numbers are not even and odd - only integers can be).

    And perhaps it would be better to multiply by 10 first, then make it into an integer (you will still have to do some rounding operation).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    thx for that swift and very precise reply, i did although change the program around myself

    Code:
    for(i=0; i<6; ++i)
    {
    y=d_array[i]-(int)d_array[i];
    y*=10;
    int k=y+0.5;
    if(k%2==0)
    {
    cout<<d_array[i]<<endl<<endl;
    cout<<"Number Is Even!!!!"<<endl<<endl;
    continue;
    }
    else
    {
    to this but its the same effect as your suggestion , thx again definately cleared things up

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    First rule for using floats and doubles in C: Don't!

    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

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    48
    hehe il take that into consideration thanks!!!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    First rule for using floats and doubles in C: Don't!

    QuantumPete
    This is of course not specific to C - it's the same in Fortran, C++, Pascal or whatever other language you may consider - unless it's not using the standard floating point formats, it will not overcome the "not precise" limitation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    This is of course not specific to C -
    Sorry, forgot I was posting in the C++ forum :P
    I'm surprised actually that there isn't a standard class in C++ that does accurate floating point operations. Does Boost have something like that?

    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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    Sorry, forgot I was posting in the C++ forum :P
    I'm surprised actually that there isn't a standard class in C++ that does accurate floating point operations. Does Boost have something like that?

    QuantumPete
    But that would require an infinite number of bits. And the point here is that double is 8 bytes (in most architecturse), which is a reasonably small amount of bits - but it's still good enough for about 15 or more places of correct result. The problem comes when comparing precise values and/or converting to integer.

    I'm sure there are bignum libraries (e.g. GMP) that support really large numbers of high precision - but they are STILL approximations.

    Unless you can device another way to describe the numbers (for some things, you could of course use fraction representation, e.g. 0.7 described as 7/10).

    This problem CAN NOT BE TRIVIALLY SOLVED for all problem settings with one solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    But that would require an infinite number of bits.
    [...]
    This problem CAN NOT BE TRIVIALLY SOLVED for all problem settings with one solution.
    I meant "accurate" as in with more bits than a double has. Like you said, this is not something that can be easily solved, which lead to the first-rule-for-using-floats-and-doubles in the first place!
    If you only need two d.p., use ints and convert for output.

    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

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    I meant "accurate" as in with more bits than a double has. Like you said, this is not something that can be easily solved, which lead to the first-rule-for-using-floats-and-doubles in the first place!
    If you only need two d.p., use ints and convert for output.

    QuantumPete
    Sure, fixed point is one of many solutions to "don't use floating point". In the case of multiples of 0.1 (which 3.4 is), it's not helping to have more precision. Representing 0.1 in a binary form is just like representing 1/3 in a decimal form - it never ends. One solution to this problem is to use BCD floats, which use 4 bits to store 0-9. But it's noticeably less efficient both in the math itself and usage of space, so it's not use very often. Turbo Pascal 3.x had a BCD library for it's floating point, which I used for calculations of monetary amounts at one stage in the late 80's. In later versions of software related to this, I used big integers and representing pence, just re-formatting for output, like you explained.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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