1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/internal/rational.h
nagachika 31816356ee merge revision(s) 2bc293e899c9d32dcd794a73de8925c49ecf8f15,d0e6c6e682b9ba2b0309a5177933a0628e8ef316,cc0dc67bbbe1951ff90004bc987f78545625d772,e1eff837cf12a8e813de9d4ff2db50c9b68b86b5,0ab0b86c8491d639b9ff1335ddf35e341ecd867e,6911b4bc82889860ff7da4ecf975504cdc3e5314: [Backport #17854]
cdhash_cmp: can take rational literals

	Rational literals are those integers suffixed with `r`.  They tend to
	be a part of more complex expressions like `123/456r`, but in theory
	they can live alone.  When such "bare" rational literals are passed to
	case-when branch, we have to take care of them.  Fixes [Bug #17854]
	---
	 common.mk                  |  1 +
	 compile.c                  |  7 +++++++
	 internal/rational.h        |  1 +
	 rational.c                 | 13 ++++++++++---
	 test/ruby/test_rational.rb |  7 +++++++
	 5 files changed, 26 insertions(+), 3 deletions(-)

	cdhash_cmp: rational literals with fractions

	Nobu kindly pointed out that rational literals can have fractions.
	---
	 compile.c                  | 5 +++--
	 test/ruby/test_rational.rb | 4 ++++
	 2 files changed, 7 insertions(+), 2 deletions(-)

	cdhash_cmp: can also take complex

	There are complex literals `123i`, which can also be a case condition.
	---
	 compile.c                  | 15 +++++++++++----
	 complex.c                  | 12 +++++++++---
	 internal/complex.h         |  1 +
	 test/ruby/test_rational.rb |  4 ++++
	 4 files changed, 25 insertions(+), 7 deletions(-)

	cdhash_cmp: recursively apply

	For instance a rational's numerator can be a bignum.  Comparison using
	C's == can be insufficient.
	---
	 compile.c | 4 ++--
	 1 file changed, 2 insertions(+), 2 deletions(-)

	cdhash_cmp: should use ||

	cf: https://github.com/ruby/ruby/pull/4469#discussion_r628386707
	---
	 compile.c | 4 ++--
	 1 file changed, 2 insertions(+), 2 deletions(-)

	test_cdhash: refactor change class

	It is now strange to test Complex in a class named Rational_Test.
	---
	 test/ruby/test_rational.rb | 15 ---------------
	 test/ruby/test_syntax.rb   | 15 +++++++++++++++
	 2 files changed, 15 insertions(+), 15 deletions(-)
2021-05-22 17:09:16 +09:00

72 lines
2.3 KiB
C

#ifndef INTERNAL_RATIONAL_H /*-*-C-*-vi:se ft=c:*/
#define INTERNAL_RATIONAL_H
/**
* @file
* @author Ruby developers <ruby-core@ruby-lang.org>
* @copyright This file is a part of the programming language Ruby.
* Permission is hereby granted, to either redistribute and/or
* modify this file, provided that the conditions mentioned in the
* file COPYING are met. Consult the file for details.
* @brief Internal header for Rational.
*/
#include "ruby/internal/config.h" /* for HAVE_LIBGMP */
#include "ruby/ruby.h" /* for struct RBasic */
#include "internal/gc.h" /* for RB_OBJ_WRITE */
#include "internal/numeric.h" /* for INT_POSITIVE_P */
#include "ruby_assert.h" /* for assert */
struct RRational {
struct RBasic basic;
VALUE num;
VALUE den;
};
#define RRATIONAL(obj) ((struct RRational *)(obj))
/* rational.c */
VALUE rb_rational_canonicalize(VALUE x);
VALUE rb_rational_uminus(VALUE self);
VALUE rb_rational_plus(VALUE self, VALUE other);
VALUE rb_rational_minus(VALUE self, VALUE other);
VALUE rb_rational_mul(VALUE self, VALUE other);
VALUE rb_rational_div(VALUE self, VALUE other);
VALUE rb_lcm(VALUE x, VALUE y);
VALUE rb_rational_reciprocal(VALUE x);
VALUE rb_cstr_to_rat(const char *, int);
VALUE rb_rational_hash(VALUE self);
VALUE rb_rational_abs(VALUE self);
VALUE rb_rational_cmp(VALUE self, VALUE other);
VALUE rb_rational_pow(VALUE self, VALUE other);
VALUE rb_rational_floor(VALUE self, int ndigits);
VALUE rb_numeric_quo(VALUE x, VALUE y);
VALUE rb_float_numerator(VALUE x);
VALUE rb_float_denominator(VALUE x);
static inline void RATIONAL_SET_NUM(VALUE r, VALUE n);
static inline void RATIONAL_SET_DEN(VALUE r, VALUE d);
RUBY_SYMBOL_EXPORT_BEGIN
/* rational.c (export) */
VALUE rb_gcd(VALUE x, VALUE y);
VALUE rb_gcd_normal(VALUE self, VALUE other);
#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
VALUE rb_gcd_gmp(VALUE x, VALUE y);
#endif
RUBY_SYMBOL_EXPORT_END
static inline void
RATIONAL_SET_NUM(VALUE r, VALUE n)
{
assert(RB_INTEGER_TYPE_P(n));
RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n);
}
static inline void
RATIONAL_SET_DEN(VALUE r, VALUE d)
{
assert(RB_INTEGER_TYPE_P(d));
assert(INT_POSITIVE_P(d));
RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d);
}
#endif /* INTERNAL_RATIONAL_H */