×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# compute with decimal number

## compute with decimal number

(OP)
Hello,

i have some problem with compute with decimal places. I have an amount without a decimal places and a field as minor unit code, where are the number of decimal places.
The compute below doesn´t work, if the minor unit code are greater than 6.
decimal = PIC 9(39V(9)6 COMP-4.

COMPUTE decimal = amount * (10 ** (dcml * -1))

With a floating-point  i get the proper value

COMPUTE decimal = amount * (10 ** (dcml * -1)) + 0.0E+00

Can somebody explain that or rather make proposal for a better way ?

Regards

Bernd

Replies continue below

### RE: compute with decimal number

COMPUTE uses its target variable for computing its immediate results.  Therefore, COMPUTE will produce incorrect results if its target value is not defined to properly receive all intermediate values.  COMPUTE is a bad idea anyway for this reason (function that is otherwise not immediately evident).

So split this calculation up and use the proper sizes of variables for receiving each intermediate result.  Then you should be fine.

We could help you further in doing this, if necessary, but you do not give how you defined "amount" and "dcml".

Measurement is not management.

### RE: compute with decimal number

(OP)
Hello,

thanks.
Below the data definition:

decimal PIC 9(3)V(9)6 COMP-4
amount  PIC 9(18) COMP-3
Dcml    PIC 9(9).

### RE: compute with decimal number

First point: It's usually standard to define values with sign storage (S9(4) BINARY as opposed to 9(4) binary ).  This would be the first thing I would do here.

COMPUTE decimal = amount * (10 ** (dcml * -1))

Order of ops - write your code so you split each step.

1) dcml * -1  (this might be the start of a problem) --> INT01
2) 10 ** Int01 --> INT02
3) amount * INT02 --> final result.

To follow the formula as written:
INT01 should be the same size as dcml but signed.
INT02 should be a decimal the size of the maximum of dcml.

I would try to rework the formula as well, but odds are that much can be simplified.  If DCML truly has no sign and is always positive, you can apply algebra (n^-3 = 1 / n^3).  Furthermore, since you use 10 as a base power (10^-3 = 1 / 10^3 = 0.001), and dcml would have to be severely limited in size for this formula to work *well* (maximum of 18), and is an integer, I would consider other methods than using the power functionality, such as redefinitions.

In other words, in the long run it would be more profitable to rethink your approach.

Measurement is not management.

### RE: compute with decimal number

(OP)
Hello

thanks

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!