你在C ++中用什么来定点表示?

时间:2022-09-02 09:01:53

I'm looking for a fixed-point standard to use for financial data, do you know any that is worth trying? Do you have any experience on the performance of that hand-made fixed-point classes?

我正在寻找用于财务数据的定点标准,你知道任何值得尝试的吗?您对手工定点课程的表现有什么经验吗?

7 个解决方案

#1


6  

Dr.Dobb's has an article about a possible implementation of fixed-point arithmetic type in C++. Check this out.

Dr.Dobb有一篇关于在C ++中可能实现定点算术类型的文章。看一下这个。

#2


6  

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.

您可以拥有一个完整的电子表格,其中包含客户类型和增值税折扣的简单计算。你做了一个总计,你把它呈现给会计师,他说价值观都是错误的。原因:输出可能只有2位小数,但在内部,该值包含float或double的所有小数位。他们确实加起来了。

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

N个小数位的快速和脏解:((double)((int)(Value * N * 10.0)))/ 10.0

Of course you need to check exactly which kind of rounding do your financials require.

当然,您需要确切地检查您的财务需要哪种舍入方式。

#3


6  

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

我使用我的定点数学课。它被设计成或多或少地替代浮子/双打。 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.

编辑:作为旁注,我个人不会为此目的使用固定点类。我只需存储分数(或十分之一,或百分之一分)。 A直接用它做数学。然后我会在向用户显示时适当地缩放值。

#4


0  

IBM's decNumber++

#5


-1  

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.

ISO指定C,TR 24732和C ++,TR 24733的十进制扩展名。它们可在ISO网站上获得。它还不是任何已发布的C ++标准的一部分。 GCC提供了内置类型和它的库实现。英特尔提供另一种实施方案。包含在C ++中的最新推动力就在这里。

#6


-1  

A 64-bit int type should suffice for representing all financial values in cents.

64位int类型应足以用美分表示所有财务值。

You just need to be careful to round percentages correctly, for some definition of correct.

您需要小心正确地舍入百分比,以获得正确的定义。

#7


-1  

Trying to answer directly

Markus Trenkwalder has one that supports some math functions - http://www.trenki.net/content/view/17/1/:

Markus Trenkwalder有一个支持一些数学函数 - http://www.trenki.net/content/view/17/1/:

The library consists of various functions for dealing with fixed point numbers (multiplication, division, inversion, sin, cos, sqrt, rsqrt). It also contains a C++ wrapper class which can be used to simplify working with fixed points numbers greatly. I used this fixed point number class in conjunction with my vector_math library to obtain a fixed point vector math library. Doing so made the 3D computations a lot faster compared to the floating point version.

该库包含用于处理定点数的各种函数(乘法,除法,求逆,sin,cos,sqrt,rsqrt)。它还包含一个C ++包装类,可用于简化固定点数的简化操作。我将这个定点数字类与我的vector_math库结合使用,以获得一个固定点矢量数学库。与浮点版本相比,这样做使得3D计算更快。

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....

作者说他的平台不支持浮点,这就是他这样做的原因。此外,请注意它是用于3D渲染,问题是财务数据,我们想要一个良好的数学函数库....

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

这似乎是一种处理财务数据的既定方式,具有良好的支持(来自英特尔和IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library

To quote:

IEEE 754-2008 Decimal Floating-Point Arithmetic specification, aimed at financial applications, especially in cases where legal requirements make it necessary to use decimal, and not binary floating-point arithmetic (as computation performed with binary floating-point operations may introduce small, but unacceptable errors).

IEEE 754-2008十进制浮点算术规范,针对金融应用,特别是在法律要求使得必须使用十进制而非二进制浮点运算的情况下(因为使用二进制浮点运算执行的计算可能会引入小的,但不可接受的错误)。

It is NOT fixed-point though, but I thought it is pretty useful for people seeking an answer to this question.

虽然这不是定点,但我认为这对于寻求这个问题答案的人来说非常有用。

#1


6  

Dr.Dobb's has an article about a possible implementation of fixed-point arithmetic type in C++. Check this out.

Dr.Dobb有一篇关于在C ++中可能实现定点算术类型的文章。看一下这个。

#2


6  

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.

您可以拥有一个完整的电子表格,其中包含客户类型和增值税折扣的简单计算。你做了一个总计,你把它呈现给会计师,他说价值观都是错误的。原因:输出可能只有2位小数,但在内部,该值包含float或double的所有小数位。他们确实加起来了。

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

N个小数位的快速和脏解:((double)((int)(Value * N * 10.0)))/ 10.0

Of course you need to check exactly which kind of rounding do your financials require.

当然,您需要确切地检查您的财务需要哪种舍入方式。

#3


6  

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

我使用我的定点数学课。它被设计成或多或少地替代浮子/双打。 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.

编辑:作为旁注,我个人不会为此目的使用固定点类。我只需存储分数(或十分之一,或百分之一分)。 A直接用它做数学。然后我会在向用户显示时适当地缩放值。

#4


0  

IBM's decNumber++

#5


-1  

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.

ISO指定C,TR 24732和C ++,TR 24733的十进制扩展名。它们可在ISO网站上获得。它还不是任何已发布的C ++标准的一部分。 GCC提供了内置类型和它的库实现。英特尔提供另一种实施方案。包含在C ++中的最新推动力就在这里。

#6


-1  

A 64-bit int type should suffice for representing all financial values in cents.

64位int类型应足以用美分表示所有财务值。

You just need to be careful to round percentages correctly, for some definition of correct.

您需要小心正确地舍入百分比,以获得正确的定义。

#7


-1  

Trying to answer directly

Markus Trenkwalder has one that supports some math functions - http://www.trenki.net/content/view/17/1/:

Markus Trenkwalder有一个支持一些数学函数 - http://www.trenki.net/content/view/17/1/:

The library consists of various functions for dealing with fixed point numbers (multiplication, division, inversion, sin, cos, sqrt, rsqrt). It also contains a C++ wrapper class which can be used to simplify working with fixed points numbers greatly. I used this fixed point number class in conjunction with my vector_math library to obtain a fixed point vector math library. Doing so made the 3D computations a lot faster compared to the floating point version.

该库包含用于处理定点数的各种函数(乘法,除法,求逆,sin,cos,sqrt,rsqrt)。它还包含一个C ++包装类,可用于简化固定点数的简化操作。我将这个定点数字类与我的vector_math库结合使用,以获得一个固定点矢量数学库。与浮点版本相比,这样做使得3D计算更快。

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....

作者说他的平台不支持浮点,这就是他这样做的原因。此外,请注意它是用于3D渲染,问题是财务数据,我们想要一个良好的数学函数库....

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

这似乎是一种处理财务数据的既定方式,具有良好的支持(来自英特尔和IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library

To quote:

IEEE 754-2008 Decimal Floating-Point Arithmetic specification, aimed at financial applications, especially in cases where legal requirements make it necessary to use decimal, and not binary floating-point arithmetic (as computation performed with binary floating-point operations may introduce small, but unacceptable errors).

IEEE 754-2008十进制浮点算术规范,针对金融应用,特别是在法律要求使得必须使用十进制而非二进制浮点运算的情况下(因为使用二进制浮点运算执行的计算可能会引入小的,但不可接受的错误)。

It is NOT fixed-point though, but I thought it is pretty useful for people seeking an answer to this question.

虽然这不是定点,但我认为这对于寻求这个问题答案的人来说非常有用。