Ruby: internal/rational.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

rational.h
1#ifndef INTERNAL_RATIONAL_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_RATIONAL_H
11#include "ruby/internal/config.h" /* for HAVE_LIBGMP */
12#include "ruby/ruby.h" /* for struct RBasic */
13#include "internal/numeric.h" /* for INT_POSITIVE_P */
14#include "ruby_assert.h" /* for assert */
15
16 struct RRational {
17 struct RBasic basic;
18 VALUE num;
19 VALUE den;
20};
21
22#define RRATIONAL(obj) ((struct RRational *)(obj))
23
24/* rational.c */
25 VALUE rb_rational_canonicalize(VALUE x);
26 VALUE rb_rational_uminus(VALUE self);
27 VALUE rb_rational_plus(VALUE self, VALUE other);
28 VALUE rb_rational_minus(VALUE self, VALUE other);
29 VALUE rb_rational_mul(VALUE self, VALUE other);
30 VALUE rb_rational_div(VALUE self, VALUE other);
31 VALUE rb_lcm(VALUE x, VALUE y);
32 VALUE rb_rational_reciprocal(VALUE x);
33 VALUE rb_cstr_to_rat(const char *, int);
34 VALUE rb_rational_hash(VALUE self);
35 VALUE rb_rational_abs(VALUE self);
36 VALUE rb_rational_cmp(VALUE self, VALUE other);
37 VALUE rb_rational_pow(VALUE self, VALUE other);
38 VALUE rb_rational_floor(VALUE self, int ndigits);
39 VALUE rb_numeric_quo(VALUE x, VALUE y);
40 VALUE rb_flo_round_by_rational(VALUE num, int ndigits, enum ruby_num_rounding_mode mode);
41 VALUE rb_flo_ceil_by_rational(VALUE num, int ndigits);
42 VALUE rb_flo_floor_by_rational(VALUE num, int ndigits);
43 VALUE rb_float_numerator(VALUE x);
44 VALUE rb_float_denominator(VALUE x);
45
46static inline void RATIONAL_SET_NUM(VALUE r, VALUE n);
47static inline void RATIONAL_SET_DEN(VALUE r, VALUE d);
48
49RUBY_SYMBOL_EXPORT_BEGIN
50/* rational.c (export) */
51 VALUE rb_gcd(VALUE x, VALUE y);
52 VALUE rb_gcd_normal(VALUE self, VALUE other);
53#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
54 VALUE rb_gcd_gmp(VALUE x, VALUE y);
55#endif
56RUBY_SYMBOL_EXPORT_END
57
58static inline void
59RATIONAL_SET_NUM(VALUE r, VALUE n)
60{
61 assert(RB_INTEGER_TYPE_P(n));
62 RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n);
63}
64
65static inline void
66RATIONAL_SET_DEN(VALUE r, VALUE d)
67{
68 assert(RB_INTEGER_TYPE_P(d));
69 assert(INT_POSITIVE_P(d));
70 RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d);
71}
72
73#endif /* INTERNAL_RATIONAL_H */
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
ruby.h
RBasic
Ruby object's base components.
Definition rbasic.h:69
RRational
Internal header for Rational.
Definition rational.h:16
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40