What is the difference between Floating-point and Packed Dec

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Rajat Singh
Registered Member
Posts: 12
Joined: Tue Dec 02, 2014 9:46 am

What is the difference between Floating-point and Packed Dec

Post by Rajat Singh »

Hi,

What is the difference between Floating-point and Packed Decimal? I have read explnation about it and searched in google but more I read more I get puzzled about it. For "normal life calculation" will not theyjust behave just same? Or should there be compiler considerations too, always?
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 825
Joined: Wed Sep 11, 2013 3:57 pm

Re: What is the difference between Floating-point and Packed

Post by enrico-sorichetti »

Floating-point and Packed Decimal?
Floating-point ... the name tells
Packed Decimal ... just a good old plain DECIMAL integer

read and meditate on the z architecture principle of operations
http://www-01.ibm.com/support/docview.w ... c500428f9a

chapters 8/9/18/19/20
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1886
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: What is the difference between Floating-point and Packed

Post by Robert Sample »

For "normal life calculation" will not theyjust behave just same?
This is ABSOLUTELY false. If you have a 7-digit packed decimal variable with value 9999999 and add 1 to it, what result do you get? If you have a floating point variable with the same value and add 1 to it, what do you get? Hint: they will NOT get the same result. This has nothing to do with compiler considerations and everything to do with numeric representation and precision / accuracy of results.
Rajat Singh
Registered Member
Posts: 12
Joined: Tue Dec 02, 2014 9:46 am

Re: What is the difference between Floating-point and Packed

Post by Rajat Singh »

Thanks enrico.

Robert,

I think I'll get 0000000 in case of packed decimal but what would I get for floating point?
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 825
Joined: Wed Sep 11, 2013 3:57 pm

Re: What is the difference between Floating-point and Packed

Post by enrico-sorichetti »

ig You are curious and have a pl/1 compiler available

just compile and run

Code: Select all

****** ***************************** Top of Data ***************************
000001  Float:
000002      Proc Options(Main);
000003      dcl pliretc builtin;
000004      dcl ( f4 ) real float binary(21)  ;
000005      dcl ( f8 ) real float binary(53)  ;
000006      dcl ( f16) real float binary(109) ;
000007      dcl ( d4 ) real float decimal(6)  ;
000008      dcl ( d8 ) real float decimal(16) ;
000009      dcl ( d16) real float decimal(33) ;
000010      f4  = 9999999 ;
000011      f8  = 9999999 ;
000012      f16 = 9999999 ;
000013      d4  = 9999999 ;
000014      d8  = 9999999 ;
000015      d16 = 9999999 ;
000016      put skip list ('Pl/1  said... Float Startd');
000017      put skip list ('f4      =', f4 ) ;
000018      put skip list ('f4 + 1  =', f4 + 1 ) ;
000019      put skip list ('f8      =', f8) ;
000020      put skip list ('f8  + 1 =', f8 + 1 ) ;
000021      put skip list ('f16     =', f16) ;
000022      put skip list ('f16 + 1 =', f16 + 1 ) ;
000023      put skip list ('d4      =', d4 ) ;
000024      put skip list ('d4 + 1  =', d4 + 1 ) ;
000025      put skip list ('d8      =', d8) ;
000026      put skip list ('d8  + 1 =', d8 + 1 ) ;
000027      put skip list ('d16     =', d16) ;
000028      put skip list ('d16 + 1 =', d16 + 1 ) ;
000029      put skip list ('Pl/1  said... Float Ended ');
000030      call pliretc(0) ;
000031      End;
****** **************************** Bottom of Data *************************
an see what is going on when using ...

Code: Select all

999999
9999999
99999999
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1886
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: What is the difference between Floating-point and Packed

Post by Robert Sample »

The floating point number would be 10 million, not 0 -- hence there are definite differences in floating point and packed decimal variables. A floating point value will have 7 to 8 digits of accuracy; an eye-opening experience would be to write and execute a COBOL program that uses floating point variables to multiply 30 times 29 times 28 times ... times 1 and then do the same multiplication from 1 to 30. Due to the limits of floating point variables, the values are not the same.
Rajat Singh
Registered Member
Posts: 12
Joined: Tue Dec 02, 2014 9:46 am

Re: What is the difference between Floating-point and Packed

Post by Rajat Singh »

Thanks enrico. I don't have PL/I compiler so I could not use the example.

But I looked at this article from wiki: https://en.wikipedia.org/wiki/Floating_point and these line are helping. Though I am trying to understand how I can apply these thoughts to COBOL too:

"The term floating point refers to the fact that a number's radix point (decimal point, or, more commonly in computers, binary point) can "float"; that is, it can be placed anywhere relative to the significant digits of the number. This position is indicated as the exponent component, and thus the floating-point representation can be thought of as a kind of scientific notation."
Rajat Singh
Registered Member
Posts: 12
Joined: Tue Dec 02, 2014 9:46 am

Re: What is the difference between Floating-point and Packed

Post by Rajat Singh »

Robert Sample wrote:The floating point number would be 10 million, not 0 -- hence there are definite differences in floating point and packed decimal variables. A floating point value will have 7 to 8 digits of accuracy; an eye-opening experience would be to write and execute a COBOL program that uses floating point variables to multiply 30 times 29 times 28 times ... times 1 and then do the same multiplication from 1 to 30. Due to the limits of floating point variables, the values are not the same.
Thanks for the hint. I shall try that. I thought to simulate it in excel too and I think it works similar there too.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1886
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: What is the difference between Floating-point and Packed

Post by Robert Sample »

Yes, the same thing will happen with any computer application / language -- the issue is with the number of bits used to represent data in floating point values. Mathematically, one-third has an infinite number of digits (0.3333333....) but computers cannot store an infinite number of digits and hence the values used in a computer are approximations of the mathematical value. Do enough multiplications and you're going to exceed the number of bits used eventually, at which time the results start diverging from the mathematical value.
Rajat Singh
Registered Member
Posts: 12
Joined: Tue Dec 02, 2014 9:46 am

Re: What is the difference between Floating-point and Packed

Post by Rajat Singh »

Thank you all. :)
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”