diff options
author | shaneh <shaneh@noemail.net> | 2010-02-17 04:19:27 +0000 |
---|---|---|
committer | shaneh <shaneh@noemail.net> | 2010-02-17 04:19:27 +0000 |
commit | 147e176affed4dc2377eaa24dfee4fa2b3004198 (patch) | |
tree | 9c9ef1aadec08d5e791b26b7558da6820210cc59 /src/func.c | |
parent | 35c1a793ce5a08da2b26c99bdb7cac3813892afe (diff) | |
download | sqlite-147e176affed4dc2377eaa24dfee4fa2b3004198.tar.gz sqlite-147e176affed4dc2377eaa24dfee4fa2b3004198.zip |
Avoid using the internal printf routine for round(x,y) in the common case where y==0.
FossilOrigin-Name: d76ad8b3c494ffb4e670da0e92a1f8dbf7f48daf
Diffstat (limited to 'src/func.c')
-rw-r--r-- | src/func.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/func.c b/src/func.c index c1d3e9060..d633ff83c 100644 --- a/src/func.c +++ b/src/func.c @@ -271,14 +271,24 @@ static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ } if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; r = sqlite3_value_double(argv[0]); - zBuf = sqlite3_mprintf("%.*f",n,r); - if( zBuf==0 ){ - sqlite3_result_error_nomem(context); + /* If Y==0 and X will fit in a 64-bit int, + ** handle the rounding directly, + ** otherwise use printf. + */ + if( n==0 && r>=0 && r<LARGEST_INT64-1 ){ + r = (double)((sqlite_int64)(r+0.5)); + }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){ + r = -(double)((sqlite_int64)((-r)+0.5)); }else{ + zBuf = sqlite3_mprintf("%.*f",n,r); + if( zBuf==0 ){ + sqlite3_result_error_nomem(context); + return; + } sqlite3AtoF(zBuf, &r); sqlite3_free(zBuf); - sqlite3_result_double(context, r); } + sqlite3_result_double(context, r); } #endif |