Right way to show numbers in binary/hex/octal in your opinion?
Rumbu
rumbu at rumbu.ro
Mon Dec 27 12:48:52 UTC 2021
On Monday, 27 December 2021 at 09:55:46 UTC, Siarhei Siamashka
wrote:
> On Monday, 27 December 2021 at 06:55:37 UTC, Rumbu wrote:
>> When people are dumping numbers to strings în any other base
>> than 10, they are expecting to see the internal representation
>> of that number.
>
> Different people may have different expectations and their
> expectations may be not the same.
Your expectations must be congruent with the host architecture,
otherwise you can have surprises (like the ones in phobos). The
architecture has a limited domain and a certain way to represent
numbers, they are not infinite. Otherwise computers should
perform math ops using strings and you don't want that for
performance reasons.
> I don't understand what's the problem here. It can be easily
> solved by having a unit test, which verifies that "-0x80" gets
> correctly converted to -128. Or have I missed something?
How can you convert 0x8000_0000_0000_0000 to long?
And if your response is "use a ulong", I have another one: how do
you convert -0x8000_0000_0000_0000 to ulong.
>
>> This is also an issue în phobos:
>>
>> https://issues.dlang.org/show_bug.cgi?id=20452
>> https://issues.dlang.org/show_bug.cgi?id=18290
>
> To me this looks very much like just a self inflicted damage
> and historical baggage, entirely caused by making wrong choices
> in the past.
No, it's just the fact that phobos doesn't use the same
convention for both senses of conversion. When converting from
number to string, it uses the internal representation - 2's
complement. When it is converting from string to number, it uses
the "human readable" convention.
More information about the Digitalmars-d
mailing list