Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

new in ABAP

Former Member
0 Kudos

Hello Everybody

what is the difference between pack and floating point datatypes, i ABAP Objects book, there is mentioned that if you are concious about accuracy then use pack datatype, otherwise use floating point. i write a program

data p1 type p.

p = 3 / 4.

write p. the result of this statement is 0,75

data f1 type f.

f1 = 3 / 4.

write f1 and the result of this is statement is 7,5000000E

so when we use pack number and what floating point give me example in detail and clear

Thanks

Rai

1 ACCEPTED SOLUTION

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

This is taken from ABAP Help Documentation.

ABAP contains a range of built-in elementary data types: Character (text), Numerical character (number string), Date, Time Integer, Floating point number, Packed number, and HeX code). Some of these data types are fully types (D, T, I, F), others (C, N, P, X) are generic, in the sense that you must provide further type information (length, for type p also the number of decimal places) when you create the data object .

The value range of type P fields depends on their length and the number of decimal places. P fields can be 1 to 16 bytes long, with two decimal digits packed into each byte, and one decimal digit and the sign packed into the last byte. There can be up to 14 decimal places. Auxiliary fields for intermediate results are always 16 bytes long and can thus hold up to 31 decimal digits. To ensure that the decimal point is correctly calculated, you should always set the program attribute "fixed point arithmetic". Otherwise, all numbers are specified as integers and all intermediate results for the next integer are rounded. If "fixed point arithmetic" is not set, the decimal places defined for the number only appear when outputting with WRITE.

Fixed point arithmetic is decimal arithmetic and is similar to using a pocket calculator or calculating with paper and pencil.

Type P is typically used for sizes, lengths, weights and sums of money.

Rule: If you want to calculate "down to the last penny", you should use the type P.

Type F values range from /- 2.2250738585072014E-308 to 1.7976931348623157E308, as well as the number 0, with an accuracy of at least 15 decimal places.

You cannot enter floating point numbers directly in your programs. Instead, you must use text literals that can be interpreted as floating point numbers. You may use the following formats:

Decimal numbers with or without sign, with or without decimal point. The form <mantissa> E<exponent>, where the mantissa is a decimal. The exponent may be specified either with or without sign. You may also use spaces before or after the number. Examples of text literals with "floating point numbers":

'1', '-12.34567', '-765E-04', '1234E5', '12E34', '+12.3E-4', '1E160'.

Use floating point arithmetic if you need a very large value range or you are making decimal calculations, but be aware of the following features of floating point arithmetic.

Internally, the exponent and the mantissa of floating point numbers are stored separately, each in two parts. This can lead to unexpected results, despite the high degree of intrinsic accuracy. These occur mainly when performing conversions from and to type F.

For example, the number 1.5 can be represented exactly in this notation, since 1.5 = 120 + 12**(-1), but the number 0.15 can only be represented approximately by the number 0,14999999999999999. If you round 0.15 up to 1 valid digit, the result is 0.1 rather than 0.2 as you would expect. On the other hand, the number 1.5E-12 is represented by the number 1.5000000000000001E-12, which would be rounded to 2E-12.

Another example which actually occurred is the calculation of 7.27% of 73050 to an accuracy of 2 decimal places. The intermediate result 5.3107349999999997E+03, since the correct result, 5310.735, cannot be represented exactly in two parts with 53 bits. (If the hardware cannot represent a real number exactly, it uses the next representable floating point number. After rounding, you therefore get 5310.73 rather than 5310.74 as you would expect.

The ABAP runtime system calculates commercially and not "numerically" like the underlying machine arithmetic. According to the rounding algorithm of the latter, the end digit 5 must always be rounded to the nearest even number (not the next largest number), i.e. from 2.5 to 2, 3.5 to 4.

You should also note that multiplication using powers of 10 (positive or negative), is not an exact operation. For example, although 100.5 can be represented exactly in two parts, after the operation

F = F / 100 * 100

F has the value 100.49999999999999.

As well as rounding errors, the restricted number of decimal places for the mantissa can lead to the loss of trailing digits. For example, 1 - 1.0000000000000001 results in zero.

This means that you cannot rely on the last digits in floating point arithmetic. In particular, you should not usually test two floating point numbers for equality; instead, you should check whether the relative difference abs((a - b)/a) is less than a predefined limit, e.g. 10**(-7).

Check this link also.

http://www.sapgenie.com/abap/example_code.htm

3 REPLIES 3

Former Member
0 Kudos

Hi,

Go through this link and find helpful sample programs.

http://www.sapdevelopment.co.uk/tips/tipshome.htm

Alos in development system type ABAPDOCU in the command prompt u can get a lot of sample programs with results.

U can debug that and find the flow.

Thanks & Regards,

Judith.

Former Member
0 Kudos

Hello Rai,

I shall make a rather non-technical attempt to resolve your doubts.

1. Type P is a generic data type. You will have to explicitly specify the number of places after the decimal point. But on the other hand, the type F is a fully defines the technical attributes of its data object.

2. Type P is used when you deal with data where the fractional part makes sense. For example, the number of centimeters in an inch, the conversion between Euro and USD, The quantity of gold (in grams) that can be bought for 1000 USD etc.,. In all these cases, the fractional part is very important and cannot possible be ignored without significant loss.

But a type P variable is used to represent the distance between the earth and the sun, the number of seconds in 10 years, the number of kilometers in a light year, the population of the world etc., All these numbers are very huge and the fractional part either doesn't make sense , or does not really matter.(That is the reason why the E is always there to denote the exponent)

And remember , whenever you are workin with data of <i>type p</i>, always set the <i>Fixed Poinmt Arithmetic</i> attribute for your program.

If you want further explanation , get back. I will see if I can get you some examples.

Regards,

Anand Mandalika.

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

This is taken from ABAP Help Documentation.

ABAP contains a range of built-in elementary data types: Character (text), Numerical character (number string), Date, Time Integer, Floating point number, Packed number, and HeX code). Some of these data types are fully types (D, T, I, F), others (C, N, P, X) are generic, in the sense that you must provide further type information (length, for type p also the number of decimal places) when you create the data object .

The value range of type P fields depends on their length and the number of decimal places. P fields can be 1 to 16 bytes long, with two decimal digits packed into each byte, and one decimal digit and the sign packed into the last byte. There can be up to 14 decimal places. Auxiliary fields for intermediate results are always 16 bytes long and can thus hold up to 31 decimal digits. To ensure that the decimal point is correctly calculated, you should always set the program attribute "fixed point arithmetic". Otherwise, all numbers are specified as integers and all intermediate results for the next integer are rounded. If "fixed point arithmetic" is not set, the decimal places defined for the number only appear when outputting with WRITE.

Fixed point arithmetic is decimal arithmetic and is similar to using a pocket calculator or calculating with paper and pencil.

Type P is typically used for sizes, lengths, weights and sums of money.

Rule: If you want to calculate "down to the last penny", you should use the type P.

Type F values range from /- 2.2250738585072014E-308 to 1.7976931348623157E308, as well as the number 0, with an accuracy of at least 15 decimal places.

You cannot enter floating point numbers directly in your programs. Instead, you must use text literals that can be interpreted as floating point numbers. You may use the following formats:

Decimal numbers with or without sign, with or without decimal point. The form <mantissa> E<exponent>, where the mantissa is a decimal. The exponent may be specified either with or without sign. You may also use spaces before or after the number. Examples of text literals with "floating point numbers":

'1', '-12.34567', '-765E-04', '1234E5', '12E34', '+12.3E-4', '1E160'.

Use floating point arithmetic if you need a very large value range or you are making decimal calculations, but be aware of the following features of floating point arithmetic.

Internally, the exponent and the mantissa of floating point numbers are stored separately, each in two parts. This can lead to unexpected results, despite the high degree of intrinsic accuracy. These occur mainly when performing conversions from and to type F.

For example, the number 1.5 can be represented exactly in this notation, since 1.5 = 120 + 12**(-1), but the number 0.15 can only be represented approximately by the number 0,14999999999999999. If you round 0.15 up to 1 valid digit, the result is 0.1 rather than 0.2 as you would expect. On the other hand, the number 1.5E-12 is represented by the number 1.5000000000000001E-12, which would be rounded to 2E-12.

Another example which actually occurred is the calculation of 7.27% of 73050 to an accuracy of 2 decimal places. The intermediate result 5.3107349999999997E+03, since the correct result, 5310.735, cannot be represented exactly in two parts with 53 bits. (If the hardware cannot represent a real number exactly, it uses the next representable floating point number. After rounding, you therefore get 5310.73 rather than 5310.74 as you would expect.

The ABAP runtime system calculates commercially and not "numerically" like the underlying machine arithmetic. According to the rounding algorithm of the latter, the end digit 5 must always be rounded to the nearest even number (not the next largest number), i.e. from 2.5 to 2, 3.5 to 4.

You should also note that multiplication using powers of 10 (positive or negative), is not an exact operation. For example, although 100.5 can be represented exactly in two parts, after the operation

F = F / 100 * 100

F has the value 100.49999999999999.

As well as rounding errors, the restricted number of decimal places for the mantissa can lead to the loss of trailing digits. For example, 1 - 1.0000000000000001 results in zero.

This means that you cannot rely on the last digits in floating point arithmetic. In particular, you should not usually test two floating point numbers for equality; instead, you should check whether the relative difference abs((a - b)/a) is less than a predefined limit, e.g. 10**(-7).

Check this link also.

http://www.sapgenie.com/abap/example_code.htm