# Rational Numbers

This document registers a tag for serializing rational numbers in Concise Binary Object Representation (CBOR) (ref. 1).

- Tag: 30
- Data item: array
- Semantics: Rational number

##
Introduction

Rational numbers are numbers that can be expressed as a ratio of two integers -- a numerator, usually written as the top part of a fraction, and the denominator, the bottom part. The value of a rational number is the numerator divided by the denominator.

Rational numbers are a superset of binary and decimal numbers, and a subset of real numbers. This is because some rational numbers can't be expressed exactly as a decimal number (for example, 1/3), and some other real numbers, such as pi, are impossible to make into a rational number.

Some programming languages have built-in support for rational numbers or support for them is included in their standard libraries, such as Python and Ruby.

##
Detailed Semantics

A rational number in CBOR has the tag 30 and consists of an array with a length of 2. The first element is the numerator, and the second is the denominator. The numerator must be an unsigned integer (major type 0), a negative integer (major type 1), or a bignum (major type 6, tag 2 or 3). The denominator must be an unsigned integer (major type 0) or an unsigned bignum (major type 6, tag 2), and must not equal 0. The rational number need not be in lowest terms.

A CBOR decoder can treat data items with tag 30 that don't meet the criteria above as an error, but this specification doesn't define how a CBOR implementation ought to behave in this case. Section 3.4 of RFC 7049 (ref. 1) details this kind of error-handling behavior.

A rational number with a denominator of 1 can be serialized to an integer with the same value (for example, to major type 0 or a bignum), but this behavior is not required.

## Example

The following example shows how the rational number 1/3 is encoded.

d8 1e ---- Tag 30
82 ---- Array length 2
01 ---- 1
03 ---- 3

##
References

Ref. 1. Bormann, C. and Hoffman, P. "Concise Binary Object Representation (CBOR)". RFC 7049, October 2013.

##
Author

Peter Occil (poccil14 at gmail dot com)

My CBOR home page.

Any copyright to this specification is released to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/

##
Acknowledgments

Carsten Bormann reviewed this document and gave helpful suggestions.