Re: Documentation and output of pretty_print()

new topic     » goto parent     » topic index » view thread      » older message » newer message

Pete Lomax wrote:

> On Mon, 13 Sep 2004 08:08:02 +0200, Juergen Luethje <j.lue at gmx.de>
> wrote:
>
>> I just realized that both Eu's printf() and pretty_print() also lose the
>> '-' sign.
> Yup.
>> The problem arises with negative numbers. Currently, the '-' sign is
>> lost, I think that's a bug.
> Maybe it is a bug, maybe it is not. Technically, if the number has a
> sign, then the hex just needs to show a set bit where the sign bit is.
> One of the reasons I previously went with signed 32-bit numbers only
> was to make the hex output unambiguous:
>
> }}}
<eucode>
> 	printf(1,"%x %x",{4294967295,-1})
> </eucode>
{{{

>
> will output "FFFFFFFF FFFFFFFF". That is why the previous version
> restricted integers from #80000000 (-2147483648) to #7FFFFFFF
> (+2147483647), with the sign implied from the hex, if you see what I
> mean. I suppose that technically it now prints 33-bit numbers blink

I see what you mean, at least I hope so. :o)
IIRC, things actually work like this for instance in PowerBASIC:
E.g. when 'a' is a signed 16-bit integer variable, and 'a = FFFF', then
'a' has a negative value, because the highest order bit is set.
When 'a' is a 32-bit integer variable, and also 'a = FFFF', then it
actually is here 'a = 0000FFFF'. So we see, that now the highest order
bit is not set, and thus 'a' has a positive value.

But when I learned to know Euphoria, I had the impression that
hexadecimal numbers work different in Euphoria. From my experience,
in Euphoria hexadecimal numbers (without the '-' sign!) always represent
positive values. That's why I write weird stuff like above. :o)
Is this true or not?

Here are the examples from 'refman_2.htm#1' (Eu 2.4):
    #FE             -- 254
    #A000           -- 40960
    #FFFF00008      -- 68718428168
   -#10             -- -16

The '-' sign is explicitely used here to indicate a negative number.

For instance in PowerBASIC for DOS 3.2, the following code
<pbcode>        :o)
? hex$(-16)
</pbcode>

prints 'FFF0' and

<pbcode>
? &hFFF0
</pbcode>

prints -16.

While in Euphoria
? #FFF0

prints 65520 instead.

Are there actually hex numbers in Euphora without a '-' sign, that
represent negative numbers??

printf(1, "%x", -16)        -- prints 'FFFFFFF0', so
printf(1, "%d", #FFFFFFF0)  -- should print '-16', but it prints 4294967280!
This is a bug, no??

> Anyway, the last version had
>
> }}}
<eucode>
> 	elsif cl<0 then
> 		txt='-'&sprintf(ppp_IntFmt,-cl)
> </eucode>
{{{

>
> which makes pp({4294967295,-1}) output {#FFFFFFFF,-#1}.
> (assuming of course that pp_IntFmt is "%x")
>
>> But when the minus sign wil be included in
>> the output, using the suggested format, then there will be output
>> strings such as
>> }}}
<eucode>
>> #-FF
>> </eucode>
{{{

> The code above causes "-#FF" to be printed (for -255).

So pp() is smart enough not to print "#-FF". Cool!

> I can accept someone might expect #FFFFFF01 instead.

Yes, that's what printf() and pretty_print() are printing.
But  '? -#FF'  prints '-255' again. This is what a naive person would
expect IMHO. So I, personally, thank you for making pp() behave this way!

> I feel another option coming on to enable/disable the two lines of
> code for negative integer handling above, and/or limit the integer
> range to avoid ambiguity... I will probably wait until someone asks
> for it though blink)
>
> <snip>
>> fmt = "%x\n"
> I trust you don't normally put \n's in pp_IntFmt or pp_FltFmt as they
> completely mangle the output. Pretty much the whole point of ppp.e is
> to automatically put in \n's in the right places for you!

In the sample code that I provided in my previous code, it actually made
sense. You can easily test it by removing \n from the format strings and
see what happens.
No, normally I don't do so. Thanks for the advice anyway.

> <snip>
>> This raises the error "hex number not formed correctly".
>> So what to do now?
> If you want machine-readable output, I suggest you avoid hex formats;
> %d (the default) will do just as well. What exactly is it that must be
> nicely formatted for human consumption and also machine readable?

My Eu code must be machine readable in any case. And source code should
be as good readable by humans as possible, in order to find bugs, and
maintain the code. Say there are some constants or variables, that mean
certain years, then I'd prefer to write 2003, 2004, etc. rather than
their hexadecimal equivalents. As you know, on the other hand
hexadecimal numbers are often preferable, when dealing with bits and
bytes.
For instance "constant CRC_TABLE" in my library 'bit.e' was written by
a little utility program, and I wanted the entries in "constant
CRC_TABLE" to be hex numbers.
I believe the bottom line is, that I want to have as much control as
possible, when writing programs. That's why I learned programming anyway,
because I want to tell the PC what to do, rather than the other way
round. smile

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu