Add the decimal_sci(X) function to the Decimal extension for showing a decimal

value in scientific notation:  +D.DDDDDe+DD

FossilOrigin-Name: 61d4923913e88b980ce93db4f3f9f9d7ba3baaac724995c36c9b887c034accdc
This commit is contained in:
drh 2023-06-29 14:49:23 +00:00
parent 7f5fe1faf3
commit 9ea7632cca
3 changed files with 84 additions and 7 deletions

View File

@ -230,6 +230,67 @@ static void decimal_result(sqlite3_context *pCtx, Decimal *p){
sqlite3_result_text(pCtx, z, i, sqlite3_free);
}
/*
** Make the given Decimal the result in an format similar to '%+#e'.
** In other words, show exponential notation with leading and trailing
** zeros omitted.
*/
static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
char *z; /* The output buffer */
int i; /* Loop counter */
int nZero; /* Number of leading zeros */
int nDigit; /* Number of digits not counting trailing zeros */
int nFrac; /* Digits to the right of the decimal point */
int exp; /* Exponent value */
signed char zero; /* Zero value */
signed char *a; /* Array of digits */
if( p==0 || p->oom ){
sqlite3_result_error_nomem(pCtx);
return;
}
if( p->isNull ){
sqlite3_result_null(pCtx);
return;
}
for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
nFrac = p->nFrac + (nDigit - p->nDigit);
nDigit -= nZero;
z = sqlite3_malloc( nDigit+20 );
if( z==0 ){
sqlite3_result_error_nomem(pCtx);
return;
}
if( nDigit==0 ){
zero = 0;
a = &zero;
nDigit = 1;
nFrac = 0;
}else{
a = &p->a[nZero];
}
if( p->sign && nDigit>0 ){
z[0] = '-';
}else{
z[0] = '+';
}
z[1] = a[0]+'0';
z[2] = '.';
if( nDigit==1 ){
z[3] = '0';
i = 4;
}else{
for(i=1; i<nDigit; i++){
z[2+i] = a[i]+'0';
}
i = nDigit+2;
}
exp = nDigit - nFrac - 1;
sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
sqlite3_result_text(pCtx, z, -1, sqlite3_free);
}
/*
** SQL Function: decimal(X)
**
@ -591,6 +652,21 @@ mul_end:
decimal_free(pB);
}
/*
** SQL Function: decimal_sci(X)
**
** Convert decimal number X into scientific notation ("+N.NNNe+NN").
*/
static void decimalSciFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
Decimal *pA = decimal_new(context, argv[0], 0, 0);
decimal_result_sci(context, pA);
decimal_free(pA);
}
#ifdef _WIN32
__declspec(dllexport)
#endif
@ -610,6 +686,7 @@ int sqlite3_decimal_init(
{ "decimal_add", 2, decimalAddFunc },
{ "decimal_sub", 2, decimalSubFunc },
{ "decimal_mul", 2, decimalMulFunc },
{ "decimal_sci", 1, decimalSciFunc },
};
unsigned int i;
(void)pzErrMsg; /* Unused parameter */

View File

@ -1,5 +1,5 @@
C CLI\senhancements\sto\sfacilitate\sSQLite\score\stesting:\n(1)\sAdd\sbuilt-in\sfunctions\sstrtod()\sand\sdtostr()\sthat\sconvert\stext\sto\nfloating\spoint\sand\sback\susing\sC-library\sroutines.\n(2)\sDo\snot\sdisable\sall\sof\s".testctrl"\swithout\s--unsafe-testing,\sbut\sonly\nthose\ssubcommands\sof\s.testctrl\sthat\sare\sactually\sdangerous.
D 2023-06-29T12:14:10.203
C Add\sthe\sdecimal_sci(X)\sfunction\sto\sthe\sDecimal\sextension\sfor\sshowing\sa\sdecimal\nvalue\sin\sscientific\snotation:\s\s+D.DDDDDe+DD
D 2023-06-29T14:49:23.326
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -287,7 +287,7 @@ F ext/misc/completion.c 6dafd7f4348eecc7be9e920d4b419d1fb2af75d938cd9c59a20cfe8b
F ext/misc/compress.c 3354c77a7c8e86e07d849916000cdac451ed96500bfb5bd83b20eb61eee012c9
F ext/misc/csv.c ca8d6dafc5469639de81937cb66ae2e6b358542aba94c4f791910d355a8e7f73
F ext/misc/dbdump.c b8592f6f2da292c62991a13864a60d6c573c47a9cc58362131b9e6a64f823e01
F ext/misc/decimal.c 57d85fa20a5a74d3b0dfc78ab7934ae6c9f5aa8eed915faa2b5246bec87ddc6d
F ext/misc/decimal.c 17303ac06f50d750fdb635c8be1f8eb820fa95896fd08c08af332b62fe8bcfce
F ext/misc/eval.c 04bc9aada78c888394204b4ed996ab834b99726fb59603b0ee3ed6e049755dc1
F ext/misc/explain.c 0086fab288d4352ea638cf40ac382aad3b0dc5e845a1ea829a694c015fd970fe
F ext/misc/fileio.c 4e7f7cd30de8df4820c552f14af3c9ca451c5ffe1f2e7bef34d598a12ebfb720
@ -2041,8 +2041,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P a915f15a916af698e0cef46c8b3e7ed11bda19349179d2d414073cd39c4cce24
R 0ff1aaf83c3a4b2103ba6e2bba60a2a8
P 669996a8ddcbf35f3de66cf466508fc1e6dd09ab269aba395ac86a11b2ec238c
R 090cbc96cc5b169f2dbe66c47db5bae8
U drh
Z f9cb0e945d7f18774016bacc44534d0c
Z d52f599d2e14938fb14fb03c95733f3a
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
669996a8ddcbf35f3de66cf466508fc1e6dd09ab269aba395ac86a11b2ec238c
61d4923913e88b980ce93db4f3f9f9d7ba3baaac724995c36c9b887c034accdc