Path: utzoo!attcan!uunet!husc6!mailrus!ames!lll-lcc!pyramid!infmx!aland From: aland@infmx.UUCP (Dr. Scump) Newsgroups: comp.databases Subject: Re: Informix 4gl - no integer arithmetic??? Summary: consistent with the documentation Message-ID: <410@infmx.UUCP> Date: 7 Sep 88 01:43:27 GMT References: <466@pan.UUCP> Organization: Informix Software Inc., Menlo Park, CA. Lines: 55 In article <466@pan.UUCP>, jw@pan.UUCP (Jamie Watson) writes: > > I have just discovered, the hard way, that in Informix 4gl the following > statement, which contains only integer values, yields a most surprising > result: > > main > define > i integer > let i = 630 / 100 * 60 > display i > end main > > Running this program produces a result of 378!!! > > The 4gl manual states that all arithmetic is done in type 'decimal', which > might explain what is going on here, but then it goes on to explain that > for integer values, the specific type used is 'decimal(10,0)', which would Um, that's for the *operands*, not necessarily the *result*. See below... > imply that the integer nature of the operations should be preserved. It > jw This is consistent with the documentation. The type used for the integer *operands* is (10,0); since none of the operands had scale, no precision is lost. The scale of the operands is *not necessarily* the scale used by the result (see page 1-13R of the manual, second note). The scale used in the *result* of an equation depends on the equation; in this case, maximum scale is *22*, not 0, due to the use of division (scale is 32 - 10 + 0 - 0, using the formula on page 1-14R of the manual). Therefore, the result of the right side of the equation is 378.00000000..., which converts to integer +378 when converting the value to integer at the end. This is similar to C's promotion of types. For example, try main() { int intvar; printf ("In C, we get %d\n", intvar = (630.0 / 100 * 60)); } and you will get the exact same result (378). Informix-4GL differs from C only in that C will not promote beyond integer if no floats / doubles are used; 4GL will promote scale depending on the type of operation in an effort to retain accuracy according to the rules on page 1-14R. -- Alan S. Denney | Informix Software, Inc. | {pyramid|uunet}!infmx!aland Disclaimer: These opinions are mine alone. If I am caught or killed, the secretary will disavow any knowledge of my actions. Santos' 4th Law: "Anything worth fighting for is worth fighting *dirty* for"