Floor¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Floor[x]
gives the greatest integer less than or equal to x.
Floor[x, a]
gives the greatest multiple of a less than or equal to x.
Notes
Floor is Listable. Exact (Integer / BigInt / Rational) inputs return exact integers; Real / MPFR inputs are rounded toward -Infinity at the input precision; symbolic inputs stay unevaluated.Examples (5)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (5)¶
In[1]:= Floor[7/2]
Out[1]= 3
In[2]:= Floor[-2.3]
Out[2]= -3
In[3]:= Floor[17, 5]
Out[3]= 15
In[4]:= Floor[{2.7, -2.7, 5/2, 11/3}]
Out[4]= {2, -3, 2, 3}
In[5]:= Floor[N[Pi, 40] 10^20]
Out[5]= 314159265358979323846
Implementation notes¶
builtin_floor calls the shared do_piecewise(res, OP_FLOOR, "Floor", true) dispatcher (the true enables the two-argument Floor[x, a] → a Floor[x/a] unit-quantization form). The per-element kernel do_piecewise_1 handles each numeric type exactly: EXPR_INTEGER/EXPR_BIGINT are already integers and pass through; rationals are floored exactly via GMP; EXPR_REAL uses C floor() cast to int64_t; EXPR_MPFR uses mpfr_floor then mpfr_get_z into an mpz_t (normalized to int/bigint) so arbitrarily large values never silently truncate; ±Infinity pass through. Floor is registered LISTABLE | NUMERICFUNCTION | PROTECTED, so threading over lists is handled by the generic evaluator before the builtin runs. Non-numeric arguments return NULL (left symbolic).
Attributes: Listable, NumericFunction, Protected.
References¶
- Source:
src/piecewise.c - Specification:
docs/spec/builtins/arithmetic.md - Tests:
tests/test_compile.c - Tests:
tests/test_compile_coverage.c - Tests:
tests/test_compiledfunction.c - Tests:
tests/test_complement.c
Notes & additional examples¶
Notes¶
Floor[x] rounds toward -Infinity; the two-argument Floor[x, a] gives the
greatest multiple of a not exceeding x. Exact inputs return exact
integers, and Floor is Listable so it threads over a vector of mixed
reals and rationals. The last example extracts the first 21 significant
digits of π exactly by flooring a 40-digit MPFR value scaled by 10^20.