[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] strtod("inf") and MSVC
From: |
Vadim Zeitlin |
Subject: |
Re: [lmi] strtod("inf") and MSVC |
Date: |
Thu, 31 May 2012 20:38:24 +0200 |
On Thu, 31 May 2012 17:37:31 +0000 Greg Chicares <address@hidden> wrote:
GC> You anticipated the case-sensitivity issue:
Yes (I'm replying to this as this part actually dates from my original
patch from 2011-06-27 email), the patch was really the smallest change
needed to make LMI work when compiled with MSVC, it was not supposed to be
exhaustive.
GC> and one thing led to another, and now I have a patch that converts all
GC> infinities for como, and I'd guess for msvc too; but does it look
GC> right, and is there a less awful way to write it that isn't slower?
I suspect that writing comparisons char-by-char, i.e. (warning: untested
pseudo-code follows)
(nptr[0] == 'i' || nptr[0] == 'I') &&
(nptr[1] == 'n' || nptr[1] == 'N') &&
(nptr[2] == 'f' || nptr[2] == 'F') &&
(nptr[3] == '\0' ||
(nptr[3] == 'i' || nptr[3] == 'I') &&
...
(nptr[7] == 'y' || nptr[7] == 'Y'))
might be actually the fastest way to do it but I didn't benchmark it so I'm
not sure about this.
But IMHO getting rid of duplicate "-foo" and "foo" comparisons would make
the code more clear so why not write it like this (same warning as above):
bool negative = false;
if(*nptr == '-')
{
negative = true;
nptr++;
}
if
(
std::strcmp(nptr, "inf") == 0
|| std::strcmp(nptr, "INF") == 0)
{
nptr += 3;
}
else if
(
std::strcmp(nptr, "infinity") == 0
|| std::strcmp(nptr, "INFINITY") == 0)
{
nptr += 8;
}
else
{
throw std::invalid_argument("Numeric conversion failed.");
}
if(endptr)
{
*endptr = const_cast<char *>(nptr);
}
return negative
? -std::numeric_limits<T>::infinity()
: std::numeric_limits<T>::infinity()
;
?
Granted, it's not a huge improvement but it still looks a bit more clear
to me.
Regards,
VZ