为什么使用非十进制数据类型对金钱不利?

时间:2011-04-24 05:19:40

标签: c# types double currency

tl; dr:我的Cur(货币)结构出了什么问题?

tl; dr 2: 在提供floatdouble的示例之前,请先阅读其余问题。 :-)


我知道这个问题在互联网上出现过很多次,但我还没有看到令人信服的答案,所以我想我会再问一次。

我无法理解为什么使用非十进制数据类型对处理钱有害。 (这是指存储二进制数字而不是十进制数字的数据类型。)

是的,将两个doublea == b进行比较是不明智的。但你可以很容易地说a - b <= EPSILON或类似的东西。

这种方法出了什么问题?

例如,我刚刚在C#中创建了一个struct我认为可以正确处理资金,而不使用任何基于十进制的数据格式:

struct Cur
{
  private const double EPS = 0.00005;
  private double val;
  Cur(double val) { this.val = Math.Round(val, 4); }
  static Cur operator +(Cur a, Cur b) { return new Cur(a.val + b.val); }
  static Cur operator -(Cur a, Cur b) { return new Cur(a.val - b.val); }
  static Cur operator *(Cur a, double factor) { return new Cur(a.val * factor); }
  static Cur operator *(double factor, Cur a) { return new Cur(a.val * factor); }
  static Cur operator /(Cur a, double factor) { return new Cur(a.val / factor); }
  static explicit operator double(Cur c) { return Math.Round(c.val, 4); }
  static implicit operator Cur(double d) { return new Cur(d); }
  static bool operator <(Cur a, Cur b) { return (a.val - b.val) < -EPS; }
  static bool operator >(Cur a, Cur b) { return (a.val - b.val) > +EPS; }
  static bool operator <=(Cur a, Cur b) { return (a.val - b.val) <= +EPS; }
  static bool operator >=(Cur a, Cur b) { return (a.val - b.val) >= -EPS; }
  static bool operator !=(Cur a, Cur b) { return Math.Abs(a.val - b.val) < EPS; }
  static bool operator ==(Cur a, Cur b) { return Math.Abs(a.val - b.val) > EPS; }
  bool Equals(Cur other) { return this == other; }
  override int GetHashCode() { return ((double)this).GetHashCode(); }
  override bool Equals(object o) { return o is Cur && this.Equals((Cur)o); }
  override string ToString() { return this.val.ToString("C4"); }
}

(很抱歉将名称Currency更改为Cur,对于较差的变量名称,省略public以及错误的布局;我试图将其全部放入屏幕,以便您可以在不滚动的情况下阅读它。):)

您可以像以下一样使用它:

Currency a = 2.50;
Console.WriteLine(a * 2);

当然,C#具有decimal数据类型,但这不是重点 - 问题是为什么上述内容很危险,而不是为什么我们不应该使用decimal

那么有人会介意向我提供一个危险陈述的现实反例,这会在C#中失败吗?我想不出任何一个。

谢谢!


注意:我 辩论decimal是否是一个不错的选择。我在问为什么基于二进制的系统被认为是不合适的。

5 个答案:

答案 0 :(得分:10)

浮动资金不能稳定累积和减少资金。这是你的实际例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BadFloat
{
    class Program
    {
        static void Main(string[] args)
        {
            Currency yourMoneyAccumulator = 0.0d;
            int count = 200000;
            double increment = 20000.01d; //1 cent
            for (int i = 0; i < count; i++)
                yourMoneyAccumulator += increment;
            Console.WriteLine(yourMoneyAccumulator + " accumulated vs. " + increment * count + " expected");
        }
    }

    struct Currency
    {
        private const double EPSILON = 0.00005;
        public Currency(double value) { this.value = value; }
        private double value;
        public static Currency operator +(Currency a, Currency b) { return new Currency(a.value + b.value); }
        public static Currency operator -(Currency a, Currency b) { return new Currency(a.value - b.value); }
        public static Currency operator *(Currency a, double factor) { return new Currency(a.value * factor); }
        public static Currency operator *(double factor, Currency a) { return new Currency(a.value * factor); }
        public static Currency operator /(Currency a, double factor) { return new Currency(a.value / factor); }
        public static Currency operator /(double factor, Currency a) { return new Currency(a.value / factor); }
        public static explicit operator double(Currency c) { return System.Math.Round(c.value, 4); }
        public static implicit operator Currency(double d) { return new Currency(d); }
        public static bool operator <(Currency a, Currency b) { return (a.value - b.value) < -EPSILON; }
        public static bool operator >(Currency a, Currency b) { return (a.value - b.value) > +EPSILON; }
        public static bool operator <=(Currency a, Currency b) { return (a.value - b.value) <= +EPSILON; }
        public static bool operator >=(Currency a, Currency b) { return (a.value - b.value) >= -EPSILON; }
        public static bool operator !=(Currency a, Currency b) { return Math.Abs(a.value - b.value) <= EPSILON; }
        public static bool operator ==(Currency a, Currency b) { return Math.Abs(a.value - b.value) > EPSILON; }
        public bool Equals(Currency other) { return this == other; }
        public override int GetHashCode() { return ((double)this).GetHashCode(); }
        public override bool Equals(object other) { return other is Currency && this.Equals((Currency)other); }
        public override string ToString() { return this.value.ToString("C4"); }
    }

}

在我的盒子上,累计产生4,000,002,000.0203美元,而C#预期为4000002000。如果它在银行的许多交易中丢失了,这是一个糟糕的交易 - 它不一定是大的,只有很多。这有帮助吗?

答案 1 :(得分:4)

通常货币计算需要精确的结果,而不仅仅是准确的结果。 floatdouble类型无法准确表示基本10个实数的整个范围。例如,0.1不能用浮点变量表示。将存储的是最接近的可表示值,其可以是诸如0.0999999999999999996之类的数字。通过单元测试结构来自行尝试 - 例如,尝试2.00 - 1.10

答案 2 :(得分:4)

我不确定为什么你对J Trana的答案毫不相干感到耸耸肩。你为什么不亲自尝试一下?同样的示例也适用于您的结构。您只需要添加一些额外的迭代,因为您使用的是double而不是float,这样可以提高一些精度。只是延迟问题,没有摆脱它。

证明:

class Program
{
    static void Main(string[] args)
    {
        Currency currencyAccumulator = new Currency(0.00);
        double doubleAccumulator = 0.00f;
        float floatAccumulator = 0.01f;
        Currency currencyIncrement = new Currency(0.01);
        double doubleIncrement = 0.01;
        float floatIncrement = 0.01f;

        for(int i=0; i<100000000; ++i)
        {
            currencyAccumulator += currencyIncrement;
            doubleAccumulator += doubleIncrement;
            floatAccumulator += floatIncrement;
        }
        Console.WriteLine("Currency: {0}", currencyAccumulator);
        Console.WriteLine("Double: {0}", doubleAccumulator);
        Console.WriteLine("Float: {0}", floatAccumulator);
        Console.ReadLine();
    }
}

struct Currency
{
    private const double EPSILON = 0.00005;
    public Currency(double value) { this.value = value; }
    private double value;
    public static Currency operator +(Currency a, Currency b) { return new Currency(a.value + b.value); }
    public static Currency operator -(Currency a, Currency b) { return new Currency(a.value - b.value); }
    public static Currency operator *(Currency a, double factor) { return new Currency(a.value * factor); }
    public static Currency operator *(double factor, Currency a) { return new Currency(a.value * factor); }
    public static Currency operator /(Currency a, double factor) { return new Currency(a.value / factor); }
    public static Currency operator /(double factor, Currency a) { return new Currency(a.value / factor); }
    public static explicit operator double(Currency c) { return System.Math.Round(c.value, 4); }
    public static implicit operator Currency(double d) { return new Currency(d); }
    public static bool operator <(Currency a, Currency b) { return (a.value - b.value) < -EPSILON; }
    public static bool operator >(Currency a, Currency b) { return (a.value - b.value) > +EPSILON; }
    public static bool operator <=(Currency a, Currency b) { return (a.value - b.value) <= +EPSILON; }
    public static bool operator >=(Currency a, Currency b) { return (a.value - b.value) >= -EPSILON; }
    public static bool operator !=(Currency a, Currency b) { return Math.Abs(a.value - b.value) <= EPSILON; }
    public static bool operator ==(Currency a, Currency b) { return Math.Abs(a.value - b.value) > EPSILON; }
    public bool Equals(Currency other) { return this == other; }
    public override int GetHashCode() { return ((double)this).GetHashCode(); }
    public override bool Equals(object other) { return other is Currency && this.Equals((Currency)other); }
    public override string ToString() { return this.value.ToString("C4"); }
}

结果:

Currency: $1,000,000.0008
Double: 1000000.00077928
Float: 262144

我们只有0.08美分,但最终还是会加起来。


您的修改

    static void Main(string[] args)
    {
        Currency c = 1.00;
        c /= 100000;
        c *= 100000;
        Console.WriteLine(c);
        Console.ReadLine();
    }
}

struct Currency
{
    private const double EPS = 0.00005;
    private double val;
    public Currency(double val) { this.val = Math.Round(val, 4); }
    public static Currency operator +(Currency a, Currency b) { return new Currency(a.val + b.val); }
    public static Currency operator -(Currency a, Currency b) { return new Currency(a.val - b.val); }
    public static Currency operator *(Currency a, double factor) { return new Currency(a.val * factor); }
    public static Currency operator *(double factor, Currency a) { return new Currency(a.val * factor); }
    public static Currency operator /(Currency a, double factor) { return new Currency(a.val / factor); }
    public static Currency operator /(double factor, Currency a) { return new Currency(a.val / factor); }
    public static explicit operator double(Currency c) { return Math.Round(c.val, 4); }
    public static implicit operator Currency(double d) { return new Currency(d); }
    public static bool operator <(Currency a, Currency b) { return (a.val - b.val) < -EPS; }
    public static bool operator >(Currency a, Currency b) { return (a.val - b.val) > +EPS; }
    public static bool operator <=(Currency a, Currency b) { return (a.val - b.val) <= +EPS; }
    public static bool operator >=(Currency a, Currency b) { return (a.val - b.val) >= -EPS; }
    public static bool operator !=(Currency a, Currency b) { return Math.Abs(a.val - b.val) < EPS; }
    public static bool operator ==(Currency a, Currency b) { return Math.Abs(a.val - b.val) > EPS; }
    public bool Equals(Currency other) { return this == other; }
    public override int GetHashCode() { return ((double)this).GetHashCode(); }
    public override bool Equals(object o) { return o is Currency && this.Equals((Currency)o); }
    public override string ToString() { return this.val.ToString("C4"); }
}

打印$ 0.

答案 3 :(得分:2)

Mehrdad,如果我引入整个SEC,我认为我不能说服。现在,您的整个类基本上实现了BigInteger算法,隐含了2位小数位移。 (出于会计目的,它应该至少为4,但我们可以很容易地将2改为4。)

优点我们用double而不是BigDecimal来支持这个类(或者如果有类似的东西可用longlong)?为了原始类型的优势,我付出了昂贵的舍入操作。我也付出了不准确的代价。 [例子来自1]

import java.text.*;

public class CantAdd {
   public static void main(String[] args) {
      float a = 8250325.12f;
      float b = 4321456.31f;
      float c = a + b;
      System.out.println(NumberFormat.getCurrencyInstance().format(c));
   }
}

好的,这里我们用浮动而不是双重支持,但是不应该是一个大的警告标志,整个概念是错误的,如果我们必须进行数百万次计算,我们可能会遇到麻烦?

每一位从事金融工作的专业人士都认为,货币的浮点表示是一个坏主意。 (参见几十个点击,http://discuss.joelonsoftware.com/default.asp?design.4.346343.29。)哪个更有可能:它们都是愚蠢的,或者浮点钱确实是一个坏主意?

答案 4 :(得分:0)

Cur c = 0.00015;
System.Console.WriteLine(c);
// rounds to 0.0001 instead of the expected 0.0002

问题是二进制中的0.00015确实是0.00014999999999999998685946966947568625982967205345630645751953125,其舍入 down ,但精确的十进制值围绕向上