[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: floating point lesson, please.
From:       "Paul M. Dorfman" <sashole () EARTHLINK ! NET>
Date:       1999-09-30 5:12:39
[Download RAW message or body]

> "Lambert, Bob" wrote:
>
> I'll answer the question with another question:........
>
> data A;
>    x=0;
>    do i = 1 to 10;
>      x+0.1;
>    end;
>    if x = 1 then put 'x is equal to 1';
>    else put 'what happened?';
> run;
>
> In the log, "what happened" is printed.
> But when you do a proc print,
>
> PROC PRINT DATA=A;
> RUN;
>
> you'll see that x does equal 1.
>
> Is there a format that displays the answer in floating point notation?  If
> not, is there A way to get a looksy at the floating point
> number?
>

Bob,

This code gets exactly what it asks for. It asks: "Does x equal 1?" and
receives the answer 'no' because, in fact, x equals one not. To SAS, for a
numeric variable to be equal precisely 1 means its having the binary
representation of

0000000000000000000000000000000000000000000000000000000000000001,

which it will have, for example, if 1 is assigned to a numeric variable or
added to one previously initialized to 0.  If you print the value of x after
the DO loop IN BINARY using the format BINARY64. by means of coding PUT
X=BINARY64. you will observe

0000000000000000000000000000000000000000000000000000000000000000,

which is the result of the rounding discussed in detail earlier, whilst
printing it with a standard numeric format, say, PUT X=, will show 1. Why?
Because, in order to print, SAS actually asks and answers another question,
namely, "Does the value of x lie within one trillionth of 1?", and since the
latter is affirmative, it prints 1. If WE ask the same, more appropriate
question, we shall obtain the same answer:

207  DATA A;
208     X=0;
209     DO I = 1 TO 10;
210       X + 0.1;
211     END;
212     IF FUZZ(X) EQ 1 THEN PUT 'X IS EQUAL TO 1';
213     ELSE PUT 'WHAT HAPPENED?';
214  RUN;
X IS EQUAL TO 1

with the result speaking for itself. Does the above mentioned format
BINARY64. answer the question you have posed?

Kind regards,
==================
Paul M. Dorfman
Jacksonville, FL
==================

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic