We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
IBM 的 decNumber++
IBM's decNumber++
ISO 指定了 C、TR 24732 和 C++、TR 24733 的十进制扩展。可以在 ISO 网站< /a>. 它尚未成为任何已发布的 C++ 标准的一部分。 GCC 提供了内置类型和它的库实现。 另一种实现是可从英特尔获得 。 最近推动将其包含在 C++ 中的是 在这里。
ISO specified a decimal extension to C, TR 24732, and to C++, TR 24733. They are available for money on the ISO website. It's not yet part of any published C++ Standard. GCC provides built-in types and a library implementation of it. Another implementation is available from Intel. The most recent push for having this included in C++ is here.
我使用定点数学课。 它的设计或多或少是浮点数/双精度数的替代品。 http://codef00.com/coding
编辑:作为旁注,我个人不会为此目的使用定点类。 相反,我会只存储美分的数量(或根据需要十分之一美分,或百分之一美分)。 A 只需直接用它进行数学计算即可。 然后我会在向用户显示时适当缩放该值。
I use my fixed point math class. It is designed to be more or less a drop in replacement for floats/doubles. http://codef00.com/coding
EDIT: As a side note, I would not personally used a fixed point class for this purpose. I would instead just store the number of cents (or tenths of a cent, or hundredths of a cent as needed). A just do the math directly with that. Then I would scale the value appropriately when displaying to the users.
Dr.Dobb 的一篇文章介绍了 C++ 中定点算术类型的可能实现。 检查这个。
Dr.Dobb's has an article about a possible implementation of fixed-point arithmetic type in C++. Check this out.
哎哟。 金融系统很棘手,你的主要问题不是定点数学,问题是舍入误差。
您可以拥有一个漂亮的电子表格,其中包含按客户类型划分的折扣和包含的增值税的奇妙计算。 你计算出总数,然后将其呈现给会计师,他说这些值都是错误的。 原因:输出的格式可能仅包含 2 位小数,但在内部,该值具有 float 或 double 的所有小数位。 而且它们确实加起来了。
您需要了解您的财务状况并决定基值在哪里。 这意味着会计师将检查哪些值(是的,这需要商业知识,请注意“棘手”部分)。
在将值保存到持久形式(数据库、文件、内存...)之前,您会截断乘法和除法可能添加的额外小数位。
N 位小数的快速而肮脏的解决方案:
((double)((int)(Value * N * 10.0)))/10.0
当然,您需要准确检查您的财务需要哪种舍入。
Ouch. Financial systems are tricky, your main problem is not fixed point math, the problem are the rounding errors.
You can have a nice spreadsheet full with maverlous calculations with discounts by client type and VAT included. You make a total, you present it to an accountant and he says the values are all wrong. The reason: The output may be formated with only 2 decimal places but internally the value has all the decimal places of a float or double. and they do add up.
You need to know your financials and decide where the base values will be. Meaning what values are the ones the accountants will check (yes it requires business knowledge, hance the 'tricky' part).
The before you save the value to a persistent form (database, file, memory ...) you truncate the extra decimal places that multiplications and divisions may have added.
Quick and dirty solution for N decimal places:
((double)((int)(Value * N * 10.0)))/10.0
Of course you need to check exactly which kind of rounding do your financials require.
尝试直接回答
Markus Trenkwalder 有一个支持一些数学函数的函数 - http://www .trenki.net/content/view/17/1/:
作者明确表示他的平台不支持浮点,这就是他这样做的原因。 另请注意,它用于 3D 渲染,问题是针对金融数据,我们需要一个良好的数学函数库......
IEEE 754-2008 十进制浮点算术规范,针对金融应用
这看起来像是一种在良好支持下处理财务数据的既定方法(来自英特尔和 IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library
引用:
虽然它不是定点,但我认为它对于寻求这个问题答案的人来说非常有用。
Trying to answer directly
Markus Trenkwalder has one that supports some math functions - http://www.trenki.net/content/view/17/1/:
The author made it a point to say his platform does not support floating point though, that's why he did it. Also, note that it's for 3D rendering, the question was for financial data and we want a good library of math functions....
IEEE 754-2008 Decimal Floating-Point Arithmetic specification, aimed at financial applications
This looks like an established way of handling financial data with good support (from Intel and IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library
To quote:
It is NOT fixed-point though, but I thought it is pretty useful for people seeking an answer to this question.
64 位 int 类型应该足以表示所有以美分为单位的财务价值。
对于某些正确的定义,您只需要小心地正确舍入百分比即可。
A 64-bit int type should suffice for representing all financial values in cents.
You just need to be careful to round percentages correctly, for some definition of correct.