Given your example, the value of pennies is indeterminate. Is it initialized prior to the example code you posted? Also, what is the data type for your diff_1 variable?
Given your example, the value of pennies is indeterminate. Is it initialized prior to the example code you posted? Also, what is the data type for your diff_1 variable?
Give us compilable piece of code.
Now that I have had coffee, I'm feeling much better. Post removed.
Last edited by jefftee; 30th June 2015 at 16:39.
the problem is within this code. When the difference is 1 penny sometimes it bypasses teh decision tree below: "no adjustment necessary" or "adjustment necessary"
coffee the key towards great thoughts
Qt Code:
cents_no_decimal = convert_cents(cents); cout<<cents_no_decimal<<" converted to no decimal"<<endl; find_change(cents_no_decimal, quarters, dimes, nickels, pennies, a, checksum_1); //cout<<cents_no_decimal<<endl; double diff_1 = 0; diff_1 = cents_no_decimal - checksum_1; //find the offset amt which is the money before the function then after cout<<"diff_1:"<<diff_1; //difference = diff_1; if (diff_1 == 0) {cout<<"no adjustment necessary"<<endl; cout<<"difference :"<<difference <<endl;} else if(diff_1==1) {cout<<"adjustment necessary"<<endl;} //executive function cout<<endl; cout<<"continue y/n : "; cin>>j; if (j!='y') { cout<<"terminating program"; flag = false; }To copy to clipboard, switch view to plain text mode
All Your Base Are Belong To Us
Try to reimplement double / float comparisons with qFuzzyCompare and qFuzzyIsNull and check if the error still persists.
And please post all of your code. From what you've posted, there's no indication of the data type for cents_no_decimal or what in the world your checksum_1 variable contains, etc. You are much more likely to get help if we don't have to keep guessing, but from the limited info you've posted so far, I agree with @stampede that your problem is likely due to floating point calculations resulting in a result that is not exactly 1.0.
If the value you are testing for equality to 1.00 (exactly) is the result of a computation then the approximation of the result may not be exactly 1.00. Some numbers cannot be exactly represented in a fixed length binary floating point form, and doing maths on them can lead to the situation where these approximation errors accumulate. You can end up with a floating point value that differs from the exact value at the 15 th decimal place and 1.0 != 1.00000000000001. This is why people keep telling you about qFuzzyCompare().
What Every Computer Scientist Should Know About Floating-Point Arithmetic
In your situation, where you seem to be working with money, you may well be better off working strictly with integer numbers of cents.
Last edited by ChrisW67; 30th June 2015 at 21:22.
Or using one of the many, many C++ classes which have been designed for working with currency and which avoid roundoff errors (resulting in "lost" fractional cents) or comparison problems such as you are experiencing. Google is your friend here.In your situation, where you seem to be working with money, you may well be better off working strictly with integer numbers of cents.
Hi guys, sorry to make things a little difficult, i apologize. Below is the full code. One problem I think I see but not sure of is the conversion between converting cents to 100 * cents to give me something to work with. Its a double...
code (plz see below). Also fyi I felt it would be quite a challenge to try to work out the decimal accuracy thing. Sure there are libraries and I'll use them if I can't get it : )
Qt Code:
double convert_cents(double cents) { return cents*100; }To copy to clipboard, switch view to plain text mode
Full code:
Qt Code:
#include <QCoreApplication> #include<iostream> #include<math.h> #include<iomanip> using namespace std; double change_due_now( double payment, double item_cost) { return payment - item_cost; } double convert_cents(double cents) { return cents*100; } int find_change(int cents_no_decimal, int quarters, int &dimes, int &nickels, int & pennies, int & checksum_1) { //rounding error cmopensation int r_remainder = 0; double pennies_1=0; quarters = cents_no_decimal / 25; r_remainder=cents_no_decimal % 25; cout<<"quarters :"<<quarters<<endl; //cout<<"remainder :"<<r_remainder<<endl; dimes = r_remainder/10; cout<<"dimes :"<<dimes<<endl; r_remainder = r_remainder % 10; //cout<<dimes<<endl;//" "<<r_remainder; nickels = r_remainder/5; cout<<"nickles :"<<nickels<<endl; pennies = (r_remainder % 5); cout<<"pennies :"<<pennies<<endl; cout<<"---------"<<endl; checksum_1 = (quarters*25)+ (dimes*10) + (nickels * 5) + (pennies * 1) ; cout<<"sum change = "<<checksum_1<<endl; return 0; } int main() { double total_change_due; double item_cost = 0; double payment = 0; double dollar_amount = 0; double cents = 0; double quarters = 0; int dimes = 0; int nickels = 0; int pennies = 0; double cents_no_decimal = 0; int a = 0; int checksum_1 = 0; bool flag = true; char j = 0; while (flag==true) { cout<<"item cost:"; cin>>item_cost; cout<<"payment :"; cin>>payment; cout<<fixed<<setprecision(2)<<showpoint; total_change_due= change_due_now(payment, item_cost); dollar_amount = int(total_change_due); double difference = 0; cents = total_change_due-int(total_change_due); cout<<"Change due: $"<<total_change_due<<endl; cout<<"dollars : $"<<dollar_amount<<endl; cout<<"cents : $"<<cents<<endl; cents_no_decimal = convert_cents(cents); cout<<cents_no_decimal<<" converted to no decimal"<<endl; find_change(cents_no_decimal, quarters, dimes, nickels, pennies, checksum_1); //cout<<cents_no_decimal<<endl; //executive function double diff_1 = 0; diff_1 = cents_no_decimal - checksum_1; //find the offset amt which is the money before the function then after cout<<"diff_1: "<<diff_1<<endl; if (diff_1 == 0) {cout<<"no adjustment necessary"<<endl; cout<<"difference :"<<difference <<endl;} else if(diff_1!=0) {cout<<"adjustment necessary"<<endl; pennies = pennies +1; cout<<"Pennies adjusted total: "<<pennies;} cout<<endl<<endl; cout<<"continue y/n : "; cin>>j; if (j!='y') { cout<<"terminating program"; flag = false; } } return 0; }To copy to clipboard, switch view to plain text mode
Last edited by T1001; 1st July 2015 at 16:26.
All Your Base Are Belong To Us
The whole point of converting all monies into cents is to avoid rounding issues with floating point numbers, so you should be returning an integer type from convert_cents, not a double.
Thank you, did that, problem solved. I thought I would lose information for sure with replacing the double with int, but my prediction was wrong.
All Your Base Are Belong To Us
Since this clearly looks like a homework assignment, maybe you can take home a few C++ lessons from this which might help you be a better programmer. Why did you think you would "lose information for sure"? What assumptions did you make about preserving information that made you decide double was better than int, even though in the end everything you calculated was based on integer values for results (i.e., you can't have 1.3 quarters and 4.25 dimes as change when you give a dollar for a 25 cent purchase).
Bookmarks