Ruby: internal/rational.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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/gc.h" /* for RB_OBJ_WRITE */
14#include "internal/numeric.h" /* for INT_POSITIVE_P */
15#include "ruby_assert.h" /* for assert */
16
17 struct RRational {
18 struct RBasic basic;
19 VALUE num;
20 VALUE den;
21};
22
23#define RRATIONAL(obj) ((struct RRational *)(obj))
24
25/* rational.c */
26 VALUE rb_rational_canonicalize(VALUE x);
27 VALUE rb_rational_uminus(VALUE self);
28 VALUE rb_rational_plus(VALUE self, VALUE other);
29 VALUE rb_rational_minus(VALUE self, VALUE other);
30 VALUE rb_rational_mul(VALUE self, VALUE other);
31 VALUE rb_rational_div(VALUE self, VALUE other);
32 VALUE rb_lcm(VALUE x, VALUE y);
33 VALUE rb_rational_reciprocal(VALUE x);
34 VALUE rb_cstr_to_rat(const char *, int);
35 VALUE rb_rational_hash(VALUE self);
36 VALUE rb_rational_abs(VALUE self);
37 VALUE rb_rational_cmp(VALUE self, VALUE other);
38 VALUE rb_rational_pow(VALUE self, VALUE other);
39 VALUE rb_rational_floor(VALUE self, int ndigits);
40 VALUE rb_numeric_quo(VALUE x, VALUE y);
41 VALUE rb_flo_round_by_rational(int argc, VALUE *argv, VALUE num);
42 VALUE rb_float_numerator(VALUE x);
43 VALUE rb_float_denominator(VALUE x);
44
45static inline void RATIONAL_SET_NUM(VALUE r, VALUE n);
46static inline void RATIONAL_SET_DEN(VALUE r, VALUE d);
47
48RUBY_SYMBOL_EXPORT_BEGIN
49/* rational.c (export) */
50 VALUE rb_gcd(VALUE x, VALUE y);
51 VALUE rb_gcd_normal(VALUE self, VALUE other);
52#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
53 VALUE rb_gcd_gmp(VALUE x, VALUE y);
54#endif
55RUBY_SYMBOL_EXPORT_END
56
57static inline void
58RATIONAL_SET_NUM(VALUE r, VALUE n)
59{
60 assert(RB_INTEGER_TYPE_P(n));
61 RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n);
62}
63
64static inline void
65RATIONAL_SET_DEN(VALUE r, VALUE d)
66{
67 assert(RB_INTEGER_TYPE_P(d));
68 assert(INT_POSITIVE_P(d));
69 RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d);
70}
71
72#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 rgengc.h:220
ruby.h
RBasic
Ruby's object's, base components.
Definition rbasic.h:64
RRational
Internal header for Rational.
Definition rational.h:17
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40