Skip to content

"XQ" and "->QPi" implementation#1652

Open
jcbenoist wants to merge 10 commits intoc3d:devfrom
jcbenoist:feature/XQ
Open

"XQ" and "->QPi" implementation#1652
jcbenoist wants to merge 10 commits intoc3d:devfrom
jcbenoist:feature/XQ

Conversation

@jcbenoist
Copy link

@jcbenoist jcbenoist commented Mar 7, 2026

Based on https://www.hpcalc.org/details/7450 QPI for HP Prime, by Han Duong (HPSolo) and Mika Heiskanen, clauded by Claude Opus 4.6 and orchestred and conducted by me.

Note : I've tried to connect the algorithm to "to_fraction" and "DFC", to save space and have global coherency, but convergence criterions are not the same than in HP Prime QPI algorithm, leading to numerical instabilities. Comments in code explains that.

@c3d By the way : DFC (and DFC2F) was merged in 0.9.16, but not advertised or documented. Do you mind if I add add DFC/DFC2F doc in this PR (in numerics.md ?), and add it to a menu (there is a space left in "NUM" menu)

@jcbenoist jcbenoist changed the title Feature/xq "XQ" and "->QPi" implementation Mar 7, 2026
jcbenoist and others added 3 commits March 9, 2026 13:01
Signed-off-by: jcbenoist <61346755+jcbenoist@users.noreply.github.com>
@jcbenoist
Copy link
Author

@c3d You does not communicate very much. I have resolved conflicts (using existing ToFractionPi), but I had to figure out the problem myself. I'll add the documentation to DFC here, in absence of other instructions. Best regards and all my thanks for this astonishing calculator, anyways.

@c3d
Copy link
Owner

c3d commented Mar 9, 2026

Go ahead for the doc. I auto generated the release notes and it somehow missed these commands. Sorry.

As for not communicating much, I just signed my divorce so my mind was elsewhere and now I am playing catch up. Should improve.

@jcbenoist
Copy link
Author

@c3d Oh, so sorry to hear this new. Please don't worry being out of touch; focus on yourself and manage your priorities for the best !

@c3d
Copy link
Owner

c3d commented Mar 9, 2026

I noticed based on the commit date that you apparently started working on this a couple of days before I did. Sorry about the duplicate effort.

@c3d
Copy link
Owner

c3d commented Mar 9, 2026

Reading your documentation, I noticed that you define →Qπ as not expanding logs, etc. However, the HP50G version apparently does. Maybe we need a compatibility flag for that behaviour?

@jcbenoist
Copy link
Author

->QPi HP50 documentation is very sparce. I admit I have not fully understood this command, and taken the simplest way. For example, https://literature.hpcalc.org/official/hp50g-aur-en.pdf does not give any advanced examples, and examples given are exactly the results of ->Q. If ln, squares etc. are taken into account, I don't really see differences with XQ.

@c3d
Copy link
Owner

c3d commented Mar 9, 2026

I usually use the physical device to check when in doubt. Well… '3/4×e' gives me 31072/15241 with both XQ and →Qπ on the HP50G, so 'e' detection is not as strong as on DB48x where it does give me '3/4×e' back.

At FIX 6, however, exp(1) is turned into e^1 by both XQ and →Qπ

@jcbenoist
Copy link
Author

Anyways, db48x is not a strict emulation. A lot of algorithms (some very complex) are around to transform a real to an algebric expression, and the QPI program used as fondation, is the closer to XQ I know, not too strong, not too weak and not too complex. But other are possible, anf maybe closer to HP50. Your experiences given above shows no real difference between ->QPi and XQ . The difference is unclear. (I have now NewRPL on my HP50g..)

@c3d
Copy link
Owner

c3d commented Mar 10, 2026

Hey @jcbenoist. Thanks a lot for all the work!

I did a quick size comparison between your implementation and the existing one, and the new implementation uses about 4K more code. This is a lot for a single command, given how severely constrained and close to the limit we are on the DM42.

Size Comparison: dev vs PR 1652

Artifact dev (current + XQ tests) PR 1652 Δ
ARM Firmware
db48x.pgm 694,212 B 698,724 B +4,512
db48x_flash.bin 694,192 B 698,704 B +4,512
db48x_qspi.bin 296,428 B 296,292 B −136
ELF (text) 896,136 900,688 +4,552
ELF (data) 94,480 94,304 −176
Simulator binary 12,217,144 B 12,239,000 B +21,856

Summary

  • Flash: PR 1652 adds about 4.5 KB, mainly from the new exact-quotient module (733 lines).
  • QSPI: PR 1652 is 136 B smaller (removals outweigh additions).
  • Simulator: PR 1652 is about 22 KB larger (debug build with address sanitizer).

Additionally, 22 of your tests pass as is, and the tests that fail are mostly due to formatting differences:

XQ Tests: Failures (dev with PR 1652 test suite)

# Step Expected Got
1 XQ(√(1/3)) '√(¹/₃)' '¹/₃·√ 3'
2 XQ(π) π Error: Bad argument type
3 XQ(π/3) '¹/₃·π' 'π÷3'
4 XQ(2π) '2·π' 'π·2'
5 XQ(-π/3) '-¹/₃·π' '-(π÷3)'
6 XQ(√2/789) '√ 2÷789' '¹/₂₀₇ ₅₀₇·√ 138 338'
7 XQ(-√3/5) '-(√ 3÷5)' '-¹/₅·√ 3'
8 HP Prime QPI(.405465108108) 'ln(³/₂)' 'exp(-³¹ ⁰⁵⁹/₃₄ ₄₀₆)'

Result: 22 passed, 8 failed (30 total)

So my current approach is to take your tests, to understand how to get the ln(3/2), to adjust a bit the output to make it nicer, to add support for symbolic input, and to investigate tests 6 and 8 which are the only real differences.

@jcbenoist
Copy link
Author

jcbenoist commented Mar 11, 2026

The ROM size is a big concern, I agree, and sometime needs compromizes. Some test cases was chosen to be challenging, and maybe are not representative of the average command usage. sqrt(2)/789 is surprizingly difficult, and I have struggled with this very test case too.
Some "failures" you presented are not really failures : presentation is not ideal, but answers are correct. Only 2, 6 and 8 are concerning (and 2 easily fixable) I agree it may not worth 4Kb. Feel free not to merge this PR, or cherry pick tests, doc or other things (dont forget DFC ;) ! ) I really don't mind : I had fun to build it with Claude, and I fully understand your concerns.

BTW : can you orient me toward some other important command implementation, without risks to collide your own work ;) ?

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.

2 participants