Skip to content
Permalink
Browse files

Expressions: add exp,ln,log10,log functions (#4365)

  • Loading branch information
wonder-sk committed Oct 11, 2011
1 parent 30e4892 commit 05912884f9ba98ee5abf720b067dd3095cb0e4eb
Showing with 39 additions and 0 deletions.
  1. +31 −0 src/core/qgsexpression.cpp
  2. +8 −0 tests/src/core/testqgsexpression.cpp
@@ -208,6 +208,33 @@ static QVariant fcnAtan2( const QVariantList& values, QgsFeature* , QgsExpressio
double x = getDoubleValue( values.at( 1 ), parent );
return QVariant( atan2( y, x ) );
}
static QVariant fcnExp( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
return QVariant( exp( x ) );
}
static QVariant fcnLn( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
if ( x <= 0 )
return QVariant();
return QVariant( log( x ) );
}
static QVariant fcnLog10( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
if ( x <= 0 )
return QVariant();
return QVariant( log10( x ) );
}
static QVariant fcnLog( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double b = getDoubleValue( values.at( 0 ), parent );
double x = getDoubleValue( values.at( 1 ), parent );
if ( x <= 0 || b <= 0 )
return QVariant();
return QVariant( log( x ) / log( b ) );
}
static QVariant fcnToInt( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
return QVariant( getIntValue( values.at( 0 ), parent ) );
@@ -356,6 +383,10 @@ FnDef QgsExpression::BuiltinFunctions[] =
FnDef( "acos", 1, fcnAcos ),
FnDef( "atan", 1, fcnAtan ),
FnDef( "atan2", 2, fcnAtan2 ),
FnDef( "exp", 1, fcnExp ),
FnDef( "ln", 1, fcnLn ),
FnDef( "log10", 1, fcnLog10 ),
FnDef( "log", 2, fcnLog ),
// casts
FnDef( "toint", 1, fcnToInt ),
FnDef( "toreal", 1, fcnToReal ),
@@ -192,6 +192,14 @@ class TestQgsExpression: public QObject
QTest::newRow( "atan 0" ) << "atan(0)" << false << QVariant( 0. );
QTest::newRow( "atan2(0,1)" ) << "atan2(0,1)" << false << QVariant( 0. );
QTest::newRow( "atan2(1,0)" ) << "atan2(1,0)" << false << QVariant( M_PI / 2 );
QTest::newRow( "exp(0)" ) << "exp(0)" << false << QVariant( 1. );
QTest::newRow( "exp(1)" ) << "exp(1)" << false << QVariant( exp(1) );
QTest::newRow( "ln(0)" ) << "ln(0)" << false << QVariant();
QTest::newRow( "log10(-1)" ) << "log10(-1)" << false << QVariant();
QTest::newRow( "ln(1)" ) << "ln(1)" << false << QVariant( log(1) );
QTest::newRow( "log10(100)" ) << "log10(100)" << false << QVariant( 2. );
QTest::newRow( "log(2,32)" ) << "log(2,32)" << false << QVariant( 5. );
QTest::newRow( "log(10,1000)" ) << "log(10,1000)" << false << QVariant( 3. );

// cast functions
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );

0 comments on commit 0591288

Please sign in to comment.
You can’t perform that action at this time.