Thread: math.h and ceil explanation

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    math.h and ceil explanation

    Code:
    #define taxRate .06#include <math.h>
    
    
    float legalInput( float minimum );
    float roundUpToPenny( float amount );
    void  makeChange ( const float denoms[], int n, float tend, float cost );
    
    
    
    
    int main ( void ) {
        const float denoms[] = {20, 10, 5, 1, .25, .10, .05, .01};
        const int nDenoms = 8;
        
        float price;        // Amount on price tag
        float tendered;     // Money from customer
        float total;        // price + tax
        
       
        printf("Making Change Please enter price and amount tendered, when prompted.\n" );
        price = legalInput( 0.01 );
        total = roundUpToPenny( price * (1 + taxRate));  
        tendered = legalInput( total ); 
        printf( "\nPrice = %.2f + tax = %.2f\n", price, total );
        printf( "Amount tendered = %.2f\n", tendered );
        makeChange ( denoms, nDenoms, total, tendered );
        
        return 0;
    }
    
    
    // -----------------------------------------------------------------------------
    // Round an amount up to the nearest penny.
    float 
    roundUpToPenny( float amount ){
        return ceil( amount*100 ) / 100;
    }
    
    
    // -----------------------------------------------------------------------------
    // Prompt user to input a dollar amount greater than or equal to the minimum.
    float 
    legalInput( float minimum ) {
        float input;
        // puts("legalInput called.");
        for (;;) {
            printf ("Enter value >= %.2f: ", minimum );
            scanf ("%f", &input );
            if (input >= minimum) break;
            printf ("Try again; enter a number >%f: ", minimum );    
        }
        return input;
    }
    
    
    
    
    // -----------------------------------------------------------------------------
    // Given a price and an amount tendered, 
    // output a list of coins and bills needed to make change.
    void 
    makeChange ( const float denoms[], int n, float cost, float tend )
    {
        //puts("\n makeChange called.");
        float change;        // Money returned to customer
        float remaining;    // Amount of change not yet printed
        int k = 0;
        remaining = change = tend - cost;
        if (change < 0) {
            printf( "Please give me $%.2f more.\n", -change );
        }
        else printf( "Total change = %.2f\n", change );
        while (remaining > 0 && k<8) {
            if (remaining >= denoms[k]-.001) {
                //if (diff > .001) {
                remaining -= denoms[k];
                printf("\t%5.2f\n", denoms[k] );
            }
            else ++k;
        }
    }
    if i input 5.00, why is the tax 31 cents and not 30 cents?

    thanks in advance

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think you are starting to find out why most calculations dealing with money is done using integer penny values or on BCD hardware.

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ceil mystery
    By Drogin in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 01:24 PM
  2. Ceil function source help pls
    By Marrah_janine in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2006, 03:13 AM
  3. Decimal ceil()
    By peckitt99 in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2006, 07:58 AM
  4. ceil question
    By 1rwhites in forum C Programming
    Replies: 8
    Last Post: 10-13-2005, 05:01 PM
  5. ceil function
    By keith Dougherty in forum C Programming
    Replies: 5
    Last Post: 11-12-2001, 08:59 PM