/src/ruby/internal/rational.h
Line | Count | Source |
1 | | #ifndef INTERNAL_RATIONAL_H /*-*-C-*-vi:se ft=c:*/ |
2 | | #define INTERNAL_RATIONAL_H |
3 | | /** |
4 | | * @author Ruby developers <ruby-core@ruby-lang.org> |
5 | | * @copyright This file is a part of the programming language Ruby. |
6 | | * Permission is hereby granted, to either redistribute and/or |
7 | | * modify this file, provided that the conditions mentioned in the |
8 | | * file COPYING are met. Consult the file for details. |
9 | | * @brief Internal header for Rational. |
10 | | */ |
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 | 0 | #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_rational_fdiv(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(VALUE num, int ndigits, enum ruby_num_rounding_mode mode); |
42 | | VALUE rb_flo_ceil_by_rational(VALUE num, int ndigits); |
43 | | VALUE rb_flo_floor_by_rational(VALUE num, int ndigits); |
44 | | VALUE rb_float_numerator(VALUE x); |
45 | | VALUE rb_float_denominator(VALUE x); |
46 | | |
47 | | static inline void RATIONAL_SET_NUM(VALUE r, VALUE n); |
48 | | static inline void RATIONAL_SET_DEN(VALUE r, VALUE d); |
49 | | |
50 | | RUBY_SYMBOL_EXPORT_BEGIN |
51 | | /* rational.c (export) */ |
52 | | VALUE rb_gcd(VALUE x, VALUE y); |
53 | | VALUE rb_gcd_normal(VALUE self, VALUE other); |
54 | | #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H) |
55 | | VALUE rb_gcd_gmp(VALUE x, VALUE y); |
56 | | #endif |
57 | | RUBY_SYMBOL_EXPORT_END |
58 | | |
59 | | static inline void |
60 | | RATIONAL_SET_NUM(VALUE r, VALUE n) |
61 | 0 | { |
62 | 0 | assert(RB_INTEGER_TYPE_P(n)); |
63 | 0 | RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n); |
64 | 0 | } Unexecuted instantiation: array.c:RATIONAL_SET_NUM Unexecuted instantiation: complex.c:RATIONAL_SET_NUM Unexecuted instantiation: enum.c:RATIONAL_SET_NUM Unexecuted instantiation: enumerator.c:RATIONAL_SET_NUM Unexecuted instantiation: gc.c:RATIONAL_SET_NUM Unexecuted instantiation: iseq.c:RATIONAL_SET_NUM Unexecuted instantiation: load.c:RATIONAL_SET_NUM Unexecuted instantiation: node_dump.c:RATIONAL_SET_NUM Unexecuted instantiation: numeric.c:RATIONAL_SET_NUM Unexecuted instantiation: ractor.c:RATIONAL_SET_NUM Unexecuted instantiation: rational.c:RATIONAL_SET_NUM Unexecuted instantiation: ruby.c:RATIONAL_SET_NUM Unexecuted instantiation: ruby_parser.c:RATIONAL_SET_NUM Unexecuted instantiation: time.c:RATIONAL_SET_NUM Unexecuted instantiation: vm.c:RATIONAL_SET_NUM Unexecuted instantiation: ast.c:RATIONAL_SET_NUM Unexecuted instantiation: compile.c:RATIONAL_SET_NUM Unexecuted instantiation: parse.c:RATIONAL_SET_NUM |
65 | | |
66 | | static inline void |
67 | | RATIONAL_SET_DEN(VALUE r, VALUE d) |
68 | 0 | { |
69 | 0 | assert(RB_INTEGER_TYPE_P(d)); |
70 | 0 | assert(INT_POSITIVE_P(d)); |
71 | 0 | RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d); |
72 | 0 | } Unexecuted instantiation: array.c:RATIONAL_SET_DEN Unexecuted instantiation: complex.c:RATIONAL_SET_DEN Unexecuted instantiation: enum.c:RATIONAL_SET_DEN Unexecuted instantiation: enumerator.c:RATIONAL_SET_DEN Unexecuted instantiation: gc.c:RATIONAL_SET_DEN Unexecuted instantiation: iseq.c:RATIONAL_SET_DEN Unexecuted instantiation: load.c:RATIONAL_SET_DEN Unexecuted instantiation: node_dump.c:RATIONAL_SET_DEN Unexecuted instantiation: numeric.c:RATIONAL_SET_DEN Unexecuted instantiation: ractor.c:RATIONAL_SET_DEN Unexecuted instantiation: rational.c:RATIONAL_SET_DEN Unexecuted instantiation: ruby.c:RATIONAL_SET_DEN Unexecuted instantiation: ruby_parser.c:RATIONAL_SET_DEN Unexecuted instantiation: time.c:RATIONAL_SET_DEN Unexecuted instantiation: vm.c:RATIONAL_SET_DEN Unexecuted instantiation: ast.c:RATIONAL_SET_DEN Unexecuted instantiation: compile.c:RATIONAL_SET_DEN Unexecuted instantiation: parse.c:RATIONAL_SET_DEN |
73 | | |
74 | | inline static bool |
75 | | f_zero_p(VALUE x) |
76 | 0 | { |
77 | 0 | if (RB_INTEGER_TYPE_P(x)) { |
78 | 0 | return FIXNUM_ZERO_P(x); |
79 | 0 | } |
80 | 0 | else if (RB_FLOAT_TYPE_P(x)) { |
81 | 0 | return FLOAT_ZERO_P(x); |
82 | 0 | } |
83 | 0 | else if (RB_TYPE_P(x, T_RATIONAL)) { |
84 | 0 | const VALUE num = RRATIONAL(x)->num; |
85 | 0 | return FIXNUM_ZERO_P(num); |
86 | 0 | } |
87 | 0 | return rb_equal(x, INT2FIX(0)) != 0; |
88 | 0 | } Unexecuted instantiation: array.c:f_zero_p Unexecuted instantiation: complex.c:f_zero_p Unexecuted instantiation: enum.c:f_zero_p Unexecuted instantiation: enumerator.c:f_zero_p Unexecuted instantiation: gc.c:f_zero_p Unexecuted instantiation: iseq.c:f_zero_p Unexecuted instantiation: load.c:f_zero_p Unexecuted instantiation: node_dump.c:f_zero_p Unexecuted instantiation: numeric.c:f_zero_p Unexecuted instantiation: ractor.c:f_zero_p Unexecuted instantiation: rational.c:f_zero_p Unexecuted instantiation: ruby.c:f_zero_p Unexecuted instantiation: ruby_parser.c:f_zero_p Unexecuted instantiation: time.c:f_zero_p Unexecuted instantiation: vm.c:f_zero_p Unexecuted instantiation: ast.c:f_zero_p Unexecuted instantiation: compile.c:f_zero_p Unexecuted instantiation: parse.c:f_zero_p |
89 | | |
90 | | #define f_nonzero_p(x) (!f_zero_p(x)) |
91 | | |
92 | | #endif /* INTERNAL_RATIONAL_H */ |