Results 1 to 17 of 17

Thread: little q: illogical logical more c++

  1. #1
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default little q: illogical logical more c++

    code below

    Qt Code:
    1. diff_1 = 0;
    2. if (diff_1 == 0.00) {cout<<"flag_1";}
    3. if (diff_1==1.00) {pennies = pennies + 1;
    4. cout<<"increment by a penny to "<<pennies<<endl;}
    To copy to clipboard, switch view to plain text mode 

    THis works a lot of the time but occasionally I'll run a number through the system and get bizzare output:

    put in for diff_1-->1.00 (that's all - no decision tress)

    sometimes 1.00 bypasses both conditions even though I manually set diff_1 to 1.00. and also fails to execute the decision tree next to it:
    Qt Code:
    1. if (diff_1==1.00) {pennies = pennies + 1;
    2. cout<<"increment by a penny to "<<pennies<<endl;}
    To copy to clipboard, switch view to plain text mode 
    Last edited by T1001; 29th June 2015 at 22:40.
    All Your Base Are Belong To Us

  2. #2
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: little q: illogical logical more c++


  3. #3
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: little q: illogical logical more c++

    Thank you for the link, but I don't think this is what is going on. Not sure how this addresses the concern...
    All Your Base Are Belong To Us

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: little q: illogical logical more c++

    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?

  5. #5
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: little q: illogical logical more c++

    Give us compilable piece of code.

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: little q: illogical logical more c++

    Now that I have had coffee, I'm feeling much better. Post removed.
    Last edited by jefftee; 30th June 2015 at 16:39.

  7. #7
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: little q: illogical logical more c++

    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:
    1. cents_no_decimal = convert_cents(cents);
    2. cout<<cents_no_decimal<<" converted to no decimal"<<endl;
    3. find_change(cents_no_decimal, quarters, dimes, nickels, pennies, a, checksum_1);
    4. //cout<<cents_no_decimal<<endl;
    5.  
    6. double diff_1 = 0;
    7. diff_1 = cents_no_decimal - checksum_1; //find the offset amt which is the money before the function then after
    8. cout<<"diff_1:"<<diff_1;
    9. //difference = diff_1;
    10. if (diff_1 == 0) {cout<<"no adjustment necessary"<<endl;
    11. cout<<"difference :"<<difference <<endl;}
    12. else if(diff_1==1) {cout<<"adjustment necessary"<<endl;}
    13. //executive function
    14. cout<<endl;
    15. cout<<"continue y/n : ";
    16.  
    17. cin>>j;
    18.  
    19. if (j!='y')
    20. {
    21. cout<<"terminating program";
    22. flag = false;
    23. }
    To copy to clipboard, switch view to plain text mode 
    All Your Base Are Belong To Us

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: little q: illogical logical more c++

    Try to reimplement double / float comparisons with qFuzzyCompare and qFuzzyIsNull and check if the error still persists.

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: little q: illogical logical more c++

    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.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: little q: illogical logical more c++

    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.

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: little q: illogical logical more c++

    In your situation, where you seem to be working with money, you may well be better off working strictly with integer numbers of cents.
    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.

  12. #12
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: little q: illogical logical more c++

    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:
    1. double convert_cents(double cents)
    2. {
    3. return cents*100;
    4. }
    To copy to clipboard, switch view to plain text mode 


    Full code:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include<iostream>
    3. #include<math.h>
    4. #include<iomanip>
    5.  
    6. using namespace std;
    7.  
    8. double change_due_now( double payment, double item_cost)
    9. {
    10. return payment - item_cost;
    11.  
    12. }
    13.  
    14. double convert_cents(double cents)
    15. {
    16. return cents*100;
    17.  
    18. }
    19.  
    20. int find_change(int cents_no_decimal, int quarters, int &dimes, int &nickels, int & pennies, int & checksum_1)
    21. {
    22.  
    23. //rounding error cmopensation
    24.  
    25. int r_remainder = 0;
    26.  
    27. double pennies_1=0;
    28. quarters = cents_no_decimal / 25;
    29. r_remainder=cents_no_decimal % 25;
    30. cout<<"quarters :"<<quarters<<endl;
    31. //cout<<"remainder :"<<r_remainder<<endl;
    32. dimes = r_remainder/10;
    33. cout<<"dimes :"<<dimes<<endl;
    34. r_remainder = r_remainder % 10;
    35. //cout<<dimes<<endl;//" "<<r_remainder;
    36. nickels = r_remainder/5;
    37. cout<<"nickles :"<<nickels<<endl;
    38.  
    39. pennies = (r_remainder % 5);
    40. cout<<"pennies :"<<pennies<<endl;
    41. cout<<"---------"<<endl;
    42. checksum_1 = (quarters*25)+ (dimes*10) + (nickels * 5) + (pennies * 1) ;
    43. cout<<"sum change = "<<checksum_1<<endl;
    44. return 0;
    45. }
    46.  
    47. int main()
    48.  
    49. {
    50. double total_change_due; double item_cost = 0; double payment = 0;
    51. double dollar_amount = 0; double cents = 0; double quarters = 0;
    52. int dimes = 0; int nickels = 0; int pennies = 0; double cents_no_decimal = 0;
    53. int a = 0; int checksum_1 = 0;
    54. bool flag = true; char j = 0;
    55. while (flag==true) {
    56. cout<<"item cost:";
    57. cin>>item_cost;
    58. cout<<"payment :";
    59. cin>>payment;
    60.  
    61. cout<<fixed<<setprecision(2)<<showpoint;
    62. total_change_due= change_due_now(payment, item_cost);
    63. dollar_amount = int(total_change_due); double difference = 0;
    64. cents = total_change_due-int(total_change_due);
    65. cout<<"Change due: $"<<total_change_due<<endl;
    66. cout<<"dollars : $"<<dollar_amount<<endl;
    67. cout<<"cents : $"<<cents<<endl;
    68.  
    69. cents_no_decimal = convert_cents(cents);
    70. cout<<cents_no_decimal<<" converted to no decimal"<<endl;
    71. find_change(cents_no_decimal, quarters, dimes, nickels, pennies, checksum_1);
    72. //cout<<cents_no_decimal<<endl;
    73. //executive function
    74. double diff_1 = 0;
    75. diff_1 = cents_no_decimal - checksum_1; //find the offset amt which is the money before the function then after
    76. cout<<"diff_1: "<<diff_1<<endl;
    77. if (diff_1 == 0) {cout<<"no adjustment necessary"<<endl;
    78. cout<<"difference :"<<difference <<endl;}
    79. else if(diff_1!=0) {cout<<"adjustment necessary"<<endl;
    80. pennies = pennies +1;
    81. cout<<"Pennies adjusted total: "<<pennies;}
    82.  
    83. cout<<endl<<endl;
    84. cout<<"continue y/n : ";
    85.  
    86. cin>>j;
    87.  
    88. if (j!='y')
    89. {
    90. cout<<"terminating program";
    91. flag = false;
    92. }
    93.  
    94. }
    95.  
    96. return 0;
    97. }
    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

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: little q: illogical logical more c++

    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.

  14. #14
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: little q: illogical logical more c++

    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

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: little q: illogical logical more c++

    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).

  16. #16
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4

    Default Re: little q: illogical logical more c++

    Hi d_stranz, sorry for following up so late. No, this is not a homework assignment per se: it was an assignment from a video course I was taking. There were no clues in the assignment so I had to research a lot of it. When I was done, the programs were similar, independently developed--I really learned a lot from it.

    I decided double was better than int b/c I needed to preserve the decimal in double until I could multiply it out x 100. Once it became an integer, I could calculate the change in quarters, dimes, nickels and cents from the mod fx. You are correct to point out why double than int in the rest of the program and to my chagrin I will have to reflect on that. I was so frustrated with fixing the 1 cent rounding error I tried some try and see programming (ugggh).

    (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).
    Sorry if I don't get your point but the program works, it gives out quarters, nickels and dimes using the mod fx.

    Best regards
    Last edited by T1001; 12th July 2015 at 20:17.
    All Your Base Are Belong To Us

  17. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: little q: illogical logical more c++

    Sorry if I don't get your point but the program works, it gives out quarters, nickels and dimes
    And it only gives out integer values for the number of quarters, nickels, and dimes, right? And if you reach into your pocket, you never pull out half a quarter, do you?

    That's the whole point of the exercise, and it is one example of what is commonly referred to as a packing problem:

    You have a box that's a certain size, and a set of smaller things that need to be packed into the box. How many of each thing can you fit in the box, with the least space left over?

    Or you are filling a plane with cargo. What is the maximum number of things you can load into the plane while staying under the maximum weight?

    Or you have a certain amount of change to give out, and coins of fixed denomination. What is the smallest number of coins of each type that adds to the total?

Similar Threads

  1. Suggestions for logical expression syntax?
    By rakkar in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 20:24
  2. Non logical Errors - qmake
    By ahmhdy in forum Qt Programming
    Replies: 7
    Last Post: 19th June 2009, 21:11
  3. Replies: 0
    Last Post: 24th November 2008, 08:52
  4. little logical question........
    By sudheer in forum Qt Tools
    Replies: 3
    Last Post: 8th May 2008, 14:15
  5. Logical fonts?
    By sdfisher in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2007, 13:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.