Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Representation  





2 Language support  



2.1  Common Lisp  





2.2  Clojure  





2.3  Julia  





2.4  Haskell  





2.5  Racket (PLT Scheme)  





2.6  Raku  





2.7  Ruby  







3 References  














Rational data type






Русский
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


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.

Representation[edit]

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[edit]

Built-in or core library:

With external libraries:

Common Lisp[edit]

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

 (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

Clojure[edit]

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

Julia[edit]

julia> 1//10 + 2//10
3//10

Haskell[edit]

In module Data.Ratio

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

Racket (PLT Scheme)[edit]

> (+ 1/10 2/10)
3/10

Raku[edit]

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

Ruby[edit]

Using special syntax in 2.1 or newer:

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

References[edit]

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

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Rational_data_type&oldid=1194044340"

    Category: 
    Data types
    Hidden category: 
    Articles with example Julia code
     



    This page was last edited on 6 January 2024, at 22:58 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki