Ruby: internal/rational.h Source File

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

rational.h
Go to the documentation of this file.
1#ifndef INTERNAL_RATIONAL_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_RATIONAL_H
12#include "ruby/internal/config.h" /* for HAVE_LIBGMP */
13#include "ruby/ruby.h" /* for struct RBasic */
14#include "internal/gc.h" /* for RB_OBJ_WRITE */
15#include "internal/numeric.h" /* for INT_POSITIVE_P */
16#include "ruby_assert.h" /* for assert */
17
18 struct RRational {
19 struct RBasic basic;
20 VALUE num;
21 VALUE den;
22};
23
24#define RRATIONAL(obj) ((struct RRational *)(obj))
25
26/* rational.c */
27 VALUE rb_rational_canonicalize(VALUE x);
28 VALUE rb_rational_uminus(VALUE self);
29 VALUE rb_rational_plus(VALUE self, VALUE other);
30 VALUE rb_rational_minus(VALUE self, VALUE other);
31 VALUE rb_rational_mul(VALUE self, VALUE other);
32 VALUE rb_rational_div(VALUE self, VALUE other);
33 VALUE rb_lcm(VALUE x, VALUE y);
34 VALUE rb_rational_reciprocal(VALUE x);
35 VALUE rb_cstr_to_rat(const char *, int);
36 VALUE rb_rational_hash(VALUE self);
37 VALUE rb_rational_abs(VALUE self);
38 VALUE rb_rational_cmp(VALUE self, VALUE other);
39 VALUE rb_rational_pow(VALUE self, VALUE other);
40 VALUE rb_rational_floor(VALUE self, int ndigits);
41 VALUE rb_numeric_quo(VALUE x, VALUE y);
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 */
assert
#define assert(x)
Definition: dlmalloc.c:1176
num
big_t * num
Definition: enough.c:232
config.h
Thin wrapper to ruby/config.h.
numeric.h
Internal header for Numeric.
rb_rational_cmp
VALUE rb_rational_cmp(VALUE self, VALUE other)
Definition: rational.c:1071
rb_rational_uminus
VALUE rb_rational_uminus(VALUE self)
Definition: rational.c:607
rb_rational_pow
VALUE rb_rational_pow(VALUE self, VALUE other)
Definition: rational.c:976
rb_rational_plus
VALUE rb_rational_plus(VALUE self, VALUE other)
Definition: rational.c:720
rb_gcd_normal
VALUE rb_gcd_normal(VALUE self, VALUE other)
Definition: rational.c:362
rb_float_numerator
VALUE rb_float_numerator(VALUE x)
Definition: rational.c:2092
rb_rational_hash
VALUE rb_rational_hash(VALUE self)
Definition: rational.c:1748
rb_rational_floor
VALUE rb_rational_floor(VALUE self, int ndigits)
Definition: rational.c:1396
rb_float_denominator
VALUE rb_float_denominator(VALUE x)
Definition: rational.c:2112
rb_rational_reciprocal
VALUE rb_rational_reciprocal(VALUE x)
Definition: rational.c:1884
rb_rational_abs
VALUE rb_rational_abs(VALUE self)
Definition: rational.c:1236
rb_rational_div
VALUE rb_rational_div(VALUE self, VALUE other)
Definition: rational.c:899
rb_cstr_to_rat
VALUE rb_cstr_to_rat(const char *, int)
Definition: rational.c:2549
RRATIONAL
#define RRATIONAL(obj)
Definition: rational.h:24
rb_rational_minus
VALUE rb_rational_minus(VALUE self, VALUE other)
Definition: rational.c:761
rb_lcm
VALUE rb_lcm(VALUE x, VALUE y)
Definition: rational.c:1922
rb_gcd
VALUE rb_gcd(VALUE x, VALUE y)
Definition: rational.c:1903
rb_rational_canonicalize
VALUE rb_rational_canonicalize(VALUE x)
Definition: rational.c:2046
rb_rational_mul
VALUE rb_rational_mul(VALUE self, VALUE other)
Definition: rational.c:857
rb_numeric_quo
VALUE rb_numeric_quo(VALUE x, VALUE y)
Definition: rational.c:2031
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
ruby.h
ruby_assert.h
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
RBasic
Definition: rbasic.h:47
RRational
Definition: rational.h:18
RRational::num
VALUE num
Definition: rational.h:20
RRational::basic
struct RBasic basic
Definition: rational.h:19
RRational::den
VALUE den
Definition: rational.h:21
VALUE
unsigned long VALUE
Definition: value.h:38