Re: 4gl round function???
Posted in 1997
Using a USING statement is going to be expensive... Investigate the decround() function in the ESQL/C manuals, and write an I4GL interface to it... Actually, come to think of it, I wrote that, a long time ago... /* @(#)File: round.c @(#)Version: 1.1 @(#)Last changed: 89/06/06 @(#)Purpose: I4GL: Round Informix Decimal Numbers @(#)Author: J Leffler */ /* -- Include Files */ #define DEBUG #include <stdio.h> #include <assert.h> #include <decimal.h> #ifndef lint static char sccs[] = "@(#)round.c 1.1 89/06/06"; #endif /* -- Routine: round */ int round(i) int i; { dec_t d; int n; assert(i == 2); popint(&n); popdec(&d); decround(&d, n); /* Undocumented function call */ retdec(&d); return(1); } It wasn't documented in 1989; it is now. I'm not sure what the #define DEBUG is about -- I think it is dead wood. I'd also use fgl_fatal() instead of assert, and prototypes, and fglsys.h, and ... Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> >From: Richard Stanford <richards@herald.net> >Date: Wed, 23 Apr 1997 15:19:10 -0500 >X-Informix-List-Id: <news.37054> > >On 23 Apr 1997, Joseph Cullipher wrote: > >> I am in need of a round function for 4gl. I am currently using 6.00 and >> haven't found one in my manuals or release notes. I have found out that >> if I use the using clause it will round. >> >> let smallint=3-(7/4) using "<" > >I haven't benchmarked it, but would guess that that using clause there is >a moderately expensive operation. > >Informix always truncates real numbers when placing them into a smallint. >The easiest way to change this is to allow Informix to work the same >way -- ie truncating -- but apply additional logic to it. Standard rounding >rounds up (the difference) if the decimal portion is => .5. Therefore: > > LET f_smallint = (real_number_or_expression) + .5 > >works by identifying the incorrectly treated portion of the original and fixing >it with a minimal amount of overhead. Similarily, if you want to always round >the answer to a particular expression upwards (very common) you might choose: > > LET f_smallint = (real_number_or_expression) + .99999 > >using (1 + precision of real) 9s. This forces all numbers not integers to >round up. Note that there may be a few glitches when the precision desired >approaches the maximum precision available on your platform. > >-Richard >