• Nem Talált Eredményt

Real numbers

In document Advanced Programming Languages (Pldal 50-53)

3.2.4. Java and C#

3.4.1.3. Real numbers

Real numbers differ mostly from integers by containing a decimal point ( ). The general syntax of valid real numbers is the following:

['+'|'-'] <digit> {<digit>} '.' <digit> {<digit>} [<exponent>]

The exponent is started usually with the letter , followed by a possible premonitory sign, and the exponent

value. The represented value would be: . For example, the

form represents the -123456.0 real value.

To the above almost every language adds some specialty:

3.4.1.4. Pascal

In Pascal using an exponent always specifies a real number, so the 12E+3 form denotes the real value of 12000.0, even if it does not contain a decimal point [Catambay, 2001].

3.4.1.5. Ada

In Ada integers also have an exponential form, where the exponent must not be negative. So gives the integer value 1 000 000. In the exponential form for real numbers negative exponents can be used [Ada95, 1995].

3.4.1.6. Eiffel

In Eiffel, for real numbers one digit and a decimal point are enough, digits are not needed on both sides of the decimal point. (Example: .)

3.4.1.7. C++, Java

In C++ and Java real number literals have the type double by default (-12.3, 12.3e4) unless the letter f or F is appended to them at the end, which declares the value to be of type float (3.141592f, 2.9e-3f).

The double type can be emphasized by appending the letter d or D ([CPP98, 1998] and [Nyéky-Gaizler et al., 2008]). The NaN (Not a Number) value was also introduced. This is also the result of 0.0/0.0.

3.4.1.8. Java

In Java the zero value has a premonitory sign: 0.0, and -0.0. These values are equal, but in some cases they give different results, e.g.: 1.0/0.0 results positive infinity (POSITIVE_INFINITY), but 1.0/-0.0 gives negative infinity (NEGATIVE_INFINITY). In computations POSITIVE_INFINITY and NEGATIVE_INFINITY are used according to mathematics rules: adding or subtracting a finite number does not change their value, multiplying them together gives NEGATIVE_INFINITY. Their sum is not defined [Nyéky-Gaizler et al., 2008].

3.4.1.9. 2.4.1.2 Allowed bases

In most of the programming languages real numbers are given in decimal, integers in decimal, in hexadecimal, sometimes in octal and binary form. There are languages, where this is extended by the possibility to specify real numbers on different bases, or use other bases for any numbers. Allowed digits depend of course on the base used, so, for example, in the octal base only the digits .. , in hexadecimal the digits .. and the letters .. as "extended" digits can be used. Octal and hexadecimal formats are recommended for bit pattern manipulation.

3.4.1.10. Pascal

In Pascal, integers are normally in decimal format, but can be in hexadecimal format by prefixing the $ symbol before the digits. A hexadecimal integer can have at most digits. Examples: +124 and -5 are decimal, $1 $A00 and -$FFFF are hexadecimal integers. Real numbers can be given only in decimal base [Catambay, 2001].

3.4.1.11. Ada

In Ada, the base of the number system can be any number between and , and real numbers could be specified also on other bases. For other basis than decimal, the following syntax must be used:

<base>'#'<number>'#'[<exponent>]

The base number comes before the digits of the number, which sequence is opened and closed by a symbol.

The base and the exponent is always specified in decimal format [Nyéky-Gaizler et al., 1998].

Example:

2#1011# -- Binary integer.

16#F.FF#E2 -- Hexadecimal real in exponential format.

2#1.1111_1111_111#E11 -- Number and exponent have the same base, -- so the last two values are both @4095.0@.

3.4.1.12. C, C++

In C or C++ floating numbers can only be given in decimal format. The base of the integers is determined by the prefix of the number:

• 0 starts octal numbers, like: 0377,

• 0x starts the hexadecimals, like: 0xFF, 0xC0B0L,

• any other form is interpreted as decimal.

If the running environment implements in two's complement format on bits, the value given as 0xffff would be . However, if more bits are used to store integers, the same 0xffff literal evaluates to 65 535 [CPP98, 1998].

3.4.1.13. Java

In Java, numbers are specified like in C. It is strictly regulated, how octal and hexadecimal digits should be mapped onto and bits, the numeric value must be padded with -s from the left to or bits, and two's complement format must be applied [Microsystems, 2003b]. For example, , but in

Ada: 16#FFFFFFFF# 4_294_967_295

3.4.1.14. Mathematica

It is interesting to mention, that in Mathematica any base between and is allowed, and beside .. all letters of the English alphabet can be used as extended digits.

To convert the decimal integer to the base a ( ) BaseForm[n, a] can be used, and the expression

for the other way around. Example: [Szili and

Tóth, 1996]

3.4.2. 2.4.2 Characters and strings

For character constants usually (single quote) symbols are used - like -, some languages also allow to specify character codes directly. Certain languages like C, C++, Java, etc. define some standard named characters and introduce the "escape" notion to specify them. For further details see Table 5.

Strings (or character sequences) are text literals constructed from the allowed character set, usually surrounded by double or sometimes by single quotes. The allowed character sets are discussed at the beginning of this chapter, for the supported character and string types of different programming languages see Chapter 5 and 6.

3.5. 2.5 Comments

Program maintainability and transparency are greatly improved by commenting the source code. In these commentaries the author describes the role of each part, and the main implementation steps of complex algorithms. The compiler simply ignores normal comments, so the syntactical validity and the interpretation of the program will not be altered. Following comment methods are supported:

In document Advanced Programming Languages (Pldal 50-53)