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

本文关键字:金钱 数据类型 十进制 为什么 | 更新日期: 2023-09-27 17:52:33

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

tl;dr2: 请阅读问题的其余部分,然后用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是否是一个好选择。我在问为什么说基于二进制的系统是不合适的。

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

浮动对于积累和减少资金来说并不稳定。以下是您的实际示例:

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"); }
    }
}

在我的盒子上,这是4000002000.0203美元的累计值,而C#中的预期值为4000002000美元。如果在一家银行的许多交易中丢失了这一点,那就太糟糕了——不一定是大额交易,只需要很多。这有帮助吗?

通常货币计算需要精确的结果,而不仅仅是精确的结果。CCD_ 14和CCD_。例如,0.1不能用浮点变量表示。将存储的是最接近的可表示值,可以是一个数字,例如0.0999999999999999996。通过对结构进行单元测试来自己尝试一下——例如,尝试2.00 - 1.10

我不知道你为什么对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。

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.)哪种可能性更大:他们都很愚蠢,还是浮点数确实是个坏主意?

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

问题是二进制中的0.00015实际上是0.00014999999999986859469669475686259829672053456306457515953125,它向下四舍五入,但确切的十进制值向上四舍五进