首页 文章

循环中的C浮点精度

提问于
浏览
2

我试图通过使用一系列while循环来计算总计中的美元和硬币面额 . 然而,当我拿到硬币时,我只差一分钱 . 当我输入说99.95时,我得到输出3个季度,1个角钱,1个镍币和4个便士 . 我已将问题缩小到浮点精度问题 . 然而,我研究过的所有解决方案都不适用于我的情况 . 有什么指针吗?

#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;
}

4 回答

  • 0

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

    正如Doug T.建议的那样,你可以使用一个整数来保存便士的数量 . 当用户键入123.45时,将其读作两个整数,然后将其存储为12345便士,而不是123.45美元 .

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

  • 0

    在这种情况下,您应该使用定点值,而不是浮点值 .

    虽然人们用美元来计算货币,但这并不准确,但货币以美分来衡量,这是精确的 . 只计算分数,除以100得到美元金额,然后取模数得到分数 . 在这种情况下,浮点不是合适的工具 .

  • 3

    通常,二进制浮点数不能表示小数十进制值,即使小数位总数很小 . 例如, 0.1 无法准确表示 .

    为了处理确切的值,存在不同的方法,这些方法都使用与 2 不同的基数 . 根据tge的具体需求,这些方法或多或少都涉及到 . 适用于您的情况的简单方法是使用固定精度而不是可变精度 . 要以固定精度计算,您只需使用一个整数,该整数表示您实际想要的值乘以适当的 10 幂 . 根据您执行的计算量,您可以将逻辑打包到类中,也可以将其分发到整个代码中 . 在"real"应用程序中,我可能只是将逻辑直接应用于 int .

  • 0

    这是固定代码,我使用了TJD的建议,而不是使用.01,而是使用了.0099 . 对于我似乎有用的问题,谢谢你们!

    #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;
       float p = .0099, n = .0499, d = .099, q = .2499;   
       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 >= q)
       {
          quarters = quarters +1;
          amount = amount - q;
    
       }
       while (amount >= d)
       {
          dimes = dimes +1;
          amount = amount - d;
    
       }
       while (amount >= n)
       {
          nickels = nickels +1;
          amount = amount - n;
    
       }
       while (amount >= p)
       {
          pennies = pennies +1;
          amount = amount - p;
    
       }
    
    
       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;
    }
    

相关问题