Your host:
Stephen Chapman
Stephen Chapman

Truncated Numbers

Question: I'm new at PL/I and have written a program. A simple X / 2 is doing some weird things. An example

X dec fixed(15) Y dec fixed(15,1)
Y = X/2

Now the problem is that this simple division is giving some weird results:

IF X = 1, Y = 0.0 (I would expect 0.5)
bart

Answer: With those definitions, where X is set to 1 and the above code is run the following are the steps that are performed.:

  • Divide X by 2. 1/2 = 0.5.
  • Multiply the number of decimal places in both to work out how many decimals to keep - 1 x 0 = 0.
  • Move the result (0) into Y. Y has one decimal place while the result being moved into it doesn't have any so .0 gets added to the end.

To get the 0.5 you expect try

Y = X/2.0

go to top

Add to del.icio.us

Credit - Refinance - Personal Loan - Internet Marketing