Post Reply 
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.
Abstractions fail. Sometimes a little, sometimes a lot. There’s leakage. Things go wrong.
It happens all over the place when you have abstractions. Here are some examples ...

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)
    b = b or 10
    local T, i, B = {}, 0, (b<0 or n<0) and -b or b
    while n~=0 do i=i+1; T[i]=n%B; n=(n-T[i])/b end -- digits of n, in base b
    for j=1,i/2 do T[i],T[j] = T[j],T[i]; i=i-1 end -- reverse digits
    return T
end

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 }
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Bits of integer - Albert Chan - 12-05-2022, 12:12 PM
RE: Bits of integer - Werner - 12-05-2022, 12:41 PM
RE: Bits of integer - J-F Garnier - 12-05-2022, 02:55 PM
RE: Bits of integer - Albert Chan - 12-05-2022, 04:13 PM
RE: Bits of integer - Valentin Albillo - 12-05-2022, 04:29 PM
RE: Bits of integer - J-F Garnier - 12-06-2022, 08:29 AM
RE: Bits of integer - robve - 12-05-2022, 10:06 PM
RE: Bits of integer - Albert Chan - 12-06-2022, 12:15 AM
RE: Bits of integer - John Keith - 12-06-2022, 07:15 PM
RE: Bits of integer - robve - 12-06-2022, 08:15 PM
RE: Bits of integer - Thomas Klemm - 12-06-2022, 07:41 AM
RE: Bits of integer - Albert Chan - 12-06-2022, 06:49 PM
RE: Bits of integer - robve - 12-06-2022, 07:26 PM
RE: Bits of integer - Albert Chan - 12-06-2022, 09:34 PM
RE: Bits of integer - FLISZT - 12-07-2022, 12:52 AM
RE: Bits of integer - Paul Dale - 12-07-2022, 06:33 AM
RE: Bits of integer - J-F Garnier - 12-07-2022, 08:41 AM
RE: Bits of integer - Albert Chan - 12-07-2022, 02:51 PM
RE: Bits of integer - J-F Garnier - 12-10-2022, 09:31 AM
RE: Bits of integer - Joe Horn - 12-07-2022, 07:18 AM
RE: Bits of integer - Werner - 12-07-2022, 08:00 AM
RE: Bits of integer - pinkman - 12-07-2022, 10:13 PM
RE: Bits of integer - mfleming - 12-07-2022, 09:48 PM
RE: Bits of integer - John Keith - 12-11-2022, 09:09 PM
RE: Bits of integer - FLISZT - 12-12-2022, 04:54 PM
RE: Bits of integer - cdmackay - 12-12-2022, 07:28 PM
RE: Bits of integer - FLISZT - 12-12-2022, 08:14 PM
RE: Bits of integer - mfleming - 12-12-2022, 10:07 PM
RE: Bits of integer - Joe Horn - 12-13-2022, 02:37 AM
RE: Bits of integer - Joe Horn - 12-13-2022, 09:17 AM
RE: Bits of integer - mfleming - 12-13-2022, 12:15 PM
RE: Bits of integer - Albert Chan - 12-14-2022, 02:35 PM
RE: Bits of integer - mfleming - 12-14-2022, 11:16 PM
RE: Bits of integer - Gjermund Skailand - 12-14-2022, 09:47 PM
RE: Bits of integer - robve - 12-15-2022, 02:07 AM
RE: Bits of integer - Albert Chan - 12-15-2022, 06:08 AM
RE: Bits of integer - EdS2 - 12-15-2022, 09:06 AM
RE: Bits of integer - Albert Chan - 12-15-2022, 12:29 PM
RE: Bits of integer - mfleming - 12-15-2022, 12:59 PM
RE: Bits of integer - Thomas Klemm - 12-15-2022, 04:08 PM
RE: Bits of integer - Albert Chan - 12-15-2022 07:44 PM
RE: Bits of integer - FLISZT - 12-17-2022, 02:31 AM



User(s) browsing this thread: 1 Guest(s)