Someprogramming languages provide a built-in (primitive)rationaldata type to representrational numbers like 1/3 and −11/17 withoutrounding, and to do arithmetic on them. Examples are theratio type ofCommon Lisp, and analogous types provided by most languages foralgebraic computation, such asMathematica andMaple. Many languages that do not have a built-in rational type still provide it as alibrary-defined type.
A variable or value of that type is usually represented as afractionm/n wherem andn are twointeger numbers, either with a fixed orarbitrary precision. Depending on the language, thedenominatornmay be constrained to be non-zero, and the two numbers may be kept inreduced form (without any commondivisors except 1).
Languages that support a rational data type usually provide special syntax for building such values, and also extend the basic arithmetic operations ('+', '−', '×', '/', integerpowers) and comparisons ('=', '<', '>', '≤') to act on them — either natively or throughoperator overloading facilities provided by the language. These operations may be translated by thecompiler into a sequence of integermachine instructions, or intolibrary calls. Support may also extend to other operations, such as formatting, rounding to an integer orfloating point value, etc.. As in mathematics, those languages often interpret an integer value as equivalent to a rational value with a unit denominator.
Built-in or core library:
<ratio> header sinceits 2011 revision.math/big package.1r3 is one-third. Rationals in J usearbitrary precision integers for both the numerator and denominator, allowing arbitrary precision non-integers. For instance,12683021339465478347804472r7322545784478161858100577 represents the square root of three to 50 decimal digits.[1]//. For example,6//9==2//3&&typeof(-4//9)==Rational{Int64}.[2]Rational type, which is really an alias forRatio Integer (Ratio being a polymorphic type implementing rational numbers for anyIntegral type of numerators and denominators). The fraction is constructed using the % operator.[3]Math::BigRat core module implements arbitrary-precision rational numbers. Thebigrat pragma can be used to turn on transparent BigRat support.Rat[4] type (rational numbers with limited-precision).FatRat[5] data type implements arbitrary-precision rational numbers.Fraction class in the modulefractions.[6]Fraction class in the formp/q wherep andq are arbitrary size integers. Applying the arithmetic operations*,+,-,/, to fractions returns areduced fraction.With external libraries:
Fraction class.Common Lisp provides a numeric data type for arbitrarily sized rational numbers:RATIO.[7]
1/3⇒1/3
The type of a rational number isRATIO:
(type-of1/3)⇒RATIO
Dividing two integers may return a rational number and the multiplication of a rational number may return an integer number:
(/68)⇒3/4(*3/416)⇒12
Thenumerator anddenominator 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]
(numerator12/16)⇒3(denominator12/16)⇒4
Computing with large integers returning a large rational number:
(/(1-(expt2200))(1-(expt243)))⇒1606938044258990275541962092341162602522202993782792835301375/8796093022207
(print (+ 1/10 2/10)) ⇒ 3/10
julia>1//10+2//103//10
In module Data.Ratio
(1 % 10) + (2 % 10) ⇒ 3 % 10
>(+1/102/10)3/10
Raku providesRat type by default.
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
say0.1 +0.2# ⇒ 0.3
say (0.1 +0.2 -0.3).fmt("%.17f")# ⇒ 0.00000000000000000
say1 / (0.1 +0.2 -0.3)# ⇒ Attempt to divide by zero when coercing Rational to Str
Using special syntax in 2.1 or newer:
irb(main):001:0>puts1/10r+2/10r3/10=> nil