# Number literal

Learn how to represent integers, decimals, floats, and ordinal numbers.

CQL supports both whole and real numbers, which include integers and numbers with a fractional part. In most cases, you do not need to worry about selecting the appropriate type, as CQL automatically chooses the most suitable one.

> **Question: Why doesn't CQL use floats by default?**
>
> Unlike other languages, CQL does not adopt floats as its default numeric type. Instead, it uses integers or decimals, depending on whether the number has a fractional part. This is because floats are approximations and may not always yield the expected results.
>
> For example, let's say you add 0.1 and 0.2. You might expect the result to be 0.3, but with floating-point numbers, it's actually 0.30000000000000004. This is because floats are represented in binary, and 0.1 cannot be represented accurately as a floating-point number.
>
> > **Wait, what?**
> >
> > Yes, we know it's weird. But sometimes, computers favor speed over precision. If you want to learn more about this, check out [this page](https://0.30000000000000004.com/).
>
> To mitigate this problem, CQL uses decimals for fractional numbers by default. This ensures that your results in CQL match what you learned in math class. And if you need to use floats, you can still do so by adding the `f` suffix to the number.

## Integers

Integers are whole numbers, either positive or negative, that have no fractional part. To express an integer literal, simply write the number without a decimal point:

```cql
42
```

For those familiar with the technical terminology, the underlying type is a 64-bit signed integer. This means that the highest value allowed is 9223372036854775807, while the lowest is -9223372036854775808. Specifying a value outside this range will result in an error.

## Real numbers

Real numbers consist of integers and numbers with a fractional part. These are commonly used to represent continuous values, such as lengths, weights, and other quantities.

CQL supports two types of real numbers: decimals, which have maximum precision, and floats, which are approximate.

Both decimal and float literals support exponential notation. This allows you to represent very large or very small numbers in a more compact way.

> **Question: How to write numbers in exponential notation?**
>
> In exponential notation, you write a real number as the product of two numbers: a coefficient and an exponent. The `e` or `E` indicate that the number is multiplied by 10 raised to the power of the exponent.
>
> For example, the number `3.0e2` represents `3.0 * (10 ** 2)`, which is equal to `300.0`, and `1.5e-3` represents `1.5 * (10 ** -3)`, which is equal to 0.0015.

Let's take a look at each of these types in more detail.

### Decimals

Decimal is the type used to represent fractional numbers in CQL. Because they are precise, they avoid many pitfalls of [floating-point](#floats) arithmetic.

Unlike integers, decimal numbers do not have a maximum or minimum value. However, they do have a maximum precision of 28 digits. Precision refers to the number of significant digits in the number, excluding any leading zeros.

For example, the number 123.456 has 6 significant digits, 0.02001 has 4, and 0.000001 has only 1.

> **Precision loss**
>
> Decimals with more than 28 significant digits are rounded to the nearest representable value, which may result in a loss of precision.

To write a decimal literal, simply write the number with a decimal point, followed by the digits of the fractional part:

```cql
3.14
```

You can also represent a whole number as a decimal by adding a `.0` to the end:

```cql
42.0
```

And you can use exponential notation to represent very large numbers:

```cql
3.0e2 // 300.0
```

Or very small numbers:

```cql
1.5e-3 // 0.0015
```

For fractions, you can skip the whole part and start with the decimal:

```cql
.5 // This is equivalent to 0.5
```

Keep in mind that although decimals are suitable for most use cases, they might not be the best choice for representing approximate values such as measurements, scores, or ratings. For these cases, consider using floats instead.

### Floats

Floats are commonly used to represent continuous values that do not need to be precise. They are approximate real numbers that trade accuracy for the speed of computational operations.

Use floats when you need to represent approximate values, such as geo-coordinates, ratios, or probabilities.

To specify a float literal, add the `f` suffix to the number:

```cql
3.14f
```

You can also use exponential notation to represent very large numbers:

```cql
3.0e2f // 300.0f
```

Or very small numbers:

```cql
1.5e-3f // 0.0015f
```

For fractions, you can skip the whole part and start with the decimal:

```cql
.5f // This is equivalent to 0.5f
```

The underlying float type is a 64-bit floating-point number, equivalent to the double type in other languages. It can only accurately represent integers in the range -9007199254740991 to 9007199254740991 and a subset of decimal numbers.

As mentioned earlier, floats are approximate and may not always yield the expected results. If you need to perform precise calculations, consider using [decimals](#decimals) instead.

## Ordinals

Ordinals are a convenient and more natural alternative to using numerical indices, which are zero-based and sometimes confusing.

In English, you add the suffixes `st`, `nd`, `rd`, and `th` to indicate position, as in 1st, 2nd, 3rd, and 4th. This is exactly how you use ordinals in CQL.

> **Can I use ordinals everywhere?**
>
> You can use ordinals only where explicitly indicated in the documentation. Attempting to use them in other situations will result in a syntax error.

One of the primary use cases of ordinals in CQL is when accessing items from collections. For example, the following expression returns the first item the user added to their cart:

```cql
cart.items[1st]
```

In some instances, you can also express ordinal numbers using words such as *first*, *second*, *third*, through *twelfth* to indicate a position. Yet, not all places where you can use ordinals support the word form, so check the relevant documentation for specific cases.

For a practical use case of ordinals, see the [Ordinal selector](/reference/cql/expressions/accessors/bracket/selectors/ordinal) documentation.
