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.
Built-in or core library:
<ratio>
header since its 2011 revision.math/big
package.1r3
is one-third. Rationals in J use arbitrary 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 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. [3] Math::BigRat
core module implements arbitrary-precision rational numbers. The bigrat
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 module fractions
. [6] Fraction
class in the form p/q
where p
and q
are arbitrary size integers. Applying the arithmetic operations *
, +
, -
, /
, to fractions returns a reduced 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 is RATIO
:
(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
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]
(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 provides Rat
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
Arithmetic is an elementary branch of mathematics that studies numerical operations like addition, subtraction, multiplication, and division. In a wider sense, it also includes exponentiation, extraction of roots, and taking logarithms. Arithmetic systems can be distinguished based on the type of number they operate on. Integer arithmetic restricts itself to calculations with positive and negative whole numbers. Rational number arithmetic involves operations on fractions that lie between integers. Real number arithmetic includes calculations with both rational and irrational numbers and covers the complete number line. Another distinction is based on the numeral system employed to perform calculations. Decimal arithmetic is the most common. It uses the basic numerals from 0 to 9 and their combinations to express numbers. Binary arithmetic, by contrast, is used by most computers and represents numbers as combinations of the basic numerals 0 and 1. Some arithmetic systems operate on mathematical objects other than numbers, such as interval arithmetic and matrix arithmetic.
The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers of the Hindu–Arabic numeral system. The way of denoting numbers in the decimal system is often referred to as decimal notation.
An irreducible fraction is a fraction in which the numerator and denominator are integers that have no other common divisors than 1. In other words, a fraction a/b is irreducible if and only if a and b are coprime, that is, if a and b have a greatest common divisor of 1. In higher mathematics, "irreducible fraction" may also refer to rational fractions such that the numerator and the denominator are coprime polynomials. Every positive rational number can be represented as an irreducible fraction in exactly one way.
In mathematics, a continued fraction is an expression obtained through an iterative process of representing a number as the sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on. In a finite continued fraction, the iteration/recursion is terminated after finitely many steps by using an integer in lieu of another continued fraction. In contrast, an infinite continued fraction is an infinite expression. In either case, all integers in the sequence, other than the first, must be positive. The integers are called the coefficients or terms of the continued fraction.
A computer number format is the internal representation of numeric values in digital device hardware and software, such as in programmable computers and calculators. Numerical values are stored as groupings of bits, such as bytes and words. The encoding between numerical values and bit patterns is chosen for convenience of the operation of the computer; the encoding used by the computer's instruction set generally requires conversion for external use, such as for printing and display. Different types of processors may have different internal representations of numerical values and different conventions are used for integer and real numbers. Most calculations are carried out with number formats that fit into a processor register, but some software systems allow representation of arbitrarily large numbers using multiple words of memory.
Division is one of the four basic operations of arithmetic. The other operations are addition, subtraction, and multiplication. What is being divided is called the dividend, which is divided by the divisor, and the result is called the quotient.
Double-precision floating-point format is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.
Rounding means replacing a number with an approximate value that has a shorter, simpler, or more explicit representation. For example, replacing $23.4476 with $23.45, the fraction 312/937 with 1/3, or the expression √2 with 1.414.
In mathematics, division by zero, division where the divisor (denominator) is zero, is a unique and problematic special case. Using fraction notation, the general example can be written as , where is the dividend (numerator).
In arithmetic, a quotient is a quantity produced by the division of two numbers. The quotient has widespread use throughout mathematics. It has two definitions: either the integer part of a division or a fraction or ratio. For example, when dividing 20 by 3, the quotient is 6 in the first sense and in the second sense.
In computing, fixed-point is a method of representing fractional (non-integer) numbers by storing a fixed number of digits of their fractional part. Dollar amounts, for example, are often stored with exactly two fractional digits, representing the cents. More generally, the term may refer to representing fractional values as integer multiples of some fixed small unit, e.g. a fractional amount of hours as an integer multiple of ten-minute intervals. Fixed-point number representation is often contrasted to the more complicated and computationally demanding floating-point representation.
In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system. This contrasts with the faster fixed-precision arithmetic found in most arithmetic logic unit (ALU) hardware, which typically offers between 8 and 64 bits of precision.
In Scheme and in Lisp dialects inspired by it, the numerical tower is a set of data types that represent numbers and a logic for their hierarchical organisation.
A fraction represents a part of a whole or, more generally, any number of equal parts. When spoken in everyday English, a fraction describes how many parts of a certain size there are, for example, one-half, eight-fifths, three-quarters. A common, vulgar, or simple fraction consists of an integer numerator, displayed above a line, and a non-zero integer denominator, displayed below that line. If these integers are positive, then the numerator represents a number of equal parts, and the denominator indicates how many of those parts make up a unit or a whole. For example, in the fraction 3/4, the numerator 3 indicates that the fraction represents 3 equal parts, and the denominator 4 indicates that 4 parts make up a whole. The picture to the right illustrates 3/4 of a cake.
Arithmetic is an elementary branch of mathematics that is widely used for tasks ranging from simple day-to-day counting to advanced science and business calculations.
Class Library for Numbers (CLN) is a free library for arbitrary precision arithmetic. It operates on signed integers, rational numbers, floating point numbers, complex numbers, modular numbers, and univariate polynomials. Its implementation programming language is C++.
In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator p and a non-zero denominator q. For example, is a rational number, as is every integer. The set of all rational numbers, also referred to as "the rationals", the field of rationals or the field of rational numbers is usually denoted by boldface Q, or blackboard bold
Some programming languages provide a complex data type for complex number storage and arithmetic as a built-in (primitive) data type.
Some programming languages provide a built-in (primitive) or library decimal data type to represent non-repeating decimal fractions like 0.3 and -1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal
type of Python, and analogous types provided by other languages.