3 ms·
Man, if I had a nickel every time I saw an itoa/ltoa/lltoa implementation that doesn't work on the most negative number I'd have about $10 or so, I think. The
by Joker_vD 2y ago
Man, if I had a nickel every time I saw an itoa/ltoa/lltoa implementation that doesn't work on the most negative number I'd have about $10 or so, I think.
The annyoing thing about it is that all the workarounds I know about are really ain't that pretty:
1. You can hard-code the check against it and return a hardcoded string representation of it:
if (number == -9223372036854775808) return "-9223372036854775808";
By the way, "(number && (number == -number))" condition doesn't work so don't try to be too smart about it: just compare against INT_MIN/LONG_MIN/etc.
2. You can widen the numeric type, and do the conversion in the larger integer width, but it doesn't really work for intmax_t and it's, of course, is slower. Alternatively, you can perform only the first iteration in the widened arithmetic, and do the rest in the original width, but this leads to some code duplication.
2a. You can do
unsigned unumber = number;
if (number < 0) unumber = -unumber;
and convert the unsigned number instead. Again, you can chose to do only the first iteration in the unsigned, on platforms where unsigned multiplication/division is slower than signed ones. Oh, and again, beware that "unsigned unumber = number < 0 ? -number : number" way of conversion doesn't work.
3. You can, instead of turning the negative numbers into positive ones and working with the positive numbers, do the opposite: turn positive numbers into negative ones and work exclusively with the negative numbers. Such conversion is safe, and division in C is required to truncate to zero, so it all works out fine except for the fact that the remainders will be negative; you'll have to deal with that.
But yeah, converting integers into strings is surprisingly slow; not as slow as converting floats, but still very noticeable. Maybe BCDs weren't such a silly idea, after all...
- nick__m 2y agoBCD for money like datatype makes a lot's of sense but there a strange lack of support for it in x64 (while the support in x86 was so anemic as to be almost useless). Easy to implement correct low-level financial math operations seems to be reserved to the mainframe! Does anyone know why Intel publish a DFP (decimal floating point) library instead of pushing those instructions down to the microcode level like the mainframes do ?
- KerrAvon 2y agoI don’t specifically know Intel’s thinking, but the usual conclusion from CPU designers is that decimal instructions are so rarely used that it’s not worth allocating silicon budget to them.
- adgjlsfhk1 2y agoprobably because in the GHz era none of this has to be fast anyway. financial programs will be spending all their time waiting for disk or network anyway. it's pretty hard to imagine a program that needs to care about decimal floating point in finance that would be slow at a human scale as a result of doing this in software