encyclopegia.org

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 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.

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 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. 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. Julia provides rational numbers with the rational operator, . For example, 6//9 == 2//3 && typeof(-4//9) == Rational{Int64} . Haskell provides a type, which is really an alias for ( being a polymorphic type implementing rational numbers for any type of numerators and denominators). The fraction is constructed using the % operator. OCaml's Num library implements arbitrary-precision rational numbers. Perl: core module implements arbitrary-precision rational numbers. The pragma can be used to turn on transparent BigRat support. Raku: use by default type (rational numbers with limited-precision). data type implements arbitrary-precision rational numbers. Python: The standard library includes a class in the module . Ruby: native support using special syntax. Smalltalk represents rational numbers using a class in the form where and are arbitrary size integers. Applying the arithmetic operations , , , , to fractions returns a reduced fraction. With external libraries: For C & C++ there are projects like the GNU Multiple Precision Arithmetic Library. The Apache Commons Math library provides rational numbers for Java with its class. 1/3 ⇒ 1/3 The type of a rational number is : (type-of 1/3) ⇒ RATIO 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: (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 julia> 1//10 + 2//10 3//10 > (+ 1/10 2/10) 3/10 my $v = 0.2; say "{$v} is {$v.^name} and has numerator {$v.numerator} and denominator {$v.denominator}"; ⇒ 0.2 is Rat and has numerator 1 and denominator 5 say 0.1 + 0.2 ⇒ 0.3 say (0.1 + 0.2 - 0.3).fmt("%.17f") ⇒ 0.00000000000000000 say 1 / (0.1 + 0.2 - 0.3) ⇒ Attempt to divide by zero when coercing Rational to Str irb(main):001:0> puts 1/10r + 2/10r 3/10 => nil

References

No reference rows published for this article.

Cite this article

MLA

"Rational data type." encyclopegia.org, 2024, https://encyclopegia.org/wiki/rational-data-type. Accessed 24 November 2026.

Chicago

"Rational data type." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/rational-data-type.

APA

encyclopegia.org. (2024). Rational data type. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/rational-data-type

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

blood type

stub · references available

Type 69 tank

stub · references available

solar-type star

stub · references available

Adobe Type

stub · references available

Audi Type B

stub · references available

Audi Type G

stub · references available

digital data

stub · references available

rational root theorem

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1251730976 · 0.5 · snapshot enwiki-001251730976

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.