Rational data type

From English Wikipedia @ Freddythechick

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 <syntaxhighlight lang="text" class="" style="" inline="1">ratio</syntaxhighlight> 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

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:

  • C++ has included support for compile-time rational arithmetic in the form of the contents of its standard library's <syntaxhighlight lang="text" class="" style="" inline="1"><ratio></syntaxhighlight> header since its 2011 revision.
  • Clojure can perform arithmetic on rational numbers and offers a literal form to represent them.
  • Go provides rational numbers in the standard library, in the <syntaxhighlight lang="text" class="" style="" inline="1">math/big</syntaxhighlight> package.
  • J provides rational numbers in the base language. For example, <syntaxhighlight lang="text" class="" style="" inline="1">1r3</syntaxhighlight> is one-third. Rationals in J use arbitrary precision integers for both the numerator and denominator, allowing arbitrary precision non-integers. For instance, <syntaxhighlight lang="text" class="" style="" inline="1">12683021339465478347804472r7322545784478161858100577</syntaxhighlight> represents the square root of three to 50 decimal digits.[1]
  • Julia provides rational numbers with the rational operator, <syntaxhighlight lang="text" class="" style="" inline="1">//</syntaxhighlight>. For example, <syntaxhighlight lang="julia" inline>6//9 == 2//3 && typeof(-4//9) == Rational{Int64}</syntaxhighlight>.[2]
  • Haskell provides a <syntaxhighlight lang="text" class="" style="" inline="1">Rational</syntaxhighlight> type, which is really an alias for <syntaxhighlight lang="text" class="" style="" inline="1">Ratio Integer</syntaxhighlight> (<syntaxhighlight lang="text" class="" style="" inline="1">Ratio</syntaxhighlight> being a polymorphic type implementing rational numbers for any <syntaxhighlight lang="text" class="" style="" inline="1">Integral</syntaxhighlight> type of numerators and denominators). The fraction is constructed using the % operator.[3]
  • OCaml's Num library implements arbitrary-precision rational numbers.
  • Perl: <syntaxhighlight lang="text" class="" style="" inline="1">Math::BigRat</syntaxhighlight> core module implements arbitrary-precision rational numbers. The <syntaxhighlight lang="text" class="" style="" inline="1">bigrat</syntaxhighlight> pragma can be used to turn on transparent BigRat support.
  • Raku: use by default <syntaxhighlight lang="text" class="" style="" inline="1">Rat</syntaxhighlight>[4] type (rational numbers with limited-precision). <syntaxhighlight lang="text" class="" style="" inline="1">FatRat</syntaxhighlight>[5] data type implements arbitrary-precision rational numbers.
  • Python: The standard library includes a <syntaxhighlight lang="text" class="" style="" inline="1">Fraction</syntaxhighlight> class in the module <syntaxhighlight lang="text" class="" style="" inline="1">fractions</syntaxhighlight>.[6]
  • Ruby: native support using special syntax.
  • Smalltalk represents rational numbers using a <syntaxhighlight lang="text" class="" style="" inline="1">Fraction</syntaxhighlight> class in the form <syntaxhighlight lang="text" class="" style="" inline="1">p/q</syntaxhighlight> where <syntaxhighlight lang="text" class="" style="" inline="1">p</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">q</syntaxhighlight> are arbitrary size integers. Applying the arithmetic operations <syntaxhighlight lang="text" class="" style="" inline="1">*</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">+</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">-</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">/</syntaxhighlight>, to fractions returns a reduced fraction.

With external libraries:

Common Lisp

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

<syntaxhighlight lang="lisp">

1/3
⇒ 1/3

</syntaxhighlight>

The type of a rational number is <syntaxhighlight lang="text" class="" style="" inline="1">RATIO</syntaxhighlight>: <syntaxhighlight lang="lisp">

(type-of 1/3)
⇒ RATIO

</syntaxhighlight>

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

<syntaxhighlight lang="lisp">

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

</syntaxhighlight>

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

<syntaxhighlight lang="lisp">

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

</syntaxhighlight>

Computing with large integers returning a large rational number:

<syntaxhighlight lang="lisp">

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

</syntaxhighlight>

Clojure

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

Julia

<syntaxhighlight lang="jlcon"> julia> 1//10 + 2//10 3//10 </syntaxhighlight>

Haskell

In module Data.Ratio

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

Racket (PLT Scheme)

<syntaxhighlight lang="racket"> > (+ 1/10 2/10) 3/10 </syntaxhighlight>

Raku

Raku provides <syntaxhighlight lang="text" class="" style="" inline="1">Rat</syntaxhighlight> type by default.

<syntaxhighlight lang="pl6"> my $v = 0.2; say "{$v} is {$v.^name} and has numerator {$v.numerator} and denominator {$v.denominator}";

  1. ⇒ 0.2 is Rat and has numerator 1 and denominator 5

</syntaxhighlight>

<syntaxhighlight lang="pl6"> say 0.1 + 0.2

  1. ⇒ 0.3

</syntaxhighlight>

<syntaxhighlight lang="pl6"> say (0.1 + 0.2 - 0.3).fmt("%.17f")

  1. ⇒ 0.00000000000000000

</syntaxhighlight>

<syntaxhighlight lang="pl6"> say 1 / (0.1 + 0.2 - 0.3)

  1. ⇒ Attempt to divide by zero when coercing Rational to Str

</syntaxhighlight>

Ruby

Using special syntax in 2.1 or newer:

<syntaxhighlight lang="irb"> irb(main):001:0> puts 1/10r + 2/10r 3/10 => nil </syntaxhighlight>

References

  1. ^ "Vocabulary/NumericPrecisions - J Wiki".
  2. ^ "Complex and Rational Numbers — Julia Language development documentation". docs.julialang.org. Archived from the original on 2012-07-15.
  3. ^ "The Haskell 98 Library Report: Rational Numbers".
  4. ^ "Class Rat".
  5. ^ "Class FatRat".
  6. ^ "Fractions — Rational numbers — Python 3.10.0 documentation".
  7. ^ Common Lisp HyperSpec: RATIO
  8. ^ Function NUMERATOR, DENOMINATOR at the Common Lisp HyperSpec