Re: Hexadecimal numbers in Eu
- Posted by "Juergen Luethje" <j.lue at gmx.de> Sep 19, 2004
- 450 views
Robert Craig wrote: > Juergen Luethje wrote: >> >> Derek Parnell wrote: >> >>> Juergen Luethje wrote: >>>> >>>> Hi, >>>> >>>> in some other programming languages, hex numbers can represent positive >>>> or negative values. E.g. in PowerBASIC 3.2, >>>> ? &hFFF0 ' prints -16 >>>> ? &h00F0 ' prints 240 >>>> >>>>> From what I read at 'refman_2.htm#1' (Eu 2.4), and from my experience, >>>> I have the impression that hexadecimal numbers (that don't have a '-' >>>> sign at the beginning) in Euphoria _always_ have positive values. >>>> >>>> Rob, is this true? >>>> >>>> In Euphoria, e.g. >>>> }}} <eucode> >>>> ? #FFF0 -- prints 65520 >>>> ? #00F0 -- prints 240 >>>> </eucode> {{{ >>> >>> >>> Yes, hexadecimal literals are always positive unless they have a leading >>> minus symbol. >> >> Rob, maybe this sentence could be added to the docs? I think it will be >> very helpful for beginners -- and people like me, who sometimes feel >> like beginners. > > OK, I'll say something in the docs about this. Thanks, that will eliminate ambiguity. >>> However it is a bit confusing because negative integers, when converted >>> to display as hex digits via the printf() routine, display without >>> the minus sign. >>> >>> printf(1, "%x", -1) --> FFFFFFFF >> >> Yes, this actually confused me some days ago!! Thanks, Derek! >> >> Rob, is there any chance that in a future version of Euphoria, >> printf(1, "%x", -1) >> will show '-1' or '-#1' instead of 'FFFFFFFF'? > > Euphoria's %x format for printf() uses C's %x format, > and only works for numbers up to 32-bits in size. > }}} <eucode> > printf(1, "%x", #FFFFFFFF) > </eucode> {{{ > displays FFFFFFFF > as does: > }}} <eucode> > printf(1, "%x", -1) > </eucode> {{{ > but Euphoria does *not* consider #FFFFFFFF to be equal to -1. Either it should do so, or
printf(1, "%x", -1)
should *not* display FFFFFFFF!! This is a great inconsistency, and a source for confusion (as Derek wrote, too) and bugs. > When people are printing numbers with %x format > they usually want to see -1 displayed as FFFFFFFF, It depends. E.g. people who are new to programming probably would not expect anything, because it will be the first time they deal with hex numbers, and they just learn what they see. I agree that more experienced programmers, printing numbers with %x format, probably want to see -1 displayed as FFFFFFFF. But this people probably also want Euphoria to consider #FFFFFFFF to be equal to -1! So most important IMHO: Neither newbies nor 'oldbies' do like inconsistencies! Especially not in a programming language, which is for them a tool that should help them to perform *logical* operations.
include get.e type int_32bit (object x) -- 32-bit integer (signed or unsigned) if atom(x) then if x = floor(x) and -#80000000 <= x and x <= #FFFFFFFF then return 1 end if end if return 0 end type function xconvert (int_32bit a) sequence ret ret = value(sprintf("#%x", a)) if ret[1] != GET_SUCCESS then puts(1, "Error concerning value(), program terminated!") abort(1) end if return ret[2] end function int_32bit a, v a = -1 puts(1, "Handling of hexadecimal numbers (Eu 2.4):\n") v = xconvert(a) if v = a then printf(1, "%d = %d\n", {v, a}) puts(1, "consistent behaviour ") else printf(1, "%d != %d\n", {v, a}) puts(1, "inconsistent behaviour ") end if
For any negative value of 'a', this test will show inconsistent behaviour. Except of backward compatibility, I can't see any reason why this behaviour should be maintained. > so I don't plan to change it. > Just as people have made their own versions of pretty_print(), > there's no reason someone couldn't make an enhanced version > of printf(), with lots of fancy new % formats. Very few programs > require a really fast printf(). I know, but that was not my point. Newbies are probably not able to make their own version of printf(). Also, I (and other people, too) want their Eu code to be compatible with the rest of the Eu world. What I wrote was neither a request for a feature that I just want privately, nor a wish for a "fancy new % format", but a serious suggestion for improvement of Euphoria in general. If you don't want to implement it, I'll accept it. But please recognize the difference. Regards, Juergen -- /"\ ASCII ribbon campain | This message has been ROT-13 encrypted \ / against HTML in | twice for higher security. X e-mail and news, | / \ and unneeded MIME | http://home.arcor.de/luethje/prog/