Introduction
"Where to keep money?" this is the joking title of a post that appears periodically on computer forums. This refers to the question: in what type of variables should the results of calculations of financial and economic calculations be stored. Usually such a question arises for programmers who first encounter such calculations. They intuitively understand that rounding is undesirable or unacceptable here. Interestingly, at the same time, they often recall the legends about cunning programmers who managed to transfer the difference between the exact and rounded value of financial calculations to their own account. True, these stories were called fairy tales in the book [1], written 50 years ago, and even there it was only about revisions of banking systems, the grounds for which (ie, for revisions) could be similar stories.
60 . . , - , , «» . «» , . , , «» , .
: 13% 1 30 . «» : 16.9 . «double», 1.68999999999999E-001 , 0.169 IEEE-754 . : ? – . ? , «» , «float» ?
, . «», , DECIMAL C#. [2], « Decimal , . Decimal . ». , , .
, , . , . – , . « » , .. , . DECIMAL C# « FLOAT», , . , .
IA-32 - . . , 64- , , Intel «» . 32- , . - , , , , , . , «» . - , – .
, , PL/1. , «» . , - IA-32 (, IA-16 1978 ) PL/1 . PL/1 [3], . , , 3-4% .
FIXED DECIMAL
PL/1 : FIXED, – FLOAT. , «FIXED/FLOAT» ( «/») . , , , «double».
PL/1 FIXED DECIMAL, , , FIXED DECIMAL(15,0) FIXED DECIMAL(12,4) .. . FIXED BINARY, , .
- BCD (Binary Coded Decimal). BCD . BCD . BCD , - , - . FIXED DECIMAL «» p ( 1 15) FLOOR((p+2)/2).
, 12345 5, PL/1 FLOOR((5+2)/2)=3 , 45 23 01. , 999 999 999 999 999. , , ,, 2013 $16,400,000,000,000, FIXED DECIMAL(14,0). , 60, PL/1 .
9 , 9 . , -2 (9-2)+1=8 1 98, 5 98 99 99.
, . – . , - .. , , « » . , 1.3 FIXED DECIMAL(2,1), 0.13 - FIXED DECIMAL(3,2), ( 0.169) FIXED DECIMAL(6,3), «», «» . «» 5, 6, PL/1 p1+p2+1, (a1a2-b1b2)+(a1b2+b1a2)i, .
- , , : , FLOOR/CEIL/TRUNC, .. FIXED DECIMAL . 8 99 ( ).
, . , .
;══════════════════ DECIMAL ═════════════════
PUBLIC ?DSUOP: ; PL/1
LEA ESI,[ESP]+4 ; 1 DECIMAL ( RET)
MOV ECX,8 ; DECIMAL
CLC ;
LEA EDI,[ESI+ECX] ; 2 DECIMAL
;---- ----
M2187:MOV AL,[EDI]
SBB AL,[ESI] ;
DAS
STOSB ;
INC ESI
LOOP M2187
POP ECX ;
MOV ESP,ESI ;
JMP ECX
;══════════════════ DECIMAL ══════════════════
EXTRN ?DOVER:NEAR
PUBLIC ?DADOP: ; PL/1
LEA ESI,[ESP]+4 ; 1 DECIMAL ( RET)
MOV ECX,8 ; DECIMAL
CLC ;
LEA EDI,[ESI+ECX] ; 2 DECIMAL
;---- DECIMAL ----
M2283:LODSB
ADC AL,[EDI] ;
DAA
STOSB ;
LOOP M2283
;---- ----
AND AL,0F0H ;
JZ @
CMP AL,90H ; ?
JNZ ?DOVER ;OVERFLOW DECIMAL
;---- ----
@: POP ECX ;
MOV ESP,ESI ;
JMP ECX
, - (ADC/SBB) (DAA/DAS), , 64- .
, , , , -, .
, FIXED DECIMAL: (, , ), PL/1 8 . , 15 31, 8 16, 16 31 . .
PL/1
, PL/1, , . FIXED DECIMAL FLOAT(53) – «double» .
test:proc main;
dcl x fixed decimal(6,3);
x=1.3;
x=x*0.13;
put skip data(x);
x=x/0.13;
put skip data(x);
dcl y float(53);
y=1.3;
y=y*0.13;
put skip data(y);
y=y/0.13;
put skip data(y);
end test;
.
FIXED DECIMAL , FLOAT - . , , , FIXED DECIMAL, , , « » .
, , PL/1, , , . , . , PL/1 E FLOAT, , . , , 25.0+1/3 , 1/3 (.. 15 ) «». 25.0+10/3 .
, , : . , , .
« », .. , , , PL/1 « 1». , . , [4] «» «». IA-32 , . 64- DAA/DAS , , , :
;--------------- DAA, X86-64 ------------------
PUBLIC DAA_X86_64:
PUSH RDX,RAX
LAHF
MOV EDX,EAX ;OLD CF OLD AL
AND AH,NOT 1B ; CF
;---- ----
TEST AH,10000B ; AF
JNZ @
PUSH RAX
AND AL,0FH
CMP AL,9 ; 9
POP RAX
JBE M2270
@: ADD AL,6 ;
OR AH,10000B ; AF
;---- ----
M2270:TEST DH,1B ; OLD CF
JNZ @
CMP DL,99H ;
JBE M2271
@: OR AH,1B ; CF
ADD AL,60H ;
;---- ----
M2271:SAHF
MOV [RSP],AL
POP RAX,RDX
RET
The basis of accurate calculations is the exact representation of numbers in the computer's memory. BCD representation makes it easy to organize accurate calculations for numbers of any length, i.e. any precision. It has been proven in practice that for most economic calculations, a length of 15 decimal places is sufficient.
Literature
1.D.R. Judd "Working with Files." Publishing house "Mir" Moscow, 1975
2.http: //msdn.microsoft.com/ru-ru/library/system.decimal.aspx
3. D. Karavaev "On the question of improving the programming language." RSDN Magazine # 4 2011
4. L.F. Sternberg "Programming errors and methods of work in the PL / 1 language", Moscow "Mechanical engineering", 1993