```Date: Wed, 30 Jan 2002 01:46:37 +0000 Reply-To: John Whittington Sender: "SAS(r) Discussion" From: John Whittington Subject: Re: Q: Floating point variable Comments: To: Joe Eplion In-Reply-To: Content-Type: text/plain; charset="us-ascii"; format=flowed At 14:13 29/01/02 -0800, Joe Eplion wrote: >1) How can we distinguish an integer variable from a >floating point variable? You can't. Unlike many/most programming languages, SAS offers no facility to define a SAS variable as 'integer numeric'. The values of all SAS numeric variables are stored as 'floating point' numbers. >2) For a floating point varaible called 'rate', its >screaan value, say 9, is actually stored at a value >very close to 9 (e.g., 8.99999999999 or >9.00000000001), but not exact 9. Therefore, if you >have a statement 'if rate =9 then ...' you may not >hit 9 as shown on your screen. Is there a solution to >handle this, besides using rounding function? If the value of a SAS numeric value is truly an integer value, then (up to extremely large numbers) it will always be stored faithfully as that integer value. Hence, for example, if your variable 'rate' acquires a value of 9 by means of an explicit assignment statement (rate = 9 ;), then that vale WILL (on any platform) be stored as _exactly_ 9, so that you comparison 'if rate = 9' will always yield a 'true' result. The problem arises when values you think are integers result from non-integer arithmetic, since decimal non-integers _cannot_ generally be stored absolutely precisely in any binary computer. Hence, even something as simple as "rate = 0.9 * 10;") is not necessarily safe, since the value which gets stored may not be _precisely_ 9 - leasing to the sort of comparison problems you describe. The only solution is to force the value to be integer (e.g. with INT or ROUND functions) either when it is stored (e.g. rate = "INT 0.9 * 10") or, probably better/safer, when a comparison test is undertaken (e.g. "if INT(rate) = 9"). >3) Different platforms may store a floating point >value differently, as I was told. What are major ways >of storing it? In general, the exponent and mantissa (and, in some cases, signs of the mantissa and exponent) are stored separately, each in binary. The number of bits/bytes of storage space allocated to the exponent determine 'how large a number' can be stored, whilst the number of bits/bytes allocated to storage of the mantissa determine the maximum 'precision' (number of significant digits) which can be stored. Both of these vary appreciably between platforms - and the amount of storage space allocated to the matissa will, to some extent, determine the extent to which the sort of 'precision problems' you describe are likely to arise. Any help? Kind Regards John John ---------------------------------------------------------------- Dr John Whittington, Voice: +44 (0) 1296 730225 Mediscience Services Fax: +44 (0) 1296 738893 Twyford Manor, Twyford, E-mail: John.W@mediscience.co.uk Buckingham MK18 4EL, UK mediscience@compuserve.com ---------------------------------------------------------------- ```

Back to: Top of message | Previous page | Main SAS-L page