RationalQ¶
Status: Experimental
present and registered, but lightly documented and not yet covered by dedicated tests.
Description¶
RationalQ[expr]
gives True if expr is an exact rational number, False otherwise.
Rational[p, q]. Returns False on reals and on symbolic expressions.
Notes
True for an Integer or BigInt (an integer is rational) and for aExamples (4)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (4)¶
An exact Rational[3, 4]
An integer is rational
A machine real is not, even with a rational value
An irrational constant is not rational
Implementation notes¶
Algorithm. builtin_rationalq returns True when its one argument is an exact
rational number. That is exactly two cases, tested directly: the argument is
integer-like (expr_is_integer_like ā an EXPR_INTEGER or a GMP EXPR_BIGINT,
since every integer is rational) or it has head Rational (core_head_is(arg,
SYM_Rational), a Rational[p, q]). Otherwise False.
Data structures. None beyond the argument. Two cheap tests ā a type tag check
and an interned-pointer head comparison ā then a fresh True/False symbol.
Complexity / limits. O(1). Reals and MPFR numbers are not rational here
(2.5 is False) even when they have an exact rational value, because the test is
on representation; an irrational constant such as Pi, and any non-numeric
expression, is False. A call that is not single-argument declines and stays
symbolic.
Attributes: Protected.
References¶
- Source:
src/core.c - Specification index:
Mathilda_spec.md
Notes & additional examples¶
Notes¶
RationalQ[expr] is True for exactly the exact rationals: any integer (Integer
or big BigInt) and any Rational[p, q]. It is a representation test, so reals and
MPFR numbers are False regardless of their value, and irrational constants and
symbolic expressions are False as well. Non-single-argument calls are left
unevaluated.