Ruby: internal/rational.h Source File

Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)

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(int argc, VALUE *argv, VALUE num);
41 VALUE rb_float_numerator(VALUE x);
42 VALUE rb_float_denominator(VALUE x);
43
44static inline void RATIONAL_SET_NUM(VALUE r, VALUE n);
45static inline void RATIONAL_SET_DEN(VALUE r, VALUE d);
46
47RUBY_SYMBOL_EXPORT_BEGIN
48/* rational.c (export) */
49 VALUE rb_gcd(VALUE x, VALUE y);
50 VALUE rb_gcd_normal(VALUE self, VALUE other);
51#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
52 VALUE rb_gcd_gmp(VALUE x, VALUE y);
53#endif
54RUBY_SYMBOL_EXPORT_END
55
56static inline void
57RATIONAL_SET_NUM(VALUE r, VALUE n)
58{
59 assert(RB_INTEGER_TYPE_P(n));
60 RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n);
61}
62
63static inline void
64RATIONAL_SET_DEN(VALUE r, VALUE d)
65{
66 assert(RB_INTEGER_TYPE_P(d));
67 assert(INT_POSITIVE_P(d));
68 RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d);
69}
70
71#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:63
RRational
Internal header for Rational.
Definition rational.h:16
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40