I had a strange problem making some calculations.
If I use some variables ad I sum them, the result is correct, but multiplying it the result is wrong.
Making the same multiply with the same total the result is correct.
- Code: Select all Expand view
Function calc
local a1:=0 , a2:=0 , a3:=0 , a4:=0 , tot:=0 , iva:=0
a1:=11.57
a2:=4.20
a3:=0.56
a4:=-16.28
tot:=a1 + a2 + a3 + a4
iva:=Round(tot*10/100,2)
? tot // 0.05 correct
? iva // 0.00 wrong
tot:=0.05
iva:=Round(tot*10/100,2)
? tot // 0.05 correct
? iva // 0.01 correct
return nil
I made the same program in COBOL and the result is correct
- Code: Select all Expand view
IDENTIFICATION DIVISION.
PROGRAM-ID. CALCOLA.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-AT.
OBJECT-COMPUTER. IBM-AT.
SPECIAL-NAMES. DECIMAL-POINT IS COMMA.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OPTIONAL PILOTA
ASSIGN TO RANDOM "PILOTA.DAT"
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS NUMREPI.
DATA DIVISION.
FILE SECTION.
FD PILOTA
BLOCK CONTAINS 1 RECORDS
LABEL RECORD IS STANDARD
DATA RECORD IS REC-PIL.
01 REC-PIL.
02 PIL-NUMBOLLE PIC 9(5).
02 PIL-NUMFATT PIC 9(5).
02 PIL-CLIENTI PIC 9(6).
02 PIL-FORNIT PIC 9(6).
02 PIL-CARICHI PIC 9(6).
02 PIL-SCARICHI PIC 9(6).
02 PIL-SCADENZE PIC 9(5).
02 PIL-FILLER PIC X.
WORKING-STORAGE SECTION.
77 NUMREPI PIC 9 VALUE 1.
77 RISPOSTA PIC X.
77 CAMPO-DISPLAY PIC 99,99.
01 CAMPI-CALCOLO.
02 VALORE1 PIC S99V99.
02 VALORE2 PIC S99V99.
02 VALORE3 PIC S99V99.
02 VALORE4 PIC S99V99.
02 VALORE5 PIC S99V99.
02 TOTALE1 PIC S99V99.
02 ALIQUOTA PIC S99V99.
02 IVA PIC S99V99.
PROCEDURE DIVISION.
PARTENZA.
INITIALIZE CAMPI-CALCOLO.
MOVE 11,57 TO VALORE1.
MOVE 4,20 TO VALORE2.
MOVE 0,56 TO VALORE3.
MOVE -16,28 TO VALORE4.
ADD VALORE1 TO TOTALE1.
ADD VALORE2 TO TOTALE1.
ADD VALORE3 TO TOTALE1.
ADD VALORE4 TO TOTALE1.
MOVE 10 TO ALIQUOTA.
COMPUTE IVA ROUNDED = TOTALE1 * ALIQUOTA / 100.
FINE-LAVORO.
MOVE TOTALE1 TO CAMPO-DISPLAY
DISPLAY CAMPO-DISPLAY.
MOVE IVA TO CAMPO-DISPLAY
DISPLAY CAMPO-DISPLAY.
ACCEPT RISPOSTA.
STOP-RUN.
STOP RUN.
TOTALE1 is equal to 0.05 and IVA is 0.01