mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Fix 'nan'='nan' evaluates to false in expressions
Also fix 'nan' and 'inf' being treated as doubles rather than strings.
This commit is contained in:
parent
8c538218f9
commit
a1a8d1b7a3
@ -176,7 +176,13 @@ inline bool isDoubleSafe( const QVariant& v )
|
||||
if ( v.type() == QVariant::UInt ) return true;
|
||||
if ( v.type() == QVariant::LongLong ) return true;
|
||||
if ( v.type() == QVariant::ULongLong ) return true;
|
||||
if ( v.type() == QVariant::String ) { bool ok; v.toString().toDouble( &ok ); return ok; }
|
||||
if ( v.type() == QVariant::String )
|
||||
{
|
||||
bool ok;
|
||||
double val = v.toString().toDouble( &ok );
|
||||
ok = ok && qIsFinite( val ) && !qIsNaN( val );
|
||||
return ok;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -239,7 +245,7 @@ static double getDoubleValue( const QVariant& value, QgsExpression* parent )
|
||||
{
|
||||
bool ok;
|
||||
double x = value.toDouble( &ok );
|
||||
if ( !ok )
|
||||
if ( !ok || qIsNaN( x ) || !qIsFinite( x ) )
|
||||
{
|
||||
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to double" ).arg( value.toString() ) );
|
||||
return 0;
|
||||
|
@ -202,7 +202,10 @@ class TestQgsExpression: public QObject
|
||||
QTest::newRow( "plus double" ) << "1+1.3" << false << QVariant( 2.3 );
|
||||
QTest::newRow( "plus with null" ) << "null+3" << false << QVariant();
|
||||
QTest::newRow( "plus invalid" ) << "1+'foo'" << true << QVariant();
|
||||
|
||||
QTest::newRow( "minus int" ) << "1-3" << false << QVariant( -2 );
|
||||
QTest::newRow( "minus nan" ) << "1-'nan'" << true << QVariant();
|
||||
QTest::newRow( "minus inf" ) << "1-'inf'" << true << QVariant();
|
||||
QTest::newRow( "mul int" ) << "8*7" << false << QVariant( 56 );
|
||||
QTest::newRow( "div int" ) << "5/2" << false << QVariant( 2.5 );
|
||||
QTest::newRow( "mod int" ) << "20%6" << false << QVariant( 2 );
|
||||
@ -238,6 +241,10 @@ class TestQgsExpression: public QObject
|
||||
QTest::newRow( "ge int 2" ) << "3 >= 3" << false << QVariant( 1 );
|
||||
QTest::newRow( "lt text 1" ) << "'bar' < 'foo'" << false << QVariant( 1 );
|
||||
QTest::newRow( "lt text 2" ) << "'foo' < 'bar'" << false << QVariant( 0 );
|
||||
QTest::newRow( "'nan'='nan'" ) << "'nan'='nan'" << false << QVariant( 1 );
|
||||
QTest::newRow( "'nan'='x'" ) << "'nan'='x'" << false << QVariant( 0 );
|
||||
QTest::newRow( "'inf'='inf'" ) << "'inf'='inf'" << false << QVariant( 1 );
|
||||
QTest::newRow( "'inf'='x'" ) << "'inf'='x'" << false << QVariant( 0 );
|
||||
|
||||
// is, is not
|
||||
QTest::newRow( "is null,null" ) << "null is null" << false << QVariant( 1 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user