Skip to content

Commit d193cb3

Browse files
authored
Add FUNCTION FRACTION-PART and fix about double value (#224)
1 parent 8b06340 commit d193cb3

File tree

5 files changed

+97
-49
lines changed

5 files changed

+97
-49
lines changed

libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolIntrinsic.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,4 +2028,25 @@ public static AbstractCobolField funcExceptionStatus() {
20282028
}
20292029
return currField;
20302030
}
2031+
2032+
/**
2033+
* cob_intr_fraction_partの実装
2034+
*
2035+
* @param srcfield
2036+
* @return
2037+
*/
2038+
public static AbstractCobolField funcFractionPart(AbstractCobolField srcfield) {
2039+
CobolFieldAttribute attr =
2040+
new CobolFieldAttribute(
2041+
CobolFieldAttribute.COB_TYPE_NUMERIC_BINARY,
2042+
18,
2043+
18,
2044+
CobolFieldAttribute.COB_FLAG_HAVE_SIGN,
2045+
null);
2046+
AbstractCobolField field = CobolFieldFactory.makeCobolField(8, (CobolDataStorage) null, attr);
2047+
makeFieldEntry(field);
2048+
2049+
currField.moveFrom(srcfield);
2050+
return currField;
2051+
}
20312052
}

libcobj/src/jp/osscons/opensourcecobol/libcobj/data/CobolNumericDoubleField.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,7 @@ public byte[] getBytes() {
4040

4141
@Override
4242
public String getString() {
43-
CobolFieldAttribute thisAttr = this.getAttribute();
44-
int flag = thisAttr.isFlagHaveSign() ? CobolFieldAttribute.COB_FLAG_HAVE_SIGN : 0;
45-
CobolFieldAttribute attr =
46-
new CobolFieldAttribute(
47-
CobolFieldAttribute.COB_TYPE_NUMERIC,
48-
thisAttr.getDigits(),
49-
thisAttr.getScale(),
50-
flag,
51-
thisAttr.getPic());
52-
CobolDataStorage storage = new CobolDataStorage(thisAttr.getDigits());
53-
CobolNumericField numericField = new CobolNumericField(thisAttr.getDigits(), storage, attr);
54-
numericField.moveFrom(this);
55-
return numericField.getString();
43+
return String.format("%.18f", this.getDouble());
5644
}
5745

5846
@Override

libcobj/src/jp/osscons/opensourcecobol/libcobj/data/CobolNumericField.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,15 @@ private void moveBinaryToDisplay(AbstractCobolField field) {
382382

383383
private void moveDoubleToDisplay(AbstractCobolField field) {
384384
CobolFieldAttribute thisAttr = this.getAttribute();
385-
double val = Math.abs(field.getDouble() * thisAttr.getScale());
386-
String formatter = "%0" + thisAttr.getDigits() + "." + thisAttr.getScale() + "f";
387-
String valString = String.format(formatter, val).replace(".", "");
385+
double val = Math.abs(field.getDouble());
388386
int startIndex = 0;
389387
if (thisAttr.isFlagHaveSign()
390388
&& thisAttr.isFlagSignLeading()
391389
&& thisAttr.isFlagSignSeparate()) {
392390
startIndex = 1;
393391
}
394392
CobolDataStorage dstStorage = this.getDataStorage().getSubDataStorage(startIndex);
395-
dstStorage.memcpy(valString, thisAttr.getDigits());
393+
dstStorage.memcpy(String.valueOf(val), thisAttr.getDigits());
396394
this.putSign(field.getSign() >= 0 ? 1 : -1);
397395
}
398396

libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public class CobolFile {
166166
protected static String file_open_name;
167167
protected static byte[] file_open_buff = new byte[1024];
168168

169-
protected static final String[] prefix = { "DD_", "dd_", "" };
169+
protected static final String[] prefix = {"DD_", "dd_", ""};
170170
protected static final int NUM_PREFIX = prefix.length;
171171

172172
protected static int eop_status = 0;
@@ -175,16 +175,16 @@ public class CobolFile {
175175
private static List<CobolFile> file_cache = new ArrayList<CobolFile>();
176176

177177
protected static int[] status_exception = {
178-
0,
179-
CobolExceptionId.COB_EC_I_O_AT_END,
180-
CobolExceptionId.COB_EC_I_O_INVALID_KEY,
181-
CobolExceptionId.COB_EC_I_O_PERMANENT_ERROR,
182-
CobolExceptionId.COB_EC_I_O_LOGIC_ERROR,
183-
CobolExceptionId.COB_EC_I_O_RECORD_OPERATION,
184-
CobolExceptionId.COB_EC_I_O_FILE_SHARING,
185-
CobolExceptionId.COB_EC_I_O,
186-
CobolExceptionId.COB_EC_I_O,
187-
CobolExceptionId.COB_EC_I_O_IMP
178+
0,
179+
CobolExceptionId.COB_EC_I_O_AT_END,
180+
CobolExceptionId.COB_EC_I_O_INVALID_KEY,
181+
CobolExceptionId.COB_EC_I_O_PERMANENT_ERROR,
182+
CobolExceptionId.COB_EC_I_O_LOGIC_ERROR,
183+
CobolExceptionId.COB_EC_I_O_RECORD_OPERATION,
184+
CobolExceptionId.COB_EC_I_O_FILE_SHARING,
185+
CobolExceptionId.COB_EC_I_O,
186+
CobolExceptionId.COB_EC_I_O,
187+
CobolExceptionId.COB_EC_I_O_IMP
188188
};
189189
public String select_name;
190190
public byte[] file_status;
@@ -232,8 +232,7 @@ public void setLinorkeyptr(Linage ptr) {
232232
this.linorkeyptr = ptr;
233233
}
234234

235-
public CobolFile() {
236-
}
235+
public CobolFile() {}
237236

238237
public CobolFile(
239238
String selectName,
@@ -290,8 +289,7 @@ public CobolFile(
290289
}
291290

292291
/**
293-
* libcob/fileio.cのsave_statusの実装
294-
* RETURN_STATUSマクロは実装できないため,本メソッドの呼び出し後の次の文はreturn;を書くこと.
292+
* libcob/fileio.cのsave_statusの実装 RETURN_STATUSマクロは実装できないため,本メソッドの呼び出し後の次の文はreturn;を書くこと.
295293
*
296294
* @param status
297295
* @param fnstatus
@@ -355,7 +353,8 @@ protected boolean file_linage_check() {
355353
Linage lingptr = getLinorkeyptr();
356354
lingptr.setLinLines(lingptr.getLinage().getInt());
357355

358-
outer: {
356+
outer:
357+
{
359358
if (lingptr.getLinLines() < 1) {
360359
break outer;
361360
}
@@ -768,22 +767,25 @@ public int open_(String filename, int mode, int sharing) throws IOException {
768767
fp = FileChannel.open(Paths.get(filename), StandardOpenOption.READ);
769768
break;
770769
case COB_OPEN_OUTPUT:
771-
fp = FileChannel.open(
772-
Paths.get(filename),
773-
StandardOpenOption.WRITE,
774-
StandardOpenOption.CREATE,
775-
StandardOpenOption.TRUNCATE_EXISTING);
770+
fp =
771+
FileChannel.open(
772+
Paths.get(filename),
773+
StandardOpenOption.WRITE,
774+
StandardOpenOption.CREATE,
775+
StandardOpenOption.TRUNCATE_EXISTING);
776776
break;
777777
case COB_OPEN_I_O:
778-
fp = FileChannel.open(
779-
Paths.get(filename),
780-
StandardOpenOption.READ,
781-
StandardOpenOption.WRITE,
782-
StandardOpenOption.CREATE);
778+
fp =
779+
FileChannel.open(
780+
Paths.get(filename),
781+
StandardOpenOption.READ,
782+
StandardOpenOption.WRITE,
783+
StandardOpenOption.CREATE);
783784
break;
784785
case COB_OPEN_EXTEND:
785-
fp = FileChannel.open(
786-
Paths.get(filename), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
786+
fp =
787+
FileChannel.open(
788+
Paths.get(filename), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
787789
break;
788790
default:
789791
break;
@@ -1073,7 +1075,8 @@ public void write(AbstractCobolField rec, int opt, AbstractCobolField fnstatus)
10731075
}
10741076

10751077
String openMode = openModeToString(this.last_open_mode);
1076-
if (invokeFun(COB_IO_WRITE, this, null, rec.getDataStorage(), fnstatus, openMode, null, null) != 0) {
1078+
if (invokeFun(COB_IO_WRITE, this, null, rec.getDataStorage(), fnstatus, openMode, null, null)
1079+
!= 0) {
10771080
return;
10781081
}
10791082

tests/run.src/functions.at

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,6 @@ AT_CHECK([java prog], [0],
674674
AT_CLEANUP
675675

676676
AT_SETUP([FUNCTION FRACTION-PART])
677-
AT_CHECK([${SKIP_TEST}])
678677

679678
AT_DATA([prog.cob], [
680679
IDENTIFICATION DIVISION.
@@ -1035,8 +1034,7 @@ AT_CHECK([java prog], [0],
10351034

10361035
AT_CLEANUP
10371036

1038-
AT_SETUP([FUNCTION MIDRANGE])
1039-
AT_CHECK([${SKIP_TEST}])
1037+
AT_SETUP([FUNCTION MIDRANGE(calculation result is negative)])
10401038

10411039
AT_DATA([prog.cob], [
10421040
IDENTIFICATION DIVISION.
@@ -1056,6 +1054,46 @@ AT_CHECK([java prog], [0],
10561054

10571055
AT_CLEANUP
10581056

1057+
AT_SETUP([FUNCTION MIDRANGE(calculation result is positive)])
1058+
1059+
AT_DATA([prog.cob], [
1060+
IDENTIFICATION DIVISION.
1061+
PROGRAM-ID. prog.
1062+
DATA DIVISION.
1063+
WORKING-STORAGE SECTION.
1064+
PROCEDURE DIVISION.
1065+
DISPLAY FUNCTION MIDRANGE ( 3 14 0 8 3 )
1066+
END-DISPLAY.
1067+
STOP RUN.
1068+
])
1069+
1070+
AT_CHECK([${COMPILE} prog.cob])
1071+
AT_CHECK([java prog], [0],
1072+
[7.000000000000000000
1073+
])
1074+
1075+
AT_CLEANUP
1076+
1077+
AT_SETUP([FUNCTION MIDRANGE(calculation result is decimal)])
1078+
1079+
AT_DATA([prog.cob], [
1080+
IDENTIFICATION DIVISION.
1081+
PROGRAM-ID. prog.
1082+
DATA DIVISION.
1083+
WORKING-STORAGE SECTION.
1084+
PROCEDURE DIVISION.
1085+
DISPLAY FUNCTION MIDRANGE ( 2 2 3 9 )
1086+
END-DISPLAY.
1087+
STOP RUN.
1088+
])
1089+
1090+
AT_CHECK([${COMPILE} prog.cob])
1091+
AT_CHECK([java prog], [0],
1092+
[5.500000000000000000
1093+
])
1094+
1095+
AT_CLEANUP
1096+
10591097
AT_SETUP([FUNCTION MIN])
10601098

10611099
AT_DATA([prog.cob], [

0 commit comments

Comments
 (0)