Next: Enumerations Up: Values, Types, and Constants Previous: Booleans Contents Index
Subsections

- Numeric Constants
- Mixing Numeric Types
- Arithmetic Operators
- Comparison Operators

# Numeric Types

`int`, `count`, and `double` types should be familiar to most programmers as integer, unsigned integer, and double-precision floating-point types.

These types are referred to collectively as numeric. Numeric types can be used in arithmetic operations (see § 3.3.3 below) as well as in comparisons (§ ).

##
Numeric Constants

`count` constants are just strings of digits: `1234` and `0` are examples.

`integer` constants are strings of digits preceded by a `+` or `-` sign: `-42` and `+5` for example. Because digit strings without a sign are of type `count`, occasionally you need to take care when defining a variable if it really needs to be of type `int` rather than `count`. Because of type inferencing (§ ), a definition like:

        local size_difference = 0;

will result in `size_difference` having type `count` when `int` is what's instead needed (because, say, the size difference can be negative). This can be resolved either by using an `int` constant in the initialization:

        local size_difference = +0;

or explicitly indicating the type:

        local size_difference: int = 0;

You write floating-point constants in the usual ways, a string of digits with perhaps a decimal point and perhaps a scale-factor written in scientific notation. Optional `+` or `-` signs may be given before the digits or before the scientific notation exponent. Examples are `-1234.`, `-1234e0`, `3.14159`, and `.003e-23`. All floating-point constants are of type `double`.

##
Mixing Numeric Types

You can freely intermix numeric types in expressions. When intermixed, values are promoted to the ``highest" type in the expression. In general, this promotion follows a simple hierarchy: `double` is highest, `int` comes next, and `count` is lowest. (Note that `bool` is not a numeric type.)

##
Arithmetic Operators

For doing arithmetic, Bro supports `+`, `-`, `*`, `/`, and `%`#4768#>In general, binary operators evaluate their operands after converting them to the higher type of the two and return a result of that type. However, subtraction of two `count` values yields an `int` value. Division is integral if its operands are `count` and/or `int`.

`+` and `-` can also be used as unary operators. If applied to a `count` type, they yield an `int` type.

`%` computes a modulus, defined in the same way as in the C language. It can only be applied to `count` or `int` types, and yields `count` if both operands are `count` types, otherwise `int`.

Binary `+` and `-` have the lowest precedence, `*`, `/`, and `%` have equal and next highest precedence. The unary `+` and `-` operators have the same precedence as the `!` operator (§ ). See § , for a table of the precedence of all Bro operators.

All arithmetic operators associate from left-to-right.

##
Comparison Operators

Bro provides the usual comparison operators: `==`, `!=`, `<`, `<=`, `>`, and `>=`. They each take two operands, which they convert to the higher of the two types (see § ). They return a `bool` corresponding to the comparison of the operands. For example,

        3 < 3.000001

yields true.

The comparison operators are all non-associative and have equal precedence, just below that of the `in` operator and just above that of the logical `&&` operator. See § , for a general discussion of precedence.

Next: Enumerations Up: Values, Types, and Constants Previous: Booleans Contents Index