Rational data type

Some programming languages provide a built-in (primitive) rational data type to represent rational numbers like 1/3 and -11/17 without rounding, and to do arithmetic on them. Examples are the ratio type of Common Lisp, and analogous types provided by most languages for algebraic computation, such as Mathematica and Maple. Many languages that do not have a built-in rational type still provide it as a library-defined type.


A variable or value of that type is usually represented as a fraction m/n where m and n are two integer numbers, either with a fixed or arbitrary precision. Depending on the language, the denominator n may be constrained to be non-zero, and the two numbers may be kept in reduced form (without any common divisors except 1).

Languages that support a rational data type usually provide special syntax for building such values, and also extend the basic arithmetic operations ('+', '', '×', '/', integer powers) and comparisons ('=', '<', '>', '≤') to act on them — either natively or through operator overloading facilities provided by the language. These operations may be translated by the compiler into a sequence of integer machine instructions, or into library calls. Support may also extend to other operations, such as formatting, rounding to an integer or floating point value, etc.. As in mathematics, those languages often interpret an integer value as equivalent to a rational value with a unit denominator.

Language support

Built-in or core library:

With external libraries:

Common Lisp

Common Lisp provides a numeric data type for arbitrarily sized rational numbers: RATIO.[8]


The type of a rational number is RATIO:

 (type-of 1/3)

Dividing two integers may return a rational number and the multiplication of a rational number may return an integer number:

 (/ 6 8)
 (* 3/4 16)

The numerator and denominator may be obtained using the homonymous functions, that reduce a rational to canonical form and compute the numerator or denominator of that form respectively:[9]

 (numerator 12/16)
 (denominator 12/16)

Computing with large integers returning a large rational number:

 (/ (1- (expt 2 200)) (1- (expt 2 43)))


(print (+ 1/10 2/10))
 ⇒ 3/10


1//10 + 2//10
 ⇒ 3//10


(1 % 10) + (2 % 10)
 ⇒ 3 % 10

Perl 6

say 1/10+2/10

Racket (PLT Scheme)

(+ 1/10 2/10)
 ⇒ 3/10


Using special syntax in 2.1 or newer:

puts 1/10r + 2/10r


  1. http://www.jsoftware.com/jwiki/Vocabulary/NumericPrecisions
  2. http://docs.julialang.org/en/latest/manual/complex-and-rational-numbers/#rational-numbers
  3. http://www.haskell.org/onlinereport/ratio.html
  4. https://docs.perl6.org/type/Rat
  5. https://docs.perl6.org/type/Rat
  6. https://docs.python.org/2/library/fractions.html
  7. https://docs.python.org/3/library/fractions.html
  8. Common Lisp HyperSpec: RATIO
  9. Function NUMERATOR, DENOMINATOR at the Common Lisp HyperSpec
This article is issued from Wikipedia - version of the 11/28/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.