问题描述
当前正在学习C ++,这对我来说才刚刚发生。我对此很好奇,因为我正准备开发一个简单的银行程序。我将使用 double
来计算美元/利率等,但是计算机计算和人工计算之间存在一些细微的差异。
Currently learning C++ and this has just occurred to me. I'm just curious about this as I'm about do develop a simple bank program. I'll be using double
for calculating dollars/interest rate etc., but there are some tiny differences between computer calculations and human calculations.
我想现实世界中的那些额外的.pennies可以起到很大作用!
I imagine that those extra .pennies in the real world can make all the difference!
推荐答案
在这种情况下,财务计算是使用定点算法而不是浮点数完成的。
In many cases, financial calculations are done using fixed-point arithmetic instead of floating point.
例如,.NET 十进制
类型,或VB6 Currency
类型。这些基本上只是整数类型,每个人都同意,单位是美分的几分之一,例如$ .0001。
For example, the .NET Decimal
type, or the VB6 Currency
type. These are basically just integer types, where everyone has agreed that the units are some fraction of a cent, like $.0001.
是的,必须进行四舍五入,但它是非常系统地完成的。通常,四舍五入规则在您合同的细则中很深处(利率为x%,每T复利一次,四舍五入至最接近的美分,但每个报表期不少于$ y)。
And yes, some rounding has to occur, but it is done very systematically. Usually the rounding rules are somewhere deep in the fine print of your contract (the interest rate is x%, compounded every T, rounded up to the nearest penny, but not less than $y every statement period).
这篇关于如果浮点数和双精度数不准确,银行如何进行涉及货币的准确计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!