Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)
|
Public APIs related to rb_cRational. More...
#include "ruby/internal/attr/pure.h"
#include "ruby/internal/dllexport.h"
#include "ruby/internal/value.h"
#include "ruby/internal/arithmetic/long.h"
Go to the source code of this file.
Macros | |
#define | rb_rational_raw1(x) |
Shorthand of (x/1)r. | |
#define | rb_rational_raw2(x, y) |
Just another name of rb_rational_raw. | |
#define | rb_rational_new1(x) |
Shorthand of (x/1)r. | |
#define | rb_rational_new2(x, y) |
Just another name of rb_rational_new. | |
#define | rb_Rational1(x) |
Shorthand of (x/1)r. | |
#define | rb_Rational2(x, y) |
Just another name of rb_Rational. |
Functions | |
VALUE | rb_rational_raw (VALUE num, VALUE den) |
Identical to rb_rational_new(), except it skips argument validations. | |
VALUE | rb_rational_new (VALUE num, VALUE den) |
Constructs a Rational, with reduction. | |
VALUE | rb_Rational (VALUE num, VALUE den) |
Converts various values into a Rational. | |
VALUE | rb_rational_num (VALUE rat) |
Queries the numerator of the passed Rational. | |
VALUE | rb_rational_den (VALUE rat) |
Queries the denominator of the passed Rational. | |
VALUE | rb_flt_rationalize_with_prec (VALUE flt, VALUE prec) |
Simplified approximation of a float. | |
VALUE | rb_flt_rationalize (VALUE flt) |
Identical to rb_flt_rationalize_with_prec(), except it auto-detects appropriate precision depending on the passed value. |
Public APIs related to rb_cRational.
Definition in file rational.h.
#define rb_Rational1 | ( | x | ) |
Shorthand of (x/1)r.
It practically converts it into a Rational of the identical value.
[in] | x | rb_cInteger, rb_cRational, or something that responds to #to_r. |
Definition at line 116 of file rational.h.
#define rb_Rational2 | ( | x, | |
y ) |
Just another name of rb_Rational.
Definition at line 119 of file rational.h.
#define rb_rational_new1 | ( | x | ) |
Shorthand of (x/1)r.
As x is already an Integer, it practically converts it into a Rational of the identical value.
[in] | x | An instance of rb_cInteger. |
Definition at line 74 of file rational.h.
Referenced by rb_flt_rationalize().
#define rb_rational_new2 | ( | x, | |
y ) |
Just another name of rb_rational_new.
Definition at line 77 of file rational.h.
Referenced by rb_flt_rationalize(), and rb_flt_rationalize_with_prec().
#define rb_rational_raw1 | ( | x | ) |
Shorthand of (x/1)r.
As x is already an Integer, it practically converts it into a Rational of the identical value.
[in] | x | An instance of rb_cInteger. |
Definition at line 51 of file rational.h.
#define rb_rational_raw2 | ( | x, | |
y ) |
Just another name of rb_rational_raw.
Definition at line 54 of file rational.h.
Identical to rb_flt_rationalize_with_prec(), except it auto-detects appropriate precision depending on the passed value.
[in] | flt | An instance of rb_cFloat to rationalise. |
Definition at line 2242 of file rational.c.
Referenced by rb_flt_rationalize().
Simplified approximation of a float.
It returns a rational rat which satisfies:
[in] | flt | An instance of rb_cFloat to rationalise. |
[in] | prec | Another rb_cFloat, which is the "precision". |
Definition at line 2226 of file rational.c.
Referenced by rb_flt_rationalize_with_prec().
Converts various values into a Rational.
This function accepts:
It (possibly recursively) applies #to_r until both sides become either Integer or Rational, then divides them.
As a special case, passing RUBY_Qundef to den is the same as passing RB_INT2NUM(1).
[in] | num | Numerator (see above). |
[in] | den | Denominator (see above). |
rb_eTypeError | Passed something not described above. |
rb_eFloatDomainError | `#to_r` produced Nan/Inf. |
rb_eZeroDivError | `#to_r` produced zero for `den`. |
Definition at line 1980 of file rational.c.
Referenced by rb_Rational().
Queries the denominator of the passed Rational.
[in] | rat | An instance of rb_cRational. |
Definition at line 1995 of file rational.c.
Referenced by rb_rational_den(), and rb_str_format().
Constructs a Rational, with reduction.
This returns for instance (2/3)r for rb_rational_new(INT2NUM(-384), INT2NUM(-576)).
[in] | num | Numerator, an instance of rb_cInteger. |
[in] | den | Denominator, an instance of rb_cInteger. |
rb_eZeroDivError | `den` is zero. |
Definition at line 1974 of file rational.c.
Referenced by rb_rational_new().
Queries the numerator of the passed Rational.
[in] | rat | An instance of rb_cRational. |
Definition at line 1989 of file rational.c.
Referenced by rb_rational_num(), and rb_str_format().
Identical to rb_rational_new(), except it skips argument validations.
It is thus dangerous for extension libraries. For instance 1/0r could be constructed using this.
[in] | num | Numerator, an instance of rb_cInteger. |
[in] | den | Denominator, an instance of rb_cInteger. |
rb_eTypeError | Either argument is not an Integer. |
Definition at line 1960 of file rational.c.
Referenced by rb_big_pow(), and rb_rational_raw().