Ruby: rational.c File Reference

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

Macros | Functions | Variables
rational.c File Reference
`#include "ruby/internal/config.h"`
`#include <ctype.h>`
`#include <float.h>`
`#include <math.h>`
`#include "id.h"`
`#include "internal.h"`
`#include "internal/array.h"`
`#include "internal/complex.h"`
`#include "internal/gc.h"`
`#include "internal/numeric.h"`
`#include "internal/object.h"`
`#include "internal/rational.h"`
`#include "ruby_assert.h"`

Go to the source code of this file.

## Macros
#define | NDEBUG
#define | ZERO INT2FIX(0)
#define | ONE INT2FIX(1)
#define | TWO INT2FIX(2)
#define | GMP_GCD_DIGITS 1
#define | INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
#define | id_idiv idDiv
#define | id_to_i idTo_i
#define | f_boolcast(x) ((x) ? Qtrue : Qfalse)
#define | f_inspect rb_inspect
#define | f_to_s rb_obj_as_string
#define | f_expt10(x) rb_int_pow(INT2FIX(10), x)
#define | f_nonzero_p(x) (!f_zero_p(x))
#define | k_exact_p(x) (!k_float_p(x))
#define | k_inexact_p(x) k_float_p(x)
#define | k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
#define | k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
#define | get_dat1(x) struct RRational *dat = RRATIONAL(x)
#define | get_dat2(x, y) struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
#define | nurat_expt rb_rational_pow
#define | id_ceil rb_intern("ceil")
#define | id_quo idQuo
#define | f_reciprocal(x) f_quo(ONE, (x))
#define | id_numerator rb_intern("numerator")
#define | f_numerator(x) rb_funcall((x), id_numerator, 0)
#define | id_denominator rb_intern("denominator")
#define | f_denominator(x) rb_funcall((x), id_denominator, 0)
#define | id_to_r idTo_r
#define | f_to_r(x) rb_funcall((x), id_to_r, 0)

## Functions
VALUE | rb_gcd_normal (VALUE x, VALUE y)
VALUE | rb_rational_uminus (VALUE self)
VALUE | rb_rational_plus (VALUE self, VALUE other)
VALUE | rb_rational_minus (VALUE self, VALUE other)
VALUE | rb_rational_mul (VALUE self, VALUE other)
VALUE | rb_rational_div (VALUE self, VALUE other)
VALUE | rb_rational_pow (VALUE self, VALUE other)
VALUE | rb_rational_cmp (VALUE self, VALUE other)
VALUE | rb_rational_abs (VALUE self)
VALUE | rb_rational_floor (VALUE self, int ndigits)
st_index_t | rb_rational_hash (VALUE self)
VALUE | rb_rational_reciprocal (VALUE x)
VALUE | rb_gcd (VALUE self, VALUE other)
VALUE | rb_lcm (VALUE self, VALUE other)
VALUE | rb_gcdlcm (VALUE self, VALUE other)
VALUE | rb_rational_raw (VALUE x, VALUE y)
VALUE | rb_rational_new (VALUE x, VALUE y)
VALUE | rb_Rational (VALUE x, VALUE y)
VALUE | rb_rational_num (VALUE rat)
VALUE | rb_rational_den (VALUE rat)
VALUE | rb_numeric_quo (VALUE x, VALUE y)
VALUE | rb_rational_canonicalize (VALUE x)
VALUE | rb_float_numerator (VALUE self)
VALUE | rb_float_denominator (VALUE self)
VALUE | rb_flt_rationalize_with_prec (VALUE flt, VALUE prec)
VALUE | rb_flt_rationalize (VALUE flt)
VALUE | rb_cstr_to_rat (const char *s, int strict)
void | Init_Rational (void)

## Variables
VALUE | rb_cRational

## Macro Definition Documentation

## ◆ f_boolcast

#define f_boolcast | ( |  | x | ) | ((x) ? Qtrue : Qfalse)

Definition at line 51 of file rational.c.

## ◆ f_denominator

#define f_denominator | ( |  | x | ) | rb_funcall((x), id_denominator, 0)

Definition at line 1992 of file rational.c.

## ◆ f_expt10

#define f_expt10 | ( |  | x | ) | rb_int_pow(INT2FIX(10), x)

Definition at line 166 of file rational.c.

## ◆ f_inspect

#define f_inspect rb_inspect

Definition at line 52 of file rational.c.

## ◆ f_nonzero_p

#define f_nonzero_p | ( |  | x | ) | (!f_zero_p(x))

Definition at line 182 of file rational.c.

## ◆ f_numerator

#define f_numerator | ( |  | x | ) | rb_funcall((x), id_numerator, 0)

Definition at line 1989 of file rational.c.

## ◆ f_reciprocal

#define f_reciprocal | ( |  | x | ) | f_quo(ONE, (x))

Definition at line 1611 of file rational.c.

## ◆ f_to_r

#define f_to_r | ( |  | x | ) | rb_funcall((x), id_to_r, 0)

Definition at line 1995 of file rational.c.

## ◆ f_to_s

#define f_to_s rb_obj_as_string

Definition at line 53 of file rational.c.

## ◆ get_dat1

#define get_dat1 | ( |  | x | ) | struct RRational *dat = RRATIONAL(x)

Definition at line 404 of file rational.c.

## ◆ get_dat2

#define get_dat2 | ( |  | x,
|  |  | y
| ) |  | struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)

Definition at line 407 of file rational.c.

## ◆ GMP_GCD_DIGITS

#define GMP_GCD_DIGITS 1

Definition at line 39 of file rational.c.

## ◆ id_ceil

#define id_ceil rb_intern("ceil")

Definition at line 1587 of file rational.c.

## ◆ id_denominator

#define id_denominator rb_intern("denominator")

Definition at line 1991 of file rational.c.

## ◆ id_idiv

#define id_idiv idDiv

Definition at line 48 of file rational.c.

## ◆ id_numerator

#define id_numerator rb_intern("numerator")

Definition at line 1988 of file rational.c.

## ◆ id_quo

#define id_quo idQuo

Definition at line 1599 of file rational.c.

## ◆ id_to_i

#define id_to_i idTo_i

Definition at line 49 of file rational.c.

## ◆ id_to_r

#define id_to_r idTo_r

Definition at line 1994 of file rational.c.

## ◆ INT_ZERO_P

#define INT_ZERO_P | ( |  | x | ) | (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))

Definition at line 41 of file rational.c.

## ◆ k_exact_one_p

#define k_exact_one_p | ( |  | x | ) | (k_exact_p(x) && f_one_p(x))

Definition at line 251 of file rational.c.

## ◆ k_exact_p

#define k_exact_p | ( |  | x | ) | (!k_float_p(x))

Definition at line 247 of file rational.c.

## ◆ k_exact_zero_p

#define k_exact_zero_p | ( |  | x | ) | (k_exact_p(x) && f_zero_p(x))

Definition at line 250 of file rational.c.

## ◆ k_inexact_p

#define k_inexact_p | ( |  | x | ) | k_float_p(x)

Definition at line 248 of file rational.c.

## ◆ NDEBUG

#define NDEBUG

Definition at line 24 of file rational.c.

## ◆ nurat_expt

#define nurat_expt rb_rational_pow

Definition at line 1051 of file rational.c.

## ◆ ONE

#define ONE INT2FIX(1)

Definition at line 36 of file rational.c.

## ◆ TWO

#define TWO INT2FIX(2)

Definition at line 37 of file rational.c.

## ◆ ZERO

#define ZERO INT2FIX(0)

Definition at line 35 of file rational.c.

## Function Documentation

## ◆ Init_Rational()

void Init_Rational | ( | void |  | ) |

Definition at line 2755 of file rational.c.

References CLASS_OF, nurat_expt, rb_cFloat, rb_cInteger, rb_cNilClass, rb_cNumeric, rb_cObject, rb_cRational, rb_cString, rb_define_alloc_func(), rb_define_class(), rb_define_class_under(), rb_define_global_function, rb_define_method, rb_define_private_method, rb_float_denominator(), rb_float_numerator(), rb_gcd(), rb_gcdlcm(), rb_lcm(), rb_marshal_define_compat(), rb_numeric_quo(), rb_provide(), rb_rational_abs(), rb_rational_cmp(), rb_rational_div(), rb_rational_minus(), rb_rational_mul(), rb_rational_plus(), rb_rational_uminus(), and rb_undef_method().

## ◆ rb_cstr_to_rat()

VALUE rb_cstr_to_rat | ( | const char * | s,
|  | int | strict
| ) |  |

Definition at line 2549 of file rational.c.

References num, rb_eFloatDomainError, rb_raise(), strlen(), and TRUE.

## ◆ rb_float_denominator()

VALUE rb_float_denominator | ( | VALUE | self | ) |

Definition at line 2112 of file rational.c.

References INT2FIX, isinf(), isnan, and RFLOAT_VALUE.

Referenced by Init_Rational().

## ◆ rb_float_numerator()

VALUE rb_float_numerator | ( | VALUE | self | ) |

Definition at line 2092 of file rational.c.

References isinf(), isnan, and RFLOAT_VALUE.

Referenced by Init_Rational().

## ◆ rb_flt_rationalize()

VALUE rb_flt_rationalize | ( | VALUE | flt | ) |

Definition at line 2247 of file rational.c.

References f, FLT_RADIX, INT2FIX, INT_ZERO_P, ONE, rb_int_lshift(), rb_int_minus(), rb_int_mul(), rb_int_plus(), rb_int_positive_pow(), rb_rational_new1, and rb_rational_new2.

## ◆ rb_flt_rationalize_with_prec()

VALUE rb_flt_rationalize_with_prec | ( | VALUE | flt,
|  | VALUE | prec
| ) |  |

Definition at line 2231 of file rational.c.

References f_abs, f_add, f_sub, and rb_rational_new2.

## ◆ rb_gcd()

VALUE rb_gcd | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 1903 of file rational.c.

Referenced by Init_Rational(), and rb_int_fdiv_double().

## ◆ rb_gcd_normal()

VALUE rb_gcd_normal | ( | VALUE | x,
|  | VALUE | y
| ) |  |

Definition at line 362 of file rational.c.

## ◆ rb_gcdlcm()

VALUE rb_gcdlcm | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 1941 of file rational.c.

References rb_assoc_new().

Referenced by Init_Rational().

## ◆ rb_lcm()

VALUE rb_lcm | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 1922 of file rational.c.

Referenced by Init_Rational().

## ◆ rb_numeric_quo()

VALUE rb_numeric_quo | ( | VALUE | x,
|  | VALUE | y
| ) |  |

Definition at line 2031 of file rational.c.

References rb_complex_div(), rb_convert_type(), rb_funcallv, rb_rational_div(), T_COMPLEX, and T_RATIONAL.

Referenced by fun2(), and Init_Rational().

## ◆ rb_Rational()

VALUE rb_Rational | ( | VALUE | x,
|  | VALUE | y
| ) |  |

Definition at line 1968 of file rational.c.

References rb_cRational.

## ◆ rb_rational_abs()

VALUE rb_rational_abs | ( | VALUE | self | ) |

Definition at line 1236 of file rational.c.

References CLASS_OF, get_dat1, num, and rb_int_abs().

Referenced by Init_Rational().

## ◆ rb_rational_canonicalize()

VALUE rb_rational_canonicalize | ( | VALUE | x | ) |

Definition at line 2046 of file rational.c.

References get_dat1, and T_RATIONAL.

## ◆ rb_rational_cmp()

VALUE rb_rational_cmp | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 1071 of file rational.c.

References CLASS_OF, FIX2LONG, FIXNUM_P, get_dat1, get_dat2, LONG2FIX, rb_dbl_cmp(), rb_int_cmp(), rb_int_minus(), rb_int_mul(), rb_intern(), rb_num_coerce_cmp(), RFLOAT_VALUE, T_BIGNUM, T_FIXNUM, T_FLOAT, T_RATIONAL, TYPE, and ZERO.

Referenced by Init_Rational().

## ◆ rb_rational_den()

VALUE rb_rational_den | ( | VALUE | rat | ) |

Definition at line 1983 of file rational.c.

Referenced by rb_str_format().

## ◆ rb_rational_div()

VALUE rb_rational_div | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 899 of file rational.c.

References CLASS_OF, get_dat1, get_dat2, ONE, rb_flo_div_flo(), RB_INTEGER_TYPE_P, rb_num_coerce_bin(), rb_num_zerodiv(), and T_RATIONAL.

Referenced by Init_Rational(), and rb_numeric_quo().

## ◆ rb_rational_floor()

VALUE rb_rational_floor | ( | VALUE | self,
|  | int | ndigits
| ) |  |

Definition at line 1396 of file rational.c.

References INT2NUM.

## ◆ rb_rational_hash()

st_index_t rb_rational_hash | ( | VALUE | self | ) |

Definition at line 1748 of file rational.c.

References get_dat1, NUM2LONG, rb_hash(), and rb_memhash().

## ◆ rb_rational_minus()

VALUE rb_rational_minus | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 761 of file rational.c.

References CLASS_OF, DBL2NUM, get_dat1, get_dat2, rb_int_minus(), rb_int_mul(), RB_INTEGER_TYPE_P, rb_num_coerce_bin(), RFLOAT_VALUE, and T_RATIONAL.

Referenced by Init_Rational().

## ◆ rb_rational_mul()

VALUE rb_rational_mul | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 857 of file rational.c.

References DBL2NUM, get_dat1, get_dat2, ONE, RB_INTEGER_TYPE_P, rb_num_coerce_bin(), RFLOAT_VALUE, and T_RATIONAL.

Referenced by Init_Rational().

## ◆ rb_rational_new()

VALUE rb_rational_new | ( | VALUE | x,
|  | VALUE | y
| ) |  |

Definition at line 1962 of file rational.c.

References rb_cRational.

Referenced by date_zone_to_diff().

## ◆ rb_rational_num()

VALUE rb_rational_num | ( | VALUE | rat | ) |

Definition at line 1977 of file rational.c.

Referenced by rb_str_format().

## ◆ rb_rational_plus()

VALUE rb_rational_plus | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 720 of file rational.c.

References CLASS_OF, DBL2NUM, get_dat1, get_dat2, rb_int_mul(), rb_int_plus(), RB_INTEGER_TYPE_P, rb_num_coerce_bin(), RFLOAT_VALUE, and T_RATIONAL.

Referenced by Init_Rational().

## ◆ rb_rational_pow()

VALUE rb_rational_pow | ( | VALUE | self,
|  | VALUE | other
| ) |  |

Definition at line 976 of file rational.c.

References CLASS_OF, DBL2NUM, FIXNUM_P, get_dat1, INT2FIX, INT_ZERO_P, k_exact_p, k_exact_zero_p, nan(), num, ONE, rb_float_pow(), rb_int_odd_p(), rb_int_pow(), rb_int_uminus(), RB_INTEGER_TYPE_P, rb_intern(), rb_num_coerce_bin(), rb_num_negative_p(), rb_num_zerodiv(), rb_warn(), T_BIGNUM, T_RATIONAL, and ZERO.

Referenced by rb_num_pow().

## ◆ rb_rational_raw()

VALUE rb_rational_raw | ( | VALUE | x,
|  | VALUE | y
| ) |  |

Definition at line 1948 of file rational.c.

References rb_cRational, rb_int_uminus(), RB_INTEGER_TYPE_P, and rb_to_int().

Referenced by rb_big_pow().

## ◆ rb_rational_reciprocal()

VALUE rb_rational_reciprocal | ( | VALUE | x | ) |

Definition at line 1884 of file rational.c.

References CLASS_OF, FALSE, and get_dat1.

## ◆ rb_rational_uminus()

VALUE rb_rational_uminus | ( | VALUE | self | ) |

Definition at line 607 of file rational.c.

References assert, CLASS_OF, get_dat1, rb_int_uminus(), and T_RATIONAL.

Referenced by Init_Rational().

## Variable Documentation

## ◆ rb_cRational

VALUE rb_cRational

Definition at line 43 of file rational.c.

Referenced by Init_Rational(), rb_num_to_dbl(), rb_Rational(), rb_rational_new(), and rb_rational_raw().