Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Rational data type

From Wikipedia, the free encyclopedia

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.

Representation

[edit]

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.

Language support

[edit]

Built-in or core library:

  • C++ has included support for compile-time rational arithmetic in the form of the contents ofits standard library's<ratio> header sinceits 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 themath/big package.
  • J provides rational numbers in the base language. For example,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]
  • Julia provides rational numbers with the rational operator,//. For example,6//9==2//3&&typeof(-4//9)==Rational{Int64}.[2]
  • Haskell provides aRational 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]
  • OCaml's Num library implements arbitrary-precision rational numbers.
  • Perl:Math::BigRat core module implements arbitrary-precision rational numbers. Thebigrat pragma can be used to turn on transparent BigRat support.
  • Raku: use by defaultRat[4] type (rational numbers with limited-precision).FatRat[5] data type implements arbitrary-precision rational numbers.
  • Python: The standard library includes aFraction class in the modulefractions.[6]
  • Ruby: native support using special syntax.
  • Smalltalk represents rational numbers using aFraction class in the formp/q wherep andq are arbitrary size integers. Applying the arithmetic operations*,+,-,/, to fractions returns areduced fraction.

With external libraries:

Common Lisp

[edit]

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

1/31/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

Clojure

[edit]
(print (+ 1/10 2/10)) ⇒ 3/10

Julia

[edit]
julia>1//10+2//103//10

Haskell

[edit]

In module Data.Ratio

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

Racket (PLT Scheme)

[edit]
>(+1/102/10)3/10

Raku

[edit]

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

Ruby

[edit]

Using special syntax in 2.1 or newer:

irb(main):001:0>puts1/10r+2/10r3/10=> nil

References

[edit]
  1. ^"Vocabulary/NumericPrecisions - J Wiki".
  2. ^"Complex and Rational Numbers — Julia Language development documentation".docs.julialang.org. Archived fromthe 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 theCommon Lisp HyperSpec
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Rational_data_type&oldid=1251730976"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp