Bits of integer
|
12-15-2022, 07:44 PM
(This post was last modified: 08-20-2023 05:13 PM by Albert Chan.)
Post: #41
|
|||
|
|||
RE: Bits of integer
(12-15-2022 04:08 PM)Thomas Klemm Wrote: Can we expect your explanations in APL instead of Lua from now on? Lua is still my favorite. I don't like write-once language, with comments that is much longer than code. Any editing of code, you then have to correct for old comments. The Law of Leaky Abstractions - Joel on Software Quote:All non-trivial abstractions, to some degree, are leaky. Devlin's Angle, Base Consideration, Feb 1996 Quote:Playing with arithmetic having negative bases is an amusing classroom exercise, but, strange as it may seem, a computer was once built that used "-2" base arithmetic. It was the UMC-1, a Polish-made computer of the late 1950s and early 1960s, of which several dozen were made and installed. base ← ⊥⍣¯1 ¯2 base 123 DOMAIN ERROR APL code cannot convert to base -2. It may take lots of work to fix. Same problem solved in Lua is easy. Code: function base(n, b) lua> require'pprint' lua> pprint(base(123, -2)) -- 9 bits { 1, 1, 0, 0, 0, 1, 1, 1, 1 } lua> pprint(base(123, 2)) -- 7 bits { 1, 1, 1, 1, 0, 1, 1 } For base 2, we can directly lookup binary exponent. lua> logb(123) + 1 -- bits of 123 7 Update: base(n, b) extended to negative n's. It uses Lua floor-mod property. (sign matching divisor) With this update, we have invariant: horner(base(n, b), b) = n lua> pprint(base(-123, -2)) -- negative b, always non-negative digits { 1, 0, 0, 0, 0, 1, 0, 1 } lua> pprint(base(-123, +2)) -- positive b, sign of digits = sign of n { -1, -1, -1, -1, 0, -1, -1 } |
|||
12-17-2022, 02:31 AM
Post: #42
|
|||
|
|||
RE: Bits of integer
(12-13-2022 02:37 AM)Joe Horn Wrote: Here's a brute-force implementation of that method: That's not that much for a calculator that came out 40 years ago. My Sanyo (1977) needs more than 30s to compute the factorial of 69!! I should have bought one 16C when you could find it in stores… Of course, with a DM16L, it would be faster (and it's about half the price). (12-13-2022 09:17 AM)Joe Horn Wrote: Here's a 16C-only routine that doesn't require any looping. The LJ command is on the [g] [A] key. #B is on the [g] [7] key. I have tried both of your programs at: https://stendec.io/ctb/rpn_prog.html Thank you! Too bad the RPL calculators don't have all the functions of the 16C. However, trying to program the missing functions might be a good exercise! Bruno Sanyo CZ-0124 ⋅ TI-57 ⋅ HP-15C ⋅ Canon X-07 + XP-140 Monitor Card ⋅ HP-41CX ⋅ HP-28S ⋅ HP-50G ⋅ HP-50G |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)