Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Many more math functions. Semantics follows PG wherever possible. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
6b93627b5d9819abf179a3e4a82e7afe |
User & Date: | drh 2020-12-07 21:13:06 |
Context
2020-12-07
| ||
21:19 | Fix to the --disable-math option to ./configure. (check-in: 99ff6418 user: drh tags: trunk) | |
21:13 | Many more math functions. Semantics follows PG wherever possible. (check-in: 6b93627b user: drh tags: trunk) | |
17:15 | Begin adding new SQL functions that depend on -lm: ceil(), ceiling(), floor(), ln(), log(), and log10() so far. More to follow. (check-in: 4db5f2f7 user: drh tags: trunk) | |
Changes
Changes to src/func.c.
︙ | ︙ | |||
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 | if( zEscape[0]==aWc[1] ) return 0; aWc[3] = zEscape[0]; } *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; return 1; } /* Extra math functions that require linking with -lm */ #ifdef SQLITE_ENABLE_MATH_FUNCTIONS /* ** Implementation SQL functions: ** ** ceil(X) ** ceiling(X) ** floor(X) ** ** The sqlite3_user_data() pointer is a pointer to the libm implementation ** of the underlying C function. */ static void ceilingFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==1 ); | > > > > > > > > > > > > | | > | < < > > > | | | | | > > | | > > | < | > > > | | > > | > > | | > | > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 | if( zEscape[0]==aWc[1] ) return 0; aWc[3] = zEscape[0]; } *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; return 1; } /* Mathematical Constants */ #ifndef M_PI # define M_PI 3.141592653589793238462643383279502884 #endif #ifndef M_LN10 # define M_LN10 2.302585092994045684017991454684364208 #endif #ifndef M_LN2 # define M_LN2 0.693147180559945309417232121458176568 #endif /* Extra math functions that require linking with -lm */ #ifdef SQLITE_ENABLE_MATH_FUNCTIONS /* ** Implementation SQL functions: ** ** ceil(X) ** ceiling(X) ** floor(X) ** ** The sqlite3_user_data() pointer is a pointer to the libm implementation ** of the underlying C function. */ static void ceilingFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==1 ); switch( sqlite3_value_numeric_type(argv[0]) ){ case SQLITE_INTEGER: { sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); break; } case SQLITE_FLOAT: { double (*x)(double) = (double(*)(double))sqlite3_user_data(context); sqlite3_result_double(context, x(sqlite3_value_double(argv[0]))); break; } default: { break; } } } /* ** Implementation of SQL functions: ** ** ln(X) - natural logarithm ** log(X) - log X base 10 ** log10(X) - log X base 10 ** log(B,X) - log X base B */ static void logFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ double x, b, ans; assert( argc==1 || argc==2 ); switch( sqlite3_value_numeric_type(argv[0]) ){ case SQLITE_INTEGER: case SQLITE_FLOAT: x = sqlite3_value_double(argv[0]); if( x<0.0 ) return; break; default: return; } if( argc==2 ){ switch( sqlite3_value_numeric_type(argv[0]) ){ case SQLITE_INTEGER: case SQLITE_FLOAT: b = x; x = sqlite3_value_double(argv[1]); if( x<0.0 ) return; break; default: return; } ans = log(x)/log(b); }else{ ans = log(x); switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){ case 1: /* Convert from natural logarithm to log base 10 */ ans *= 1.0/M_LN10; break; case 2: /* Convert from natural logarithm to log base 2 */ ans *= 1.0/M_LN2; break; default: break; } } sqlite3_result_double(context, ans); } /* ** Functions to converts degrees to radians and radians to degrees. */ static double degToRad(double x){ return x*(M_PI/180.0); } static double radToDeg(double x){ return x*(180.0/M_PI); } /* ** Implementation of 1-argument SQL math functions: ** ** exp(X) - Compute e to the X-th power */ static void math1Func( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==2 ); int type0; double v0, ans; double (*x)(double); type0 = sqlite3_value_numeric_type(argv[0]); if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; v0 = sqlite3_value_double(argv[0]); x = (double(*)(double))sqlite3_user_data(context); ans = x(v0); sqlite3_result_double(context, ans); } /* ** Implementation of 2-argument SQL math functions: ** ** power(X,Y) - Compute X to the Y-th power */ static void math2Func( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==2 ); int type0, type1; double v0, v1, ans; double (*x)(double,double); type0 = sqlite3_value_numeric_type(argv[0]); if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; type1 = sqlite3_value_numeric_type(argv[1]); if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return; v0 = sqlite3_value_double(argv[0]); v1 = sqlite3_value_double(argv[1]); x = (double(*)(double,double))sqlite3_user_data(context); ans = x(v0, v1); sqlite3_result_double(context, ans); } /* ** Implementation of 2-argument SQL math functions: ** ** power(X,Y) - Compute X to the Y-th power */ static void piFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==0 ); sqlite3_result_double(context, M_PI); } #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ /* ** Implementation of sign(X) function. */ static void signFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ assert( argc==1 ); int type0; double x; type0 = sqlite3_value_numeric_type(argv[0]); if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; x = sqlite3_value_double(argv[0]); sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); } /* ** All of the FuncDef structures in the aBuiltinFunc[] array above ** to the global function hash table. This occurs at start-time (as ** a consequence of calling sqlite3_initialize()). ** ** After this routine runs |
︙ | ︙ | |||
2087 2088 2089 2090 2091 2092 2093 | LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), #endif #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION FUNCTION(unknown, -1, 0, 0, unknownFunc ), #endif FUNCTION(coalesce, 1, 0, 0, 0 ), FUNCTION(coalesce, 0, 0, 0, 0 ), | < < > > > > > > > > > > > > > > > > > > > > > > > > > > | 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 | LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), #endif #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION FUNCTION(unknown, -1, 0, 0, unknownFunc ), #endif FUNCTION(coalesce, 1, 0, 0, 0 ), FUNCTION(coalesce, 0, 0, 0, 0 ), #ifdef SQLITE_ENABLE_MATH_FUNCTIONS MFUNCTION(ceil, 1, ceil, ceilingFunc ), MFUNCTION(ceiling, 1, ceil, ceilingFunc ), MFUNCTION(floor, 1, floor, ceilingFunc ), MFUNCTION(trunc, 1, trunc, ceilingFunc ), FUNCTION(ln, 1, 0, 0, logFunc ), FUNCTION(log, 1, 1, 0, logFunc ), FUNCTION(log10, 1, 1, 0, logFunc ), FUNCTION(log2, 1, 2, 0, logFunc ), FUNCTION(log, 2, 0, 0, logFunc ), MFUNCTION(exp, 1, exp, math1Func ), MFUNCTION(pow, 2, pow, math2Func ), MFUNCTION(power, 2, pow, math2Func ), MFUNCTION(mod, 2, fmod, math2Func ), MFUNCTION(acos, 1, acos, math1Func ), MFUNCTION(asin, 1, asin, math1Func ), MFUNCTION(atan, 1, atan, math1Func ), MFUNCTION(atan2, 2, atan2, math2Func ), MFUNCTION(cos, 1, cos, math1Func ), MFUNCTION(sin, 1, sin, math1Func ), MFUNCTION(tan, 1, tan, math1Func ), MFUNCTION(cosh, 1, cosh, math1Func ), MFUNCTION(sinh, 1, sinh, math1Func ), MFUNCTION(tanh, 1, tanh, math1Func ), MFUNCTION(acosh, 1, acosh, math1Func ), MFUNCTION(asinh, 1, asinh, math1Func ), MFUNCTION(atanh, 1, atanh, math1Func ), MFUNCTION(sqrt, 1, sqrt, math1Func ), MFUNCTION(radians, 1, degToRad, math1Func ), MFUNCTION(degrees, 1, radToDeg, math1Func ), FUNCTION(pi, 0, 0, 0, piFunc ), #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ FUNCTION(sign, 1, 0, 0, signFunc ), INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), }; #ifndef SQLITE_OMIT_ALTERTABLE sqlite3AlterFunctions(); #endif sqlite3WindowFunctions(); sqlite3RegisterDateTimeFunctions(); sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)); |
︙ | ︙ |
Changes to test/func7.test.
︙ | ︙ | |||
21 22 23 24 25 26 27 | do_execsql_test func7-100 { SELECT ceil(99.9), ceiling(-99.01), floor(17), floor(-17.99); } {100.0 -99.0 17 -18.0} do_execsql_test func7-110 { SELECT quote(ceil(NULL)), ceil('-99.99'); } {NULL -99.0} do_execsql_test func7-200 { | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | do_execsql_test func7-100 { SELECT ceil(99.9), ceiling(-99.01), floor(17), floor(-17.99); } {100.0 -99.0 17 -18.0} do_execsql_test func7-110 { SELECT quote(ceil(NULL)), ceil('-99.99'); } {NULL -99.0} do_execsql_test func7-200 { SELECT round(ln(5),2), log(100.0), log(100), log(2,'256'); } {1.61 2.0 2.0 8.0} do_execsql_test func7-210 { SELECT ln(-5), log(-5,100.0); } {{} {}} # Test cases derived from PostgreSQL documentation # do_execsql_test func7-pg-100 { SELECT abs(-17.4) } {17.4} do_execsql_test func7-pg-110 { SELECT ceil(42.2) } {43.0} do_execsql_test func7-pg-120 { SELECT ceil(-42.2) } {-42.0} do_execsql_test func7-pg-130 { SELECT round(exp(1.0),7) } {2.7182818} do_execsql_test func7-pg-140 { SELECT floor(42.8) } {42.0} do_execsql_test func7-pg-150 { SELECT floor(-42.8) } {-43.0} do_execsql_test func7-pg-160 { SELECT round(ln(2.0),7) } {0.6931472} do_execsql_test func7-pg-170 { SELECT log(100.0) } {2.0} do_execsql_test func7-pg-180 { SELECT log10(1000.0) } {3.0} do_execsql_test func7-pg-190 { SELECT log(2.0, 64.0) } {6.0} do_execsql_test func7-pg-200 { SELECT mod(9,4); } {1.0} do_execsql_test func7-pg-210 { SELECT round(pi(),7); } {3.1415927} do_execsql_test func7-pg-220 { SELECT power(9,3); } {729.0} do_execsql_test func7-pg-230 { SELECT round(radians(45.0),7); } {0.7853982} do_execsql_test func7-pg-240 { SELECT round(42.4); } {42.0} do_execsql_test func7-pg-250 { SELECT round(42.4382,2); } {42.44} do_execsql_test func7-pg-260 { SELECT sign(-8.4); } {-1} do_execsql_test func7-pg-270 { SELECT round( sqrt(2), 7); } {1.4142136} do_execsql_test func7-pg-280 { SELECT trunc(42.8), trunc(-42.8); } {42.0 -42.0} do_execsql_test func7-pg-300 { SELECT acos(1); } {0.0} do_execsql_test func7-pg-301 { SELECT degrees(acos(0.5)); } {60.0} do_execsql_test func7-pg-310 { SELECT round( asin(1), 7); } {1.5707963} do_execsql_test func7-pg-311 { SELECT degrees( asin(0.5) ); } {30.0} do_execsql_test func7-pg-320 { SELECT round( atan(1), 7); } {0.7853982} do_execsql_test func7-pg-321 { SELECT degrees( atan(1) ); } {45.0} do_execsql_test func7-pg-330 { SELECT round( atan2(1,0), 7); } {1.5707963} do_execsql_test func7-pg-331 { SELECT degrees( atan2(1,0) ); } {90.0} do_execsql_test func7-pg-400 { SELECT cos(0); } {1.0} do_execsql_test func7-pg-401 { SELECT cos( radians(60.0) ); } {0.5} do_execsql_test func7-pg-400 { SELECT cos(0); } {1.0} do_execsql_test func7-pg-410 { SELECT round( sin(1), 7); } {0.841471} do_execsql_test func7-pg-411 { SELECT sin( radians(30) ); } {0.5} do_execsql_test func7-pg-420 { SELECT round( tan(1), 7); } {1.5574077} do_execsql_test func7-pg-421 { SELECT tan( radians(45) ); } {1.0} do_execsql_test func7-pg-500 { SELECT round( sinh(1), 7); } {1.1752012} do_execsql_test func7-pg-510 { SELECT round( cosh(0), 7); } {1.0} do_execsql_test func7-pg-520 { SELECT round( tanh(1), 7); } {0.7615942} do_execsql_test func7-pg-530 { SELECT round( asinh(1), 7); } {0.8813736} do_execsql_test func7-pg-540 { SELECT round( acosh(1), 7); } {0.0} do_execsql_test func7-pg-550 { SELECT round( atanh(0.5), 7); } {0.5493061} # Test cases derived from MySQL documentation # do_execsql_test func7-mysql-100 { SELECT acos(1); } {0.0} do_execsql_test func7-mysql-110 { SELECT acos(1.0001); } {{}} do_execsql_test func7-mysql-120 { SELECT round( acos(0.0), 7); } {1.5707963} do_execsql_test func7-mysql-130 { SELECT round( asin(0.2), 7); } {0.2013579} do_execsql_test func7-mysql-140 { SELECT asin('foo'); } {{}} ;# Note: MySQL returns 0 here, not NULL. # SQLite deliberately returns NULL. # SQLServer and Oracle throw an error. do_execsql_test func7-mysql-150 { SELECT round( atan(2), 7), round( atan(-2), 7); } {1.1071487 -1.1071487} do_execsql_test func7-mysql-160 { SELECT round( atan2(-2,2), 7), round( atan2(pi(),0), 7); } {-0.7853982 1.5707963} do_execsql_test func7-mysql-170 { SELECT ceiling(1.23), ceiling(-1.23); } {2.0 -1.0} do_execsql_test func7-mysql-180 { SELECT cos(pi()); } {-1.0} do_execsql_test func7-mysql-190 { SELECT degrees(pi()), degrees(pi()/2); } {180.0 90.0} do_execsql_test func7-mysql-190 { SELECT round( exp(2), 7), round( exp(-2), 7), exp(0); } {7.3890561 0.1353353 1.0} do_execsql_test func7-mysql-200 { SELECT floor(1.23), floor(-1.23); } {1.0 -2.0} do_execsql_test func7-mysql-210 { SELECT round(ln(2),7), quote(ln(-2)); } {0.6931472 NULL} #do_execsql_test func7-mysql-220 { # SELECT round(log(2),7), log(-2); #} {0.6931472 NULL} # log() means natural logarithm in MySQL do_execsql_test func7-mysql-230 { SELECT log(2,65536), log(10,100), quote(log(1,100)); } {16.0 2.0 Inf} do_execsql_test func7-mysql-240 { SELECT log2(65536), quote(log2(-100)); } {16.0 NULL} do_execsql_test func7-mysql-250 { SELECT round(log10(2),7), log10(100), quote(log10(-100)); } {0.30103 2.0 NULL} do_execsql_test func7-mysql-260 { SELECT mod(234,10), 253%7, mod(29,9), 29%9; } {4.0 1 2.0 2} do_execsql_test func7-mysql-270 { SELECT mod(34.5,3); } {1.5} do_execsql_test func7-mysql-280 { SELECT pow(2,2), pow(2,-2); } {4.0 0.25} do_execsql_test func7-mysql-281 { SELECT power(2,2), power(2,-2); } {4.0 0.25} do_execsql_test func7-mysql-290 { SELECT round(radians(90),7); } {1.5707963} do_execsql_test func7-mysql-300 { SELECT sign(-32), sign(0), sign(234); } {-1 0 1} do_execsql_test func7-mysql-310 { SELECT sin(pi()) BETWEEN -1.0e-15 AND 1.0e-15; } {1} do_execsql_test func7-mysql-320 { SELECT sqrt(4), round(sqrt(20),7), quote(sqrt(-16)); } {2.0 4.472136 NULL} do_execsql_test func7-mysql-330 { SELECT tan(pi()) BETWEEN -1.0e-15 AND 1.0e-15; } {1} do_execsql_test func7-mysql-331 { SELECT round(tan(pi()+1),7); } {1.5574077} finish_test |