View previous topic :: View next topic
|
Author |
Message |
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
I have three fields.
x1 = 00.0010000 PIC S9(2)V9(7) COMP-3.
x2 = 0000.04025 PIC S9(4)V9(5) COMP-3.
x3 = 000100000.00 PIC S9(9)V9(2) COMP-3.
when I muilply these and the result is in comp-2(floating point field), the result is not 4.025E0 but 4.0249999999999999E+00.
Y = x1 * x2 * x3.
I read in the manuals that this is becuase all the fields are converted to floating point. How does this conversion take place? And how is the result justififed. I cannot use any other type field than floating point field. |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
The primary problem is the total number of digist possible, your numbers are exceeding some limit the compiler uses in decieding how to do the calculation.
Floating point has a larger range but you may need to half round the result.
If x3 is always that large, pre calculating x1 * x3 and then multplying x2 to the intermediat total might keep everything decimal..... |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Do you have any problem with the result?
The difference between two result is insignificant (1 * 10^-16). Unless you are doing scientific work the result should be OK for you.
I will suggest not going to the details of conversion because it is quite different from standard IEEE 754 and difficult to follow (Atleast to me). |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
niks_jude wrote: |
x1 = 00.0010000 PIC S9(2)V9(7) COMP-3.
x2 = 0000.04025 PIC S9(4)V9(5) COMP-3.
x3 = 000100000.00 PIC S9(9)V9(2) COMP-3.
|
The compiler only sees the max size possible:
2+4+9 & 7+5+2 or S9(15)v9(14)
which exceeds the ARITH(COMPAT) compiler option of 18 digits.
Try the compiler option of ARITH(EXTEND) which allows 31 digits. |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
I cannot try with x3 = x1 * x3
and y = x3 * x2.
because it may lead to truncation in extreme conditions.
Do I have any other option.
Because its a production problem, In cannot change the compiler option also. |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
niks_jude wrote: |
I cannot try with x3 = x1 * x3
and y = x3 * x2.
because it may lead to truncation in extreme conditions. |
Heck, we couldn't have that.... max conditions would exceed 18 total digits....
Quote: |
Do I have any other option. |
Change the compiler option....
Quote: |
Because its a production problem, In cannot change the compiler option also. |
Now this does not make sense, it is a compile option, not a runtime option.
Any method you use to fix this will require a compile, you can't deny that and if a different compile option will fix it, use it - that's what they are there for.......
Or just half round and live with the close answer..... |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
This would require the production compiler options to be chnaged which is not feasible. As other elements may get out of synch.
Is it possible to do this with tricky use of intermediate variables? |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
I believe that you can include Compiler Options in the source. That would remove the 'special' compiliation circumstances.
Not knowing what kind of application is in use hinders 'work around solutions'. I have worked for banks and transportation companies - neither of which ever required floating point. So, I can't (should not) make any rash judgements or decisions....... but, why is their only one module in the company that requires this kind of arithmetics? |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
niks_jude wrote: |
This would require the production compiler options to be chnaged which is not feasible. As other elements may get out of synch. |
Wrong, the "production compiler options" can be overridden and/or supplemented within the COBOL source code itself and would only apply to that one module.
Quote: |
Is it possible to do this with tricky use of intermediate variables? |
I could not see any reasonable way that would not exceed 18 digits..... |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
Thanks I would have to ask the senior guys if this is allowed in our production environmenr. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
Is it possible to do this with tricky use of intermediate variables |
It is a guarantee that you do not want to seek and or use some "tricky thing" even if you find one that you think might work. . .
I am not clear on why you have used floating-point for your result. If you use
Code: |
77 FLD-X1 PIC S9(2)V9(7) COMP-3.
77 FLD-X2 PIC S9(4)V9(5) COMP-3.
77 FLD-X3 PIC S9(9)V9(2) COMP-3.
77 FLD-Y PIC S9(9)V9(9) COMP-3.
77 FLD-Y-PRT PIC 9(9).9(9).
MOVE 00.0010000 TO FLD-X1.
MOVE 0000.04025 TO FLD-X2.
MOVE 000100000.00 TO FLD-X3.
COMPUTE FLD-Y = FLD-X1 * FLD-X2 * FLD-X3.
MOVE FLD-Y TO FLD-Y-PRT.
DISPLAY FLD-Y.
DISPLAY FLD-Y-PRT.
|
you get
Code: |
000000004025000000
000000004.025000000 |
which i believe is correct. . .
You can shift the number of digits depending on your requirement, but i believe you would be able to work within 18 digits. |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
No the requirement is following ---------
Use floating point till last step. In the last step round the floating point result to two decimal places. |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
niks_jude wrote: |
No the requirement is following ---------
Use floating point till last step. In the last step round the floating point result to two decimal places. |
Well then, do it......
Do you know how to round the result to two decimals? |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
Yes. I thought of using some intemediate flaoting variables so that the dip in value is not there. |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
Won't help, just half round and be done with it..... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Please explain the requirement that restricts the process to floating point. I do not understand such a requirement for what it appears you need to accomplish.
What business reason dictates this? If there is no business reason, using standard arithmetic would be a better choice. |
|
Back to top |
|
|
niks_jude Warnings : 1 Active User
Joined: 01 Dec 2006 Posts: 144 Location: Mumbai
|
|
|
|
We have been provided this in design specifications and we have to abide by it. Even I cannot really understand the requirement but I have to strictly follow the design specifications. Thanks. |
|
Back to top |
|
|
|