txt: ## rational.md
  role Rational
  
  Number stored as numerator and denominator
  
  ```raku
  role Rational[::NuT, ::DeT] does Real { ... }
  ```
  
  `Rational` is the common role for numbers that are stored as pairs of numerator and denominator. It is parameterized by the types of the numerator (`NuT`) and denominator (`DeT`). By default, these are [`Int`](/type/Int), but other types of `Rational` are possible by using a different parameterization. In addition, `Rational` objects are immutable throughout their life.
  
  ```raku
  class Positive does Rational[UInt] {};
  my Positive $one-third = Positive.new(1,3);
  say $one-third;                         # OUTPUT: «0.333333␤»
  my Positive $fail =Positive.new(-2,3);  # OUTPUT: «Type check failed in binding to parameter 'nu'; expected UInt but got Int (-2)␤»
  ```
  
  Please note that, since `DeT` is by default equal to `NuT`, in this case both are instantiated to [`UInt`](/type/UInt). Built into Raku are [`Rat`](/type/Rat) and [`FatRat`](/type/FatRat), which both do the `Rational` role.
  
  # [Methods](#role_Rational "go to top of document")[§](#Methods "direct link")
  
  ## [method new](#role_Rational "go to top of document")[§](#method_new "direct link")
  
  ```raku
  method new(NuT:D $numerator, DeT:D $denominator --> Rational:D)
  
  ```
  
  Creates a new rational object from numerator and denominator, which it normalizes to the lowest terms. The `$denominator` can be zero, in which case the numerator is normalized to `-1`, `0`, or `1` depending on whether the original is negative, zero, or positive, respectively.
  
  ## [method Bool](#role_Rational "go to top of document")[§](#method_Bool "direct link")
  
  ```raku
  multi method Bool(Rational:D: --> Bool:D)
  ```
  
  Returns `False` if [numerator](/routine/numerator) is `0`, otherwise returns `True`. This applies for `<0/0>` zero-denominator <`Rational` as well, despite `?<0/0>.Num` being `True`.
  
  ## [method Bridge](#role_Rational "go to top of document")[§](#method_Bridge "direct link")
  
  ```raku
  method Bridge()
  ```
  
  Returns the number, converted to [`Num`](/type/Num).
  
  ## [method Int](#role_Rational "go to top of document")[§](#method_Int "direct link")
  
  ```raku
  method Int(Rational:D: --> Int:D)
  ```
  
  Coerces the invocant to [`Int`](/type/Int) by truncating non-whole portion of the represented number, if any. If the [denominator](/routine/denominator) is zero, will [fail](/routine/fail) with [`X::Numeric::DivideByZero`](/type/X/Numeric/DivideByZero).
  
  ## [method Num](#role_Rational "go to top of document")[§](#method_Num "direct link")
  
  ```raku
  method Num(Rational:D: --> Num:D)
  ```
  
  Coerces the invocant to [`Num`](/type/Num) by dividing [numerator](/routine/numerator) by [denominator](/routine/denominator). If [denominator](/routine/denominator) is `0`, returns `Inf`, `-Inf`, or `NaN`, based on whether [numerator](/routine/numerator) is a positive number, negative number, or `0`, respectively.
  
  ## [method ceiling](#role_Rational "go to top of document")[§](#method_ceiling "direct link")
  
  ```raku
  method ceiling(Rational:D: --> Int:D)
  ```
  
  Return the smallest integer not less than the invocant. If [denominator](/routine/denominator) is zero, [fails](/routine/fail) with [`X::Numeric::DivideByZero`](/type/X/Numeric/DivideByZero).
  
  ## [method floor](#role_Rational "go to top of document")[§](#method_floor "direct link")
  
  ```raku
  method floor(Rational:D: --> Int:D)
  ```
  
  Return the largest integer not greater than the invocant. If [denominator](/routine/denominator) is zero, [fails](/routine/fail) with [`X::Numeric::DivideByZero`](/type/X/Numeric/DivideByZero).
  
  ## [method isNaN](#role_Rational "go to top of document")[§](#method_isNaN "direct link")
  
  ```raku
  method isNaN(Rational:D: --> Bool:D)
  ```
  
  Tests whether the invocant's Num value is a NaN, an acronym for *Not available Number*. That is both its numerator and denominator are zero.
  
  ## [method numerator](#role_Rational "go to top of document")[§](#method_numerator "direct link")
  
  ```raku
  method numerator(Rational:D: --> NuT:D)
  
  ```
  
  Returns the numerator.
  
  ## [method denominator](#role_Rational "go to top of document")[§](#method_denominator "direct link")
  
  ```raku
  method denominator(Rational:D: --> DeT:D)
  
  ```
  
  Returns the denominator.
  
  ## [method nude](#role_Rational "go to top of document")[§](#method_nude "direct link")
  
  ```raku
  method nude(Rational:D: --> Positional)
  ```
  
  Returns a list of the numerator and denominator.
  
  ## [method norm](#role_Rational "go to top of document")[§](#method_norm "direct link")
  
  ```raku
  method norm(Rational:D: --> Rational:D)
  ```
  
  **DEPRECATED as of 6.d**. The method is no longer needed, because as of 6.d language version, it's required for `Rational` type to be normalized on creation.
  
  Returns a normalized Rational object, i.e. with positive denominator, and numerator and denominator coprime. The denominator can also by zero, but using it in any operation or a conversion to string will result in an exception.
  
  ```raku
  use v6.c;
  my Rational $by-zero = 3/0;
  say $by-zero.norm.raku; # OUTPUT: «<1/0>␤»
  
  ```
  
  ```raku
  say $by-zero; # OUTPUT: «Attempt to divide by zero when coercing Rational to Str␤
  
  ```
  
  ## [method base-repeating](#role_Rational "go to top of document")[§](#method_base-repeating "direct link")
  
  ```raku
  method base-repeating(Rational:D: Int:D() $base = 10)
  ```
  
  Returns a list of two strings that, when concatenated, represent the number in base `$base`. The second element is the one that repeats. For example:
  
  ```raku
  my ($non-rep, $repeating) = (19/3).base-repeating(10);
  say $non-rep;                               # OUTPUT: «6.␤»
  say $repeating;                             # OUTPUT: «3␤»
  printf '%s(%s)', $non-rep, $repeating;      # OUTPUT: «6.(3)»
  ```
  
  19/3 is 6.333333... with the 3 repeating indefinitely.
  
  If no repetition occurs, the second string is empty:
  
  ```raku
  say (5/2).base-repeating(10).raku;          # OUTPUT: «("2.5", "")␤»
  ```
  
  The precision for determining the repeating group is limited to 1000 characters, above that, the second string is `???`.
  
  `$base` defaults to `10`.
  
  ## [method Range](#role_Rational "go to top of document")[§](#method_Range "direct link")
  
  Returns a [Range object](/type/Range) that represents the range of values supported.
  
  # [Typegraph](# "go to top of document")[§](#typegraphrelations "direct link")
  
  Type relations for `Rational`
  
  
  
  raku-type-graph
  
  
  
  Rational
  Rational
  
  
  Real
  Real
  
  
  Rational->Real
  
  
  
  Numeric
  Numeric
  
  
  Real->Numeric
  
  
  
  Mu
  Mu
  
  
  Any
  Any
  
  
  Any->Mu
  
  
  
  Cool
  Cool
  
  
  Cool->Any
  
  
  
  Rat
  Rat
  
  
  Rat->Rational
  
  
  
  Rat->Cool
  
  
  
  FatRat
  FatRat
  
  
  FatRat->Rational
  
  
  
  FatRat->Cool
  
  
  
  Stringy
  Stringy
  
  
  Str
  Str
  
  
  Str->Cool
  
  
  
  Str->Stringy
  
  
  
  Allomorph
  Allomorph
  
  
  Allomorph->Str
  
  
  
  RatStr
  RatStr
  
  
  RatStr->Rat
  
  
  
  RatStr->Allomorph
  
  [Expand chart above](/assets/typegraphs/Rational.svg)
  