Arithmetic Problems using ProC

This is a discussion on Arithmetic Problems using ProC within the C Programming forums, part of the General Programming Boards category; I have a problem with the following code sample which does not return the expected result: Code: int main ( ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Arithmetic Problems using ProC

    I have a problem with the following code sample which does not return the expected result:

    Code:
    int main ( int argc, char **argv )
    {
        int in_mth_alloc = 0;
        float db_result = 0.0;
        float db_bill_amt = 0.0;
        int return_code = 0 ;
    
        /* Check start up parameters and logon to DB */
        if ( ( return_code = program_init( argc, argv ) ) == 0 )
        {
            EXEC SQL SELECT integer1, decimal1
                       INTO :in_mth_alloc,
                            :db_bill_amt
                       FROM lookup_table;
    
            printf("in_mth_alloc = %i, db_bill_amt = %.2f, sqlcode = %i\n", in_mth_alloc, db_bill_amt, sqlca.sqlcode );
            db_result = ( db_bill_amt * ( ( 102 - in_mth_alloc ) / 102 ) * 12 );
            printf("%.6f = ( %.2f * ( ( 102 - %i ) / 102 ) * 12 )\n", db_result, db_bill_amt, in_mth_alloc );
        }
        exit( 0 );              /* Completed program */
    }
    The Oracle table that the code reads from has two columns, and a single row of data as shown:

    integer1, type=number(2), value=30
    decimal1, type=number(12,2), value=76484.29

    The program displays the following output:

    in_mth_alloc = 30, db_bill_amt = 76484.29, sqlcode = 0
    0.000000 = ( 76484.29 * ( ( 102 - 30 ) / 102 ) * 12 )

    Obviously the result of the calculation is incorrect, the answer should be 647867.011764 (rounded to 6 dp). The only explantion that I can think of is that for some reason the result of the divison part of the calculation has evaluated to zero. But I can't imagine why that should have happened.

    Does anyone have any idea about what's going wrong.

    Thanks

    Paul

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > ( 102 - 30 ) / 102
    This part of the sub-expression will be done using integer arithmetic, so it will always be 0 (and so will the whole thing).

    Try this
    db_result = ( db_bill_amt * ( ( 102.0 - in_mth_alloc ) / 102 ) * 12 );
    That should force the whole expression to be evaluated as a float expression. If not, add some more .0 to your other constants.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    Thanks Salem, that worked a treat!

    Paul

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Pointer Arithmetic problems
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2004, 06:40 AM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  4. I still have some problems
    By Mike Jones in forum C Programming
    Replies: 2
    Last Post: 04-01-2002, 01:26 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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