19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A numeral containing only digits (let n be the total number of digits) and a radix point (let m be the number of digits<br />

after the radix point) has type RadixPointNumeraln, m, 10, v where v is the value of the numeral, with the radix<br />

point deleted, interpreted in radix ten.<br />

A numeral containing only digits (let n be the total number of digits) and a radix point (let m be the number of digits<br />

after the radix point), then an underscore, then a radix indicator (let r be the radix) has the following type:<br />

RadixPointNumeralWithExplicitRadixn, m, r, v<br />

where v is the value of the n-digit numeral, with the radix point deleted, interpreted in radix r.<br />

Every numeral also has type Numeraln, m, r, v for appropriate values of n , m , r , and v .<br />

Numerals are not directly converted to any of the number types because, as in common mathematical usage, we expect<br />

them to be polymorphic. For example, consider the numeral 3.1415926535897932384 ; converting it immediately to<br />

a floating-point number may lose precision. If that numeral is used in an expression involving floating-point intervals,<br />

it would be better to convert it directly to an interval. <strong>The</strong>refore, numerals have their own types as described above.<br />

This approach allows library designers to decide how numerals should interact with other types of objects by defining<br />

coercion operations (see Section 17.1 for an explanation of coercion in <strong>Fortress</strong>). <strong>The</strong> <strong>Fortress</strong> standard libraries define<br />

coercions from numerals to integers (for simple numerals) and rational numbers (for compound numerals).<br />

In <strong>Fortress</strong>, dividing two integers using the / operator produces a rational number; this is true regardless of whether<br />

the integers are of type Z (or ZZ), Z64 (or ZZ64), N32 (or NN32), or whatever. Addition, subtraction, multiplication,<br />

and division of rationals are always exact; thus values such as 1/3 are represented exactly in <strong>Fortress</strong>.<br />

Numerals containing a radix point are actually rational literals; thus 3.125 has the rational value 3125/1000 . <strong>The</strong><br />

quotient of two integer literals is a constant expression (described in Section 13.27) whose value is rational. Similarly,<br />

a sequence of digits with a radix point followed by the symbol × and an integer literal raised to an integer literal,<br />

such as 6.0221415 × 10 23 is a constant expression whose value is rational. If such constants are mentioned as part<br />

of a floating-point computation, the compiler performs the rational arithmetic exactly and then converts the result to<br />

a floating-point value, thus incurring at most one floating-point rounding error. But in general rational computations<br />

may also be performed at run time, not just at compile time.<br />

A rational number can be thought of as a pair of integers p and q that have been reduced to “standard form in lowest<br />

terms”; that is, q > 0 and there is no nonzero integer k such that p k and q k are integers and ∣ ∣ p ∣<br />

k + q ∣<br />

k < |p| + |q|. <strong>The</strong><br />

type Q includes all such rational numbers.<br />

<strong>The</strong> type Q ∗ relaxes the requirement q > 0 to q ≥ 0 and includes two extra values, 1/0 and −1/0 (sometimes<br />

called “the infinite rational” and “the indefinite rational”). <strong>The</strong> advantage of Q ∗ is that it is closed under the rational<br />

operations +, −, ×, and /. If a value of type Q ∗ is assigned to a variable of type Q, a DivideByZeroException is<br />

thrown at run time if the value is 1/0 or −1/0 . <strong>The</strong> type Q # includes all of 1/0 , −1/0 , and 0/0 . In ASCII, Q ,<br />

Q ∗ , and Q # are written as QQ, QQ star, and QQ splat, respectively. See Section 38.1 for definitions of Q , Q ∗ ,<br />

and Q # .<br />

13.1.1 Pi<br />

<strong>The</strong> object named π (or pi) may be used to represent the ratio of the circumference of a circle to its diameter rather<br />

than a specific floating-point value or interval value. In <strong>Fortress</strong>, π has type RationalValueTimesPifalse, 1, 1 .<br />

When used in a floating-point computation, it becomes a floating-point value of the appropriate precision; when used<br />

in an interval computation, it becomes an interval of the appropriate precision.<br />

13.1.2 Infinity and Zero<br />

<strong>The</strong> object named ∞ has type ExtendedIntegerValuetrue, 0,true . One can negate ∞ to get a negative infinity.<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!