<tfoot id='KinGZ'></tfoot>
  • <small id='KinGZ'></small><noframes id='KinGZ'>

    <i id='KinGZ'><tr id='KinGZ'><dt id='KinGZ'><q id='KinGZ'><span id='KinGZ'><b id='KinGZ'><form id='KinGZ'><ins id='KinGZ'></ins><ul id='KinGZ'></ul><sub id='KinGZ'></sub></form><legend id='KinGZ'></legend><bdo id='KinGZ'><pre id='KinGZ'><center id='KinGZ'></center></pre></bdo></b><th id='KinGZ'></th></span></q></dt></tr></i><div id='KinGZ'><tfoot id='KinGZ'></tfoot><dl id='KinGZ'><fieldset id='KinGZ'></fieldset></dl></div>
      <bdo id='KinGZ'></bdo><ul id='KinGZ'></ul>
    1. <legend id='KinGZ'><style id='KinGZ'><dir id='KinGZ'><q id='KinGZ'></q></dir></style></legend>

        while 循环中的 C++ 浮点精度

        C++ floating point accuracy in while loops(while 循环中的 C++ 浮点精度)
          <legend id='ILZTq'><style id='ILZTq'><dir id='ILZTq'><q id='ILZTq'></q></dir></style></legend>

            1. <small id='ILZTq'></small><noframes id='ILZTq'>

              <i id='ILZTq'><tr id='ILZTq'><dt id='ILZTq'><q id='ILZTq'><span id='ILZTq'><b id='ILZTq'><form id='ILZTq'><ins id='ILZTq'></ins><ul id='ILZTq'></ul><sub id='ILZTq'></sub></form><legend id='ILZTq'></legend><bdo id='ILZTq'><pre id='ILZTq'><center id='ILZTq'></center></pre></bdo></b><th id='ILZTq'></th></span></q></dt></tr></i><div id='ILZTq'><tfoot id='ILZTq'></tfoot><dl id='ILZTq'><fieldset id='ILZTq'></fieldset></dl></div>

                <tbody id='ILZTq'></tbody>
                <bdo id='ILZTq'></bdo><ul id='ILZTq'></ul>

                  <tfoot id='ILZTq'></tfoot>

                  本文介绍了while 循环中的 C++ 浮点精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用一系列 while 循环来计算总计中美元和硬币面额的数量.然而,当我认真对待硬币时,我就差一分钱了.当我输入 99.95 时,我会得到 3 个四分之三、1 角钱、1 镍和 4 便士的输出.我已将问题缩小到浮点精度问题.但是,我研究过的所有解决方案都不适用于我的情况.有什么指点吗?

                  I am trying to count the amount of dollar and coin denominations in a grand total by using a series of while loops. When I get down to the coins however, I am off by a penny. When I enter say 99.95, I get the output 3 quarters, 1 dime, 1 nickel, and 4 pennies. I've narrowed the problem down to a floating point accuracy issue. However all the solutions I've researched haven't been applicable in my situation. Any pointers?

                  #include <iostream>
                  using namespace std;
                  
                  int main()
                  
                  {
                     float amount;
                     cout<<"enter amount" << endl;
                     cin>>amount;
                     int pennies=0, nickels=0, dimes=0, quarters=0, ones=0, fives=0, 
                  tens=0, 
                  twenties=0, fifties=0, hundreds=0;
                  
                     while (amount >= 100) 
                     {
                        hundreds = hundreds +1;
                        amount = amount - 100;
                  
                     }
                     while (amount >= 50)
                     {
                        fifties = fifties +1;
                        amount = amount - 50;
                  
                     }
                     while (amount >= 20)
                     {
                        twenties = twenties +1;
                        amount = amount - 20;
                  
                     }
                     while (amount >= 10)
                     {
                        tens = tens +1;
                        amount = amount - 10;
                  
                     }
                     while (amount >= 5)
                     {
                        fives = fives +1;
                        amount = amount - 5;
                  
                     }
                     while (amount >= 1)
                     {
                        ones = ones +1;
                        amount = amount - 1;
                  
                     }
                     while (amount >= .25)
                     {
                        quarters = quarters +1;
                        amount = amount - .25;
                  
                     }
                     while (amount >= .10)
                     {
                        dimes = dimes +1;
                        amount = amount - .10;
                  
                     }
                     while (amount >= .05)
                     {
                        nickels = nickels +1;
                        amount = amount - .05;
                  
                     }
                     while (amount >= .01)
                     {
                        pennies = pennies +1;
                        amount = amount - .01;
                  
                     }
                  
                  
                     cout<<endl<<"pennies:"<< pennies;
                     cout<<endl<<"nickels:"<<nickels;
                     cout<<endl<<"dimes:"<<dimes;
                     cout<<endl<<"quarters:"<<quarters;
                     cout<<endl<<"ones:"<<ones;
                     cout<<endl<<"fives:"<<fives;
                     cout<<endl<<"tens:"<<tens;
                     cout<<endl<<"twenties:"<<twenties;
                     cout<<endl<<"fifties:"<<fifties;
                     cout<<endl<<"hundreds:"<<hundreds<<endl;
                  
                  
                  
                  
                  
                  
                  
                  return 0;
                  }
                  

                  推荐答案

                  在需要精确值的情况下不要使用浮点数.99.95 不能用浮点数或双精度数精确表示,有点像 1/3 不能用有限数量的正常十进制数字精确表示.

                  Don't use floating point in cases where you need exact values. 99.95 can't be exactly represented in a float or double, a bit like 1/3 can't be exactly represented using a finite number of normal decimal digits.

                  正如 Doug T. 所建议的,您可以使用一个整数来表示便士的数量.当用户输入 123.45 时,将其读取为两个整数,然后将其存储为 12345 便士,而不是 123.45 美元.

                  As Doug T. suggested, you can use an integer to hold the number of pennies. When the user types 123.45, read it as two integers, and then store it as 12345 pennies, not as 123.45 dollars.

                  在这种情况下,您还可以尝试将最后一个 while (amount >= .01) 更改为类似 while (amount >= .005).这不是一个普遍推荐的解决方案,如果这是一个真实的银行应用程序,你真的应该避免它,但它至少有助于防止一些错误.

                  In this case, you can also try to change your last while (amount >= .01) to something like while (amount >= .005). It's not a solution that can be generally recommended, and if this is a real-life bank application you really should avoid it, but it will help against at least some of the errors.

                  这篇关于while 循环中的 C++ 浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                • <legend id='9jsEA'><style id='9jsEA'><dir id='9jsEA'><q id='9jsEA'></q></dir></style></legend>
                  <tfoot id='9jsEA'></tfoot>
                    <tbody id='9jsEA'></tbody>

                      • <i id='9jsEA'><tr id='9jsEA'><dt id='9jsEA'><q id='9jsEA'><span id='9jsEA'><b id='9jsEA'><form id='9jsEA'><ins id='9jsEA'></ins><ul id='9jsEA'></ul><sub id='9jsEA'></sub></form><legend id='9jsEA'></legend><bdo id='9jsEA'><pre id='9jsEA'><center id='9jsEA'></center></pre></bdo></b><th id='9jsEA'></th></span></q></dt></tr></i><div id='9jsEA'><tfoot id='9jsEA'></tfoot><dl id='9jsEA'><fieldset id='9jsEA'></fieldset></dl></div>

                        <small id='9jsEA'></small><noframes id='9jsEA'>

                          • <bdo id='9jsEA'></bdo><ul id='9jsEA'></ul>