1.2. Numerics

Orc includes two numeric types, integers and numbers. Orc's integers are arbitrary-precision two's complement integers. Orc's numbers are floating point numbers, with an arbitrary-precision two's complement significand and a 32-bit two's complement exponent (base 10). These numbers behaves in accordance with the ANSI INCITS 274-1996 subset of ANSI/IEEE Std 754-2008. Namely, "infinite, NaN, or subnormal results are always treated as errors, and –0 results are hidden".

Note that the divide operation on Orc numbers can encounter a non-terminating decimal expansion, where there is no exact representable decimal result. In this case, the divide operation falls back to division using IEEE 754 binary64 (formerly called double precision) binary floating-point operands. This fall back may result in a loss of precision for this operation.

Similarly, the exponent operation may fall back to IEEE 754 binary64 binary floating-point operands in the case of a fractional exponent. This fall back may result in a loss of precision for this operation.

1.2.1. Literals

Syntax

[54]IntegerLiteral::= DecimalDigit+  
[55]NumberLiteral::= IntegerLiteral DecimalPart? ExponentPart?  
[56]DecimalPart::= . IntegerLiteral  
[57]ExponentPart::= E IntegerLiteral
E+ IntegerLiteral
E- IntegerLiteral
e IntegerLiteral
e+ IntegerLiteral
e- IntegerLiteral
 

Numeric literals in Orc are specified in decimal. Leading zeros are allowed, but have no significance. Trailing zeros after a decimal point also have no significance. If a numeric literal contains a decimal point or an "E", it is a number (floating point) literal, otherwise it is an integer literal.

1.2.2. Operations

Notable operations on integers and numbers include:

  • Add: +

  • Subtract: -

  • Negate (unary minus): -

  • Multiply: *

  • Divide: /

  • Exponent: **

  • Remainder: %

  • Absolute value: abs

  • Signum: signum

  • Floor: Floor

  • Ceiling: Ceil

Arithmetic operators with two integer arguments will perform an integer operation and return an integer result; for example, 5 / 2 performs integer division and returns 2. However, if either argument to an operator has a decimal part (even if it is trivial, as in 3.0), the other argument will be promoted, and a decimal operation will be performed. For example, 5 / 2.0 and 5.0 / 2 both perform decimal division and return 2.5.

1.2.3. Java calls

Orc integer values are passed in calls to and returns from Java code as java.math.BigInteger, or if the callee method expects a specific numeric type, an Orc integer will be converted to a java.lang.Byte, java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Float, or java.lang.Double, as appropriate. These values are boxed and unboxed per The Java Language Specification.

Orc number values are passed in calls to and returns from Java code as java.math.BigDecimal, or if the callee method expects a specific numeric type, an Orc number will be converted to a java.lang.Float or java.lang.Double, as appropriate. These values are boxed and unboxed per The Java Language Specification.

1.2.4. Type

All numeric values (integers and numbers) have type Number. Integer values also have the more specific type Integer, which is a subtype of Number.

1.2.5. Examples

Operator Precedence
{- Operators follow common precedence practice. 
   Combinators have lower precedence than operators.
-}

4 + 15 / 3 * 2 >result> result

{-
OUTPUT:
14
-}

See the Orc precedence table.

Integer to Number Promotion
{- Arithmetic operations, when all arguments are integers,
   operate as integer operations.  If any argument is a floating-
   point number, then the operation treats all arguments as
   floating-point numbers and operates in that domain.
-}

16 - 8 + 50.0 / 3 * 1 >a>
16 - Floor(8.5) + Ceil(4e1 + 9.99) / 3 * 1 >b>
(a, b) 

{-
OUTPUT:
(24.666666666666668, 24)
-}
Square Roots and Squares
{- Calculate the square roots and squares of
   a list of numbers
-}

val nums = [1, 2, 3, 4.0, 5.00]

each(nums) >x> ( 
  x**(1.0/2) >sqrt>
  x**2 >sq>
  (x,sqrt,sq)
)

{-
OUTPUT:PERMUTABLE
(1, 1.0, 1)
(2, 1.4142135623730951, 4)
(3, 1.7320508075688772, 9)
(4.0, 2.0, 16.00)
(5.00, 2.23606797749979, 25.0000)
-}

1.2.6. Related Links

Related Tutorial Sections