Concepts of Programming Languages

(Sean Pound) #1

248 Chapter 6 Data Types


6.2.1.3 Complex
Some programming languages support a complex data type—for example,
Fortran and Python. Complex values are represented as ordered pairs of
floating-point values. In Python, the imaginary part of a complex literal is speci-
fied by following it with a j or J—for example,

(7 + 3j)

Languages that support a complex type include operations for arithmetic
on complex values.

6.2.1.4 Decimal
Most larger computers that are designed to support business systems applica-
tions have hardware support for decimal data types. Decimal data types store
a fixed number of decimal digits, with the decimal point at a fixed position in
the value. These are the primary data types for business data processing and
are therefore essential to COBOL. C# and F# also have decimal data types.
Decimal types have the advantage of being able to precisely store dec-
imal values, at least those within a restricted range, which cannot be done
with floating-point. For example, the number 0.1 (in decimal) can be exactly
represented in a decimal type, but not in a floating-point type, as we saw in
Section 6.2.1.2. The disadvantages of decimal types are that the range of val-
ues is restricted because no exponents are allowed, and their representation in
memory is mildly wasteful, for reasons discussed in the following paragraph.
Decimal types are stored very much like character strings, using binary
codes for the decimal digits. These representations are called binary coded
decimal (BCD). In some cases, they are stored one digit per byte, but in others,
they are packed two digits per byte. Either way, they take more storage than
binary representations. It takes at least four bits to code a decimal digit. There-
fore, to store a six-digit coded decimal number requires 24 bits of memory.

Figure 6.1


IEEE floating-point
formats: (a) single
precision, (b) double
precision


Exponent Fraction

8 bits

(a)

(b)

Sign bit

23 bits

11 bits 52 bits

Sign bit

Exponent Fraction
Free download pdf