Make QString("inf").toFloat() return inf instead of zero.
Currently, QString::toFloat() returns 0 (and sets ok to false) if you try to convert "inf". This is because inf is greater than QT_MAX_FLOAT and there is currently no check to handle inf. Task-number: QTBUG-8629 Change-Id: I498daf4a7a6f880f928461fca628fcaf7d1d6d08 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
parent
93ead35939
commit
872f0b94ac
@ -48,6 +48,7 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <private/qutfcodec_p.h>
|
#include <private/qutfcodec_p.h>
|
||||||
#include "qsimd_p.h"
|
#include "qsimd_p.h"
|
||||||
|
#include <qnumeric.h>
|
||||||
#include <qdatastream.h>
|
#include <qdatastream.h>
|
||||||
#include <qlist.h>
|
#include <qlist.h>
|
||||||
#include "qlocale.h"
|
#include "qlocale.h"
|
||||||
@ -6051,14 +6052,21 @@ float QString::toFloat(bool *ok) const
|
|||||||
{
|
{
|
||||||
bool myOk;
|
bool myOk;
|
||||||
double d = toDouble(&myOk);
|
double d = toDouble(&myOk);
|
||||||
if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
|
if (!myOk) {
|
||||||
|
if (ok != 0)
|
||||||
|
*ok = false;
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
if (qIsInf(d))
|
||||||
|
return float(d);
|
||||||
|
if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
|
||||||
if (ok != 0)
|
if (ok != 0)
|
||||||
*ok = false;
|
*ok = false;
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
if (ok != 0)
|
if (ok != 0)
|
||||||
*ok = true;
|
*ok = true;
|
||||||
return (float) d;
|
return float(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \fn QString &QString::setNum(int n, int base)
|
/*! \fn QString &QString::setNum(int n, int base)
|
||||||
|
@ -4850,6 +4850,21 @@ void tst_QString::nanAndInf()
|
|||||||
|
|
||||||
QString("0INF0").toLong(&ok, 36);
|
QString("0INF0").toLong(&ok, 36);
|
||||||
QVERIFY(ok);
|
QVERIFY(ok);
|
||||||
|
|
||||||
|
// Check that inf (float) => "inf" (QString) => inf (float).
|
||||||
|
float value = qInf();
|
||||||
|
QString valueAsString = QString::number(value);
|
||||||
|
QCOMPARE(valueAsString, QString::fromLatin1("inf"));
|
||||||
|
float valueFromString = valueAsString.toFloat();
|
||||||
|
QVERIFY(qIsInf(valueFromString));
|
||||||
|
|
||||||
|
// Check that -inf (float) => "-inf" (QString) => -inf (float).
|
||||||
|
value = -qInf();
|
||||||
|
valueAsString = QString::number(value);
|
||||||
|
QCOMPARE(valueAsString, QString::fromLatin1("-inf"));
|
||||||
|
valueFromString = valueAsString.toFloat();
|
||||||
|
QVERIFY(value == -qInf());
|
||||||
|
QVERIFY(qIsInf(valueFromString));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QString::arg_fillChar_data()
|
void tst_QString::arg_fillChar_data()
|
||||||
|
Loading…
Reference in New Issue
Block a user