Re: Peculiar number formatting?
"David McClain (as dbm at refined-audiometrics dot com)" <[email protected]> Wed, 20 May 2026 11:07:34 -0700
| Newsgroups | gmane.lisp.lispworks.general |
|---|---|
| Message-ID | <[email protected]> |
To elucidate, When I write my DSP simulation code, Lisp is using its Integer facility. It does not require Bignums, but it also doesn’t restrict itself naturally to 24- or 48-bits, in the way that my DSP chip does, which uses saturating arithmetic. But that also gives me the chance to spot inadvertent overflows in my algorithms, which would otherwise just saturate on the DSP chip. So I use Lisp to help find some great MiniMax approximations for things I need, like dB10(x) and ampl20(x), and Lisp then simulates the action of the DSP chip but also allows me to find anomalies that wouldn’t show up as obvious over-range during intermediate calculation results. Hence the need to see signed hex values when they are over-range of 24- or 48-bit values. FWIW, C printf() doesn’t format negative integer values, in hexadecimal with a leading sign. It simply wraps and prints the finite width integer value, bit for bit. But decimal format does, sort of, obey the #digits spec: printf(“%05d”, 15) => 00015 but printf(“%05d”, -15) => -0015 So that 05 is a width spec, not a number of digits spec. And it reasonably plants the minus sign in front. By contrast, in Lisp (format nil “~5,’0d” -15) => “00-15” which looks totally weird to me. - DM > On May 19, 2026, at 15:09, David McClain (as dbm at refined-audiometrics dot com) <[email protected]> wrote: > > > >> On May 19, 2026, at 05:16, Pascal Bourguignon <[email protected]> wrote: >> >> But the truth of hexadecimal, octal and binary formatting, >> is that you don’t use them for negative numbers. When you >> have negative integers, which is Common Lisp have infinite >> number of digits (1s) before the meaningful digits, what you >> do to format them is that you truncate them to the word size: >> > > > Heh! Yes you are correct, until you overflow (in Lisp) the 24-bit register size. But you still need a way to show what is happening. > > That’s when I resort to printing the prefix #\- sign and a bunch of hex digits from the absolute value. > > Furthermore, to ease the eyestrain I insert #\_ between digit groups. A 24-but hex number is shown as xx_xxxx while a 48-bit number is shown as a concatenation of two 24-bit numbers: xx_xxxx_xx_xxxx. >