Skip to content

Conversation

@bollu
Copy link
Contributor

@bollu bollu commented Jan 20, 2026

The fast semantics should be closer to the 'round' implementation, which will help. inspired by @abdoo8080 , who suggested that we have this for fast execution.

Comment on lines +162 to +185
/-- The upper approximant of 'v'.
Returns the smallest 'x : X' such that 'r ≤ v x'. -/
def upper (e s : Nat) (r : ExtRat) : PackedFloat e s :=
(lower e s r.neg).neg

/-- Lower half, return 'true' iff we are strictly in the lower half. -/
def lh (e s : Nat) (r : ExtRat) : Bool :=
(r - (lower e s r).toExtRat) < (upper e s r).toExtRat - r

/-- Tiebreak, return 'true' iff we are exactly in the middle of the lower and upper approximants. -/
def tb (e s : Nat) (r : ExtRat) : Bool :=
r - (lower e s r).toExtRat = (upper e s r).toExtRat - r
/-- Upper half, return 'true' iff we are strictly in the upper half. -/
def uh (e s : Nat) (r : ExtRat) : Bool :=
(r - (lower e s r).toExtRat) > (upper e s r).toExtRat - r

/-- Check if 'X' is even. -/
def ev (e s : Nat) (x : PackedFloat e s) : Bool :=
match x.toExtRat with
| .Number n =>
let den := n.den
let num := n.num
num = 0 ∨ (den = 1 ∧ num.natAbs % 2 = 0)
| _ => false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These differ from the definition in the paper...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean --- the paper just says "let there exist an integer such that 2 * integer equals the rat", and this should be the computational version of this?

@bollu
Copy link
Contributor Author

bollu commented Jan 21, 2026

While the slow / reference implementation works, the fast one doesn't seem to. Debugging what's going on...

Comment on lines 198 to 213
if _hz : r = .Number 0 then rsz e s sign
else
if _hlh : lh e s r
then lower e s
else
if _htb : tb e s r
then
if _heven : ev e s (lower e s r)
then lower e s
else upper e s
else
-- not tie break, not lower, so we are in upper half.
-- have : uh r v := by
-- have := trichotomy_lh_tb_uh r v
-- grind
upper e s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you flatten the ites to mirror the paper? Don't worry about introducing redundancies in the conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants