World Library  
Flag as Inappropriate
Email this Article

Rational data type

Article Id: WHEBN0022742285
Reproduction Date:

Title: Rational data type  
Author: World Heritage Encyclopedia
Language: English
Subject: Data types, Process Environment Block, Quotient type, Void type, Complex data type
Collection: Data Types
Publisher: World Heritage Encyclopedia

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.


  • Representation 1
  • Language support 2
    • Common Lisp 2.1
  • References 3


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

  • Julia provides rational numbers with the rational operator, //. For example, 6//9 == 2//3 && typeof(-4//9) == Rational{Int64}.[1]
  • Haskell provides a Rational type, which is really an alias for Ratio Integer (Ratio being a polymorphic type implementing rational numbers for any Integral type of numerators and denominators). The fraction is constructed using the % operator.[2]
  • OCaml's Num library implements arbitrary-precision rational numbers.
  • Perl's Math::BigRat core module implements arbitrary-precision rational numbers. The bigrat pragma can be used to turn on transparent BigRat support.
  • Starting with Python 2.6, Python's standard library includes a Fraction class in the module fractions.[3]
  • Ruby's standard library includes a Rational class in the module rational.
  • The Apache Commons Math library provides rational numbers for Java with its Fraction class.
  • Clojure can perform arithmetic on rational numbers and offers a literal form to represent them.
  • For C & C++ there are projects like the GNU Multiple Precision Arithmetic Library.

Common Lisp

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

 ⇒ 1/3

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
 (* 3/4 16)
 ⇒ 12

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:[5]

 (numerator 12/16)
 ⇒ 3
 (denominator 12/16)
 ⇒ 4

Computing with large integers returning a large rational number:

 (/ (1- (expt 2 200)) (1- (expt 2 43)))
 ⇒ 1606938044258990275541962092341162602522202993782792835301375/8796093022207


  1. ^
  2. ^
  3. ^
  4. ^ Common Lisp HyperSpec: RATIO
  5. ^ Function NUMERATOR, DENOMINATOR at the Common Lisp HyperSpec
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.