SQLite

Check-in [44dd6328]
Login

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

Overview
Comment:A second approach to working on the round() function so that it gives a small boost to numbers that are ...49999999 such that they round up.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | round-up-2
Files: files | file ages | folders
SHA3-256: 44dd632896e688a7d73707f43261577b237628a587800b94f1b77d3ab0cedc2e
User & Date: drh 2024-06-11 22:47:33
Context
2024-06-12
00:30
Test cases for the round() function for values within one epsilon of the 5 round-up threshold. (check-in: 552b1b10 user: drh tags: round-up-2)
2024-06-11
22:47
A second approach to working on the round() function so that it gives a small boost to numbers that are ...49999999 such that they round up. (check-in: 44dd6328 user: drh tags: round-up-2)
17:04
Remove some dead JS code and update some JS docs. (check-in: 6935ac71 user: stephan tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/func.c.

431
432
433
434
435
436
437






















438
439
440
441
442
443
444
445



446
447


448
449
450
451



452
453
454
455
456
457
458
459
460
461

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
      p2 = len-p1;
      if( p2<0 ) p2 = 0;
    }
    sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
  }
}























/*
** Implementation of the round() function
*/
#ifndef SQLITE_OMIT_FLOATING_POINT
static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  int n = 0;
  double r;
  char *zBuf;



  assert( argc==1 || argc==2 );
  if( argc==2 ){


    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
    n = sqlite3_value_int(argv[1]);
    if( n>30 ) n = 30;
    if( n<0 ) n = 0;



  }
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  r = sqlite3_value_double(argv[0]);
  /* If Y==0 and X will fit in a 64-bit int,
  ** handle the rounding directly,
  ** otherwise use printf.
  */
  if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
    /* The value has no fractional part so there is nothing to round */
  }else if( n==0 ){ 

    r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
  }else{
    zBuf = sqlite3_mprintf("%!.*f",n,r);
    if( zBuf==0 ){
      sqlite3_result_error_nomem(context);
      return;
    }
    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
    sqlite3_free(zBuf);
  }
  sqlite3_result_double(context, r);
}
#endif

/*
** Allocate nByte bytes of space using sqlite3Malloc(). If the
** allocation fails, call sqlite3_result_error_nomem() to notify







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|
|
|
>
>
>


>
>




>
>
>



<
<
<
<
<
<
|
>
|

|
<
<
<
|
<
|
<







431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484






485
486
487
488
489



490

491

492
493
494
495
496
497
498
      p2 = len-p1;
      if( p2<0 ) p2 = 0;
    }
    sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
  }
}

/*
** The library round() function is only available if
** SQLITE_ENABLE_MATH_FUNCTIONS is defined.  Without that macro, we
** have to grow our own.
**
** The sqlite3Round(x) routine only needs to deal with non-negative
** numbers.
*/
#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
# define sqlite3Round(X) round(X)
#else
static double sqlite3Round(double x){
  assert( x>=0.0 );
  if( x>+4503599627370496.0 ){
    return x;
  }else{
    sqlite3_int64 ii = (sqlite3_int64)(x+0.5);
    return (double)ii;
  }
}
#endif

/*
** Implementation of the round() function
*/
#ifndef SQLITE_OMIT_FLOATING_POINT
static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  int n = 0;        /* Second argument. Digits to the right of decimal point */
  double r;         /* First argument.  Value to be rounded */
  double rX = 1.0;  /* Scaling factor.  pow(10,n) */
  double rSgn;      /* Sign of the first first */
  static const double rTwoPowerMinus52 =  /* pow(2,-52) */
                           2.220446049250313080847263336181640625e-16;
  assert( argc==1 || argc==2 );
  if( argc==2 ){
    double rY = 10;
    int i;
    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
    n = sqlite3_value_int(argv[1]);
    if( n>30 ) n = 30;
    if( n<0 ) n = 0;
    for(i=n, rY=10; i>0; i>>=1, rY=rY*rY){
      if( i&1 ) rX *= rY;
    }
  }
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  r = sqlite3_value_double(argv[0]);






  if( r<0 ){
    rSgn = -1.0;
    r = -r;
  }else{
    rSgn = 1.0;



  }

  r = rSgn*sqlite3Round(r*rX + rX*r*rTwoPowerMinus52)/rX;

  sqlite3_result_double(context, r);
}
#endif

/*
** Allocate nByte bytes of space using sqlite3Malloc(). If the
** allocation fails, call sqlite3_result_error_nomem() to notify
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
  int argc,
  sqlite3_value **argv
){
  assert( argc==0 );
  (void)argv;
  sqlite3_result_double(context, M_PI);
}

#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */

/*
** Implementation of sign(X) function.
*/
static void signFunc(
  sqlite3_context *context,







<







2521
2522
2523
2524
2525
2526
2527

2528
2529
2530
2531
2532
2533
2534
  int argc,
  sqlite3_value **argv
){
  assert( argc==0 );
  (void)argv;
  sqlite3_result_double(context, M_PI);
}

#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */

/*
** Implementation of sign(X) function.
*/
static void signFunc(
  sqlite3_context *context,