/ Check-in [d76ad8b3]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Avoid using the internal printf routine for round(x,y) in the common case where y==0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d76ad8b3c494ffb4e670da0e92a1f8dbf7f48daf
User & Date: shaneh 2010-02-17 04:19:27
Context
2010-02-17
17:48
Consistent use of #ifdef for SQLITE_HAS_CODEC to avoid confusion. check-in: 34a3413a user: shaneh tags: trunk
04:19
Avoid using the internal printf routine for round(x,y) in the common case where y==0. check-in: d76ad8b3 user: shaneh tags: trunk
03:57
More rounding tests. check-in: 3863638b user: shaneh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/func.c.

   267    267       if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
   268    268       n = sqlite3_value_int(argv[1]);
   269    269       if( n>30 ) n = 30;
   270    270       if( n<0 ) n = 0;
   271    271     }
   272    272     if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   273    273     r = sqlite3_value_double(argv[0]);
   274         -  zBuf = sqlite3_mprintf("%.*f",n,r);
   275         -  if( zBuf==0 ){
   276         -    sqlite3_result_error_nomem(context);
          274  +  /* If Y==0 and X will fit in a 64-bit int,
          275  +  ** handle the rounding directly,
          276  +  ** otherwise use printf.
          277  +  */
          278  +  if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
          279  +    r = (double)((sqlite_int64)(r+0.5));
          280  +  }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
          281  +    r = -(double)((sqlite_int64)((-r)+0.5));
   277    282     }else{
          283  +    zBuf = sqlite3_mprintf("%.*f",n,r);
          284  +    if( zBuf==0 ){
          285  +      sqlite3_result_error_nomem(context);
          286  +      return;
          287  +    }
   278    288       sqlite3AtoF(zBuf, &r);
   279    289       sqlite3_free(zBuf);
   280         -    sqlite3_result_double(context, r);
   281    290     }
          291  +  sqlite3_result_double(context, r);
   282    292   }
   283    293   #endif
   284    294   
   285    295   /*
   286    296   ** Allocate nByte bytes of space using sqlite3_malloc(). If the
   287    297   ** allocation fails, call sqlite3_result_error_nomem() to notify
   288    298   ** the database handle that malloc() has failed and return NULL.