Changeset: 6dc5334b7273 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6dc5334b7273
Modified Files:
        sql/src/backends/monet5/sql.mx
        sql/src/sql/skyserver.sql
Branch: default
Log Message:

Added support for truncation of float numbers until 'precision' positions before
or after the decimal point. Needed by the skyserver functions.
Fixed bug in sql.round(dbl,bte) with precision=0.


diffs (82 lines):

diff -r 61791360b98f -r 6dc5334b7273 sql/src/backends/monet5/sql.mx
--- a/sql/src/backends/monet5/sql.mx    Wed Dec 22 20:54:59 2010 +0100
+++ b/sql/src/backends/monet5/sql.mx    Thu Dec 23 15:23:19 2010 +0100
@@ -526,6 +526,10 @@
 address @1_round_wrap
 comment "round off the floating point v to r digits behind the dot (if r < 0, 
before the dot)";
 
+command sql.ms_trunc( v:@1, r:int) :@1
+address @1_trunc_wrap
+comment "truncate the floating point v to r digits behind the dot (if r < 0, 
before the dot)";
+
 @mal
 @:mal_fround(flt)@
 @:mal_fround(dbl)@
@@ -1177,6 +1181,7 @@
 @= fround_export
 sql5_export str @1_dec_round_wrap( @1 *res, @1 *v, @1 *r );
 sql5_export str @1_round_wrap( @1 *res, @1 *v, bte *r );
+sql5_export str @1_trunc_wrap( @1 *res, @1 *v, int *r );
 @h
 @:fround_export(flt)@
 @:fround_export(dbl)@
@@ -3618,10 +3623,27 @@
                else
                        *res = (@1) (floor(*v*(@1)scales[d]+.5)/scales[d]);
        } else {
-               *res = *v;
+               *res = (@1) round(*v); 
        }
        return MAL_SUCCEED;
 }
+str
+...@1_trunc_wrap( @1 *res, @1 *v, int *r )
+{
+    /* shortcut nil */
+    if (*v == @1_nil) {
+        *res = @1_nil;
+    } else if (*r < 0) {
+               int d = -*r;
+               *res = (@1) (trunc((*v)/((@1)scales[d])) * scales[d]); 
+    } else if (*r > 0) {
+               int d = *r;
+               *res = (@1) (trunc(*v * (@1)scales[d]) / ((@1)scales[d]));
+    } else {
+        *res = trunc(*v);
+    }
+    return MAL_SUCCEED;
+}
 
 @c
 @:fround(flt)@
diff -r 61791360b98f -r 6dc5334b7273 sql/src/sql/skyserver.sql
--- a/sql/src/sql/skyserver.sql Wed Dec 22 20:54:59 2010 +0100
+++ b/sql/src/sql/skyserver.sql Thu Dec 23 15:23:19 2010 +0100
@@ -33,16 +33,20 @@
        RETURN res;
 END;
 
-CREATE FUNCTION MS_ROUND(num float, precision int, truncat int)
-RETURNS float
+CREATE FUNCTION MS_TRUNC(num double, prc int)
+RETURNS double
+external name sql.ms_trunc;
+
+CREATE FUNCTION MS_ROUND(num double, prc int, truncat int)
+RETURNS double
 BEGIN
-        IF (truncat = 0)
-                THEN RETURN ROUND(num, precision);
-                ELSE RETURN ROUND(FLOOR(num), precision);
-        END IF;
+       IF (truncat = 0)
+               THEN RETURN ROUND(num, prc);
+               ELSE RETURN MS_TRUNC(num, prc);
+       END IF;
 END;
 
-CREATE FUNCTION MS_STR(num float, precision int, truncat int)
+CREATE FUNCTION MS_STR(num float, prc int, truncat int)
 RETURNS string
 BEGIN
         RETURN CAST(num as string);
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to