Floor: Difference between revisions
m (→Model) |
|||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{Built-in|Floor|⌊}} is a [[monadic]] [[scalar function]] | {{Built-in|Floor|⌊}} is a [[monadic]] [[scalar function]] that gives the [[wikipedia:floor and ceiling functions|floor]] of a real number, that is, the greatest integer tolerantly<ref>[[Robert Bernecky|Bernecky, Robert]]. [https://www.jsoftware.com/papers/satn23.htm "Comparison Tolerance"]. Sharp APL Technical Notes. 1977-06-10;.</ref> [[less than or equal to]] the given value. This operation is also known as '''integral part''', '''entier''', and '''round down'''. Floor shares the [[glyph]] <syntaxhighlight lang=apl inline>⌊</syntaxhighlight> with the dyadic arithmetic function [[Minimum]]. [[Comparison_with_traditional_mathematics#Prefix|Traditional mathematics]] derives [[Ken_Iverson#Floor_and_Ceiling|its notation]] and name for floor from APL. | ||
== Examples == | == Examples == | ||
Line 5: | Line 5: | ||
Floor rounds down the given numbers to the nearest integers. | Floor rounds down the given numbers to the nearest integers. | ||
< | <syntaxhighlight lang=apl> | ||
⌊2 2.8 ¯2 ¯2.8 | ⌊2 2.8 ¯2 ¯2.8 | ||
2 2 ¯2 ¯3 | 2 2 ¯2 ¯3 | ||
</ | </syntaxhighlight> | ||
Rounding to the ''nearest'' integer (rounding up on half) can be achieved by [[add|adding]] 0.5 before applying Floor. | Rounding to the ''nearest'' integer (rounding up on half) can be achieved by [[add|adding]] 0.5 before applying Floor. | ||
< | <syntaxhighlight lang=apl> | ||
⌊0.5+2 2.3 2.5 2.8 | ⌊0.5+2 2.3 2.5 2.8 | ||
2 2 3 3 | 2 2 3 3 | ||
</ | </syntaxhighlight> | ||
Integral quotient of division can be found with [[divide|division]] followed by Floor. | Integral quotient of division can be found with [[divide|division]] followed by Floor. | ||
< | <syntaxhighlight lang=apl> | ||
⌊10 20 30÷3 | ⌊10 20 30÷3 | ||
3 6 10 | 3 6 10 | ||
</ | </syntaxhighlight> | ||
== Properties == | == Properties == | ||
Line 30: | Line 30: | ||
Floor is affected by [[comparison tolerance]]. If the given number is [[tolerant comparison|tolerantly equal]] to its [[ceiling]], it is rounded to that number instead. | Floor is affected by [[comparison tolerance]]. If the given number is [[tolerant comparison|tolerantly equal]] to its [[ceiling]], it is rounded to that number instead. | ||
< | <syntaxhighlight lang=apl> | ||
⎕PP←16 | ⎕PP←16 | ||
⎕←v←1+0.6×⎕CTׯ2 ¯1 0 | |||
0.999999999999988 0.999999999999994 1 | 0.999999999999988 0.999999999999994 1 | ||
⌊v | ⌊v | ||
0 1 1 | 0 1 1 | ||
</ | </syntaxhighlight> | ||
== Model == | |||
Floor can very easily be modelled using residue like: | |||
<syntaxhighlight lang=apl> | |||
model←{⍵-1|⍵} | |||
</syntaxhighlight> | |||
To model it without using residue, because residue uses floor under the hood, approaches like converting to strings and then stripping the decimal component or converting to binary and stripping the decimal component can be used. | |||
<syntaxhighlight lang=apl> | |||
model←{ | |||
⎕pp←34 ⍝ set to max as we are using strings, the execute and format primitives round the number to the ⎕pp value | |||
dotPos←⍸,'.'⍷⍕⍵ ⍝ convert num to string and get the position of the decimal point | |||
int←⍎(⍕⍵)↑⍨¯1+dotPos ⍝ strip integer based on the decimal point | |||
int-(⍵<0)∧(~0∊⍴dotPos) ⍝ Subtract 1 only when negative+non int component exists. eg: ¯123.32→¯124 | |||
} | |||
</syntaxhighlight> | |||
Converting to the exponent/scientific notation (123E¯2) and then using the exponent and mantissa to strip the decimal points can be used. | |||
Warning: However, the method present here has issues dealing with larger values due to the loss in precision because of the ⍎ operator. | |||
<syntaxhighlight lang=apl> | |||
model←{ | |||
fmt←{⎕FR≡1287:¯33⍕⍵ ⋄ ¯16⍕⍵}⍵ | |||
(m e)←'E'(≠⊆⊢)fmt | |||
en←⍎e | |||
diff←-(⍵<0)∧('.'∊⍕⍵) | |||
en<0:diff+0 | |||
m↑⍨←3+(⍵<0)+en | |||
diff+⍎m,'E',e | |||
} | |||
</syntaxhighlight> | |||
Other approaches could include writing a hungry loop to evaluate the closest integer value and evaluate from there. | |||
=== Complex floor === | === Complex floor === | ||
Line 44: | Line 81: | ||
[[Eugene McDonnell]] designed the domain extension of Floor to [[complex number|complex numbers]].<ref>McDonnell, Eugene. [https://www.jsoftware.com/papers/eem/complexfloor.htm "Complex Floor"].</ref> Complex floor maps every complex number to a [[wikipedia:Gaussian integer|Gaussian integer]], a complex number whose real and imaginary parts are integers. It has an important property that the [[magnitude]] of [[subtract|difference]] between any complex number Z and its floor is [[less than]] 1. This extension is currently implemented in [[Dyalog APL]], [[J]], and [[NARS2000]], and is internally used to implement complex [[ceiling]], [[residue]], and [[GCD]]. | [[Eugene McDonnell]] designed the domain extension of Floor to [[complex number|complex numbers]].<ref>McDonnell, Eugene. [https://www.jsoftware.com/papers/eem/complexfloor.htm "Complex Floor"].</ref> Complex floor maps every complex number to a [[wikipedia:Gaussian integer|Gaussian integer]], a complex number whose real and imaginary parts are integers. It has an important property that the [[magnitude]] of [[subtract|difference]] between any complex number Z and its floor is [[less than]] 1. This extension is currently implemented in [[Dyalog APL]], [[J]], and [[NARS2000]], and is internally used to implement complex [[ceiling]], [[residue]], and [[GCD]]. | ||
< | <syntaxhighlight lang=apl> | ||
v←1.8J2.5 2.2J2.5 2.5J2.2 2.5J1.8 | v←1.8J2.5 2.2J2.5 2.5J2.2 2.5J1.8 | ||
⌊v | ⌊v | ||
Line 50: | Line 87: | ||
1>|v-⌊v | 1>|v-⌊v | ||
1 1 1 1 | 1 1 1 1 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
== External links == | == External links == | ||
Line 56: | Line 93: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Floor.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_110.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_110.htm APLX] | ||
* [https://www.jsoftware.com/help/dictionary/d011.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/ltdot NuVoc] | * [https://www.jsoftware.com/help/dictionary/d011.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/ltdot NuVoc] | ||
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#additional-arithmetic BQN] | |||
==References== | ==References== | ||
<references/> | <references/> | ||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]] |
Latest revision as of 16:27, 24 December 2023
⌊
|
Floor (⌊
) is a monadic scalar function that gives the floor of a real number, that is, the greatest integer tolerantly[1] less than or equal to the given value. This operation is also known as integral part, entier, and round down. Floor shares the glyph ⌊
with the dyadic arithmetic function Minimum. Traditional mathematics derives its notation and name for floor from APL.
Examples
Floor rounds down the given numbers to the nearest integers.
⌊2 2.8 ¯2 ¯2.8 2 2 ¯2 ¯3
Rounding to the nearest integer (rounding up on half) can be achieved by adding 0.5 before applying Floor.
⌊0.5+2 2.3 2.5 2.8 2 2 3 3
Integral quotient of division can be found with division followed by Floor.
⌊10 20 30÷3 3 6 10
Properties
The floor of any real number is an integer.
Floor is affected by comparison tolerance. If the given number is tolerantly equal to its ceiling, it is rounded to that number instead.
⎕PP←16 ⎕←v←1+0.6×⎕CTׯ2 ¯1 0 0.999999999999988 0.999999999999994 1 ⌊v 0 1 1
Model
Floor can very easily be modelled using residue like:
model←{⍵-1|⍵}
To model it without using residue, because residue uses floor under the hood, approaches like converting to strings and then stripping the decimal component or converting to binary and stripping the decimal component can be used.
model←{ ⎕pp←34 ⍝ set to max as we are using strings, the execute and format primitives round the number to the ⎕pp value dotPos←⍸,'.'⍷⍕⍵ ⍝ convert num to string and get the position of the decimal point int←⍎(⍕⍵)↑⍨¯1+dotPos ⍝ strip integer based on the decimal point int-(⍵<0)∧(~0∊⍴dotPos) ⍝ Subtract 1 only when negative+non int component exists. eg: ¯123.32→¯124 }
Converting to the exponent/scientific notation (123E¯2) and then using the exponent and mantissa to strip the decimal points can be used.
Warning: However, the method present here has issues dealing with larger values due to the loss in precision because of the ⍎ operator.
model←{ fmt←{⎕FR≡1287:¯33⍕⍵ ⋄ ¯16⍕⍵}⍵ (m e)←'E'(≠⊆⊢)fmt en←⍎e diff←-(⍵<0)∧('.'∊⍕⍵) en<0:diff+0 m↑⍨←3+(⍵<0)+en diff+⍎m,'E',e }
Other approaches could include writing a hungry loop to evaluate the closest integer value and evaluate from there.
Complex floor
- Main article: Complex Floor
Eugene McDonnell designed the domain extension of Floor to complex numbers.[2] Complex floor maps every complex number to a Gaussian integer, a complex number whose real and imaginary parts are integers. It has an important property that the magnitude of difference between any complex number Z and its floor is less than 1. This extension is currently implemented in Dyalog APL, J, and NARS2000, and is internally used to implement complex ceiling, residue, and GCD.
v←1.8J2.5 2.2J2.5 2.5J2.2 2.5J1.8 ⌊v 2J2 2J2 2J2 2J2 1>|v-⌊v 1 1 1 1
External links
Documentation
References
- ↑ Bernecky, Robert. "Comparison Tolerance". Sharp APL Technical Notes. 1977-06-10;.
- ↑ McDonnell, Eugene. "Complex Floor".