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

发布于 2024-07-06 03:24:21 字数 1560 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

云裳 2024-07-13 03:24:21

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.

望喜 2024-07-13 03:24:21

我使用定点数学课。 它的设计或多或少是浮点数/双精度数的替代品。 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.

雪落纷纷 2024-07-13 03:24:21

Dr.Dobb 的一篇文章介绍了 C++ 中定点算术类型的可能实现。 检查这个

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

苏佲洛 2024-07-13 03:24:21

哎哟。 金融系统很棘手,你的主要问题不是定点数学,问题是舍入误差。

您可以拥有一个漂亮的电子表格,其中包含按客户类型划分的折扣和包含的增值税的奇妙计算。 你计算出总数,然后将其呈现给会计师,他说这些值都是错误的。 原因:输出的格式可能仅包含 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.

一身仙ぐ女味 2024-07-13 03:24:21

尝试直接回答

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

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

作者明确表示他的平台不支持浮点,这就是他这样做的原因。 另请注意,它用于 3D 渲染,问题是针对金融数据,我们需要一个良好的数学函数库......

IEEE 754-2008 十进制浮点算术规范,针对金融应用

这看起来像是一种在良好支持下处理财务数据的既定方法(来自英特尔和 IEEE) - http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library

引用:

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

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

Trying to answer directly

Markus Trenkwalder has one that supports some math functions - 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.

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:

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

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

酒几许 2024-07-13 03:24:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文