Floor: Difference between revisions

Jump to navigation Jump to search
1,600 bytes added ,  16:27, 24 December 2023
m
(Added Reference for Comparison Tolerance)
 
(One intermediate revision by one other user not shown)
Line 37: Line 37:
0 1 1
0 1 1
</syntaxhighlight>
</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 ===

Navigation menu