A rational data type is a method of representing rational numbers used in programming [1] . Rational numbers in a computer are presented in such a way as to store the numerator of the number separately, and the number denominator separately, which avoids the problem of rounding numbers when performing mathematical operations, as is the case when using variables stored in the form of a floating point or fixed point [2] . Mathematical operations on rational numbers are modeled by software [3] [4] . In some programming languages, to connect the ability to work with rational numbers, modules are used that implement work with rational numbers and which allow you to work with numbers that are presented in other forms [5] .
Content
Storage Example
For example, in computers it is necessary to work with a rational number:
To store it, a separate memory area is allocated for storing the numerator separately for the denominator . When working with a rational number, they separately work with the numerator, separately with the denominator, while the accuracy of the calculation is not lost, as happens when storing floating-point numbers [3] .
Python Rational Numbers Example
from fractions import Fraction # Enabling the Fraction function to use
n1 = Fraction ( 1 , 3 ) # Rational number 1/3
n2 = Fraction ( 2 , 3 ) # Rational number 2/3
n3 = 1/3 # Real number 1/3
r1 = n1 + n2 # Mathematical operation on the set of rational numbers
r2 = n1 + 2 # Mathematical operation on the set of rational and integer numbers
print ( "Answer:" , r1 )
>> Answer : 1
print ( "Answer:" , r2 )
>> Answer : 7/3
print ( "Answer:" , n3 )
>> Answer : 0.333333333333333333
See also
- Rational numbers
- Floating point number
- Fixed point number
- Real number
Notes
- ↑ Richter J. CLR via C #. Programming with Microsoft .NET Framework 2.0 in C #. Master Class. - 2nd ed., Revised .. - M., St. Petersburg: Russian edition, Peter, 2008. - P. 188-192. - 656 p.
- ↑ Side view: IEEE754 standard .
- ↑ 1 2 Opaleva E. , Samoilenko V. Programming languages and translation methods. - SPb. : BHV-Petersburg, 2005 .-- S. 52. - 480 p. - ISBN 5-94157-327-8 .
- ↑ Schmidsky Jacob. Mathematica 5. Tutorial. - M., St. Petersburg., Kiev: Williams, 2004 .-- S. 79 .-- 592 p. - ISBN 5-8459-0678-4 .
- ↑ Fractions - Rational numbers . Official site of the Phyton Software Foundation .
Literature
- Elvira Opaleva, Vladimir Samoilenko. Programming languages and translation methods. - SPb. : BHV-Petersburg, 2005 .-- 480 p. - ISBN 5-94157-327-8 .
Links
- Rational numbers - Phyton Rational numbers
- Rational - rational numbers in the LISP language
- Rational Numbers - Haskell Rational Numbers
- Rational Structure - Rational Numbers in the Microsoft.SolverFoundation.Common Microsoft FrameWork Namespace