Thread: Wrong final value after adding functions. C language

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    4

    Question Wrong final value after adding functions. C language

    Hello,
    I need help fixing this code written on C please.
    The expected total revenue is: TOTALREVENUE is $3894.00
    The total revenue I'm getting is: TOTALREVENUE is $4086.00

    I can't find were the math goes wrong in the functions I added to the program.


    Wrong final value after adding functions. C language-untitled-jpg

    gala.c

    gala.txt

    Program7.pdf

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,857
    One way to approach this is to do a direct debugging method: run your program using a debugger, place breakpoints and check that the values of the variables are what you expect them to be at those breakpoints. If they differ from expectation, then you likely have a bug somewhere before that breakpoint (or maybe you yourself computed the expected value wrongly), so you can look more closely at that part of the code to fix it.

    Since you're adding functions, you can also do unit testing: write test function that you call from the main function. In this test function, you call your functions with known input values and check their outputs. If the output of a function call isn't what you expected, you print the function name, input, expected output and actual output, upon which you know you need to debug that function in particular for that input value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    4

    Thumbs up thanks

    Quote Originally Posted by laserlight View Post
    One way to approach this is to do a direct debugging method: run your program using a debugger, place breakpoints and check that the values of the variables are what you expect them to be at those breakpoints. If they differ from expectation, then you likely have a bug somewhere before that breakpoint (or maybe you yourself computed the expected value wrongly), so you can look more closely at that part of the code to fix it.

    Since you're adding functions, you can also do unit testing: write test function that you call from the main function. In this test function, you call your functions with known input values and check their outputs. If the output of a function call isn't what you expected, you print the function name, input, expected output and actual output, upon which you know you need to debug that function in particular for that input value.
    Hello, I followed your advice and I got a lot closer to the expected output, but i'm still off by 4, and I can't not understand why or where is my math incorrect.

    thanks again, I will keep doing what you told me hopefully I will find the problem.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    I strongly recommend that you don't use floating point types with currency calculations.

    Because of the nature of floating point implementation, most values aren't exact and due to rounding, some basic mathematical properties (i.e: commutative property) aren't valid. Example:

    Code:
    #include <stdio.h>
    
    #define SHOW_RESULT(c) \
      printf("[%s]\n", ((c))?"yes":"no")
    
    void testfp1 ( void )
    {
      double x = 1.2;
    
      printf ( "x = 1.2 - 0.4 - 0.4 - 0.4; x = 0.0? " );
    
      x -= 0.4;
      x -= 0.4;
      x -= 0.4;
    
      /* x should be 0.0, right!? Wrong! */
      SHOW_RESULT ( x == 0.0 );
    }
    
    void testfp2 ( void )
    {
      double x;
      double y;
    
      printf ( "x = (0.1 + 0.2) + 0.3; y = 0.1 + (0.2 + 0.3); x == y ? " );
    
      x = ( 0.1 + 0.2 ) + 0.3;
      y = 0.1 + ( 0.2 + 0.3 );
    
      /* x == y, right? Wrong! */
      SHOW_RESULT ( x == y );
    }
    
    void testfp3 ( void )
    {
      double x;
      double y;
    
      printf ( "x = (0.1 * 0.2) * 0.3; y = 0.1 * (0.2 * 0.3); x == y? " );
    
      x = ( 0.1 * 0.2 ) * 0.3;
      y = 0.1 * ( 0.2 * 0.3 );
    
      /* x == y, right? Wrong! */
      SHOW_RESULT ( x == y );
    }
    
    void testfp4 ( void )
    {
      double x;
      double y;
    
      printf ( "x = (0.1 + 0.2) * 0.3; y = (0.1 * 0.3) + (0.2 * 0.3); x == y? " );
    
      x = ( 0.1 + 0.2 ) * 0.3;
      y = ( 0.1 * 0.3 ) + ( 0.2 * 0.3 );
    
      /* x == y, right? Wrong! */
      SHOW_RESULT ( x == y );
    }
    
    int main ( void )
    {
      testfp1();
      testfp2();
      testfp3();
      testfp4();
    
      return 0;
    }
    Yet... Your code is using the float type (which has 24 bits of precision). To avoid truncation you should use, at least, 'double'.
    PS: Avoid using 'long double', since it will use fp87 on Intel platforms...

    If you should not use floating point, what to do then? Well... You could use integers working with 'cents' units:
    Code:
    typedef long long currency_T;
    
    #define FLT2CUR(x) ((x) * 100.0)
    The only problem is that you need to do some calculations before and after multiplications and divisions:
    Code:
    currency_T mulcur(currency_T v1, currency_T v2)
    {
      // FIXME: You should use greater precision (using compiler/language extensions).
      __int128 t = (__int128)v1 * v2;
      return t / 100;  // don't bother with overflows!
    }
    
    currency_T divcur( currency_T v1, currency_T v2 )
    {
      return ( v1 / v2 ) * 100;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,122
    > *totalRevenue += ((availTickets - soldtickets + numTickets)) * 2;
    This calculation looks very suspect, when you're trying to sell the last few remaining raffle tickets.

    The whole 'buyRaffle' is complicated.
    Consider separating out functions to say
    - count how many tickets are available
    - purchase one ticket.

    If there are 5 tickets available, and someone wants to buy 10, do they get 5 tickets or 0 tickets?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding two octal numbers from char[] C language
    By kamilosinio in forum C Programming
    Replies: 4
    Last Post: 04-25-2020, 03:49 AM
  2. Replies: 4
    Last Post: 10-19-2013, 11:01 PM
  3. What's wrong with C++ as a first language?
    By Angie in forum C++ Programming
    Replies: 43
    Last Post: 09-20-2009, 10:39 PM
  4. DirectX dll Final Debug and Final Retail
    By hdragon in forum Tech Board
    Replies: 0
    Last Post: 11-15-2005, 09:46 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread