BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lupacante
Calcite | Level 5

Hi,

why is it that these two codes work:

data strange;
     variable = 1/5;
     if variable = 0.2;
run;

data strange2;
     variable = 1 - 1 +1/5;
     if variable = 0.2;
run;

but this one doesn't:

data strange3;
     variable = 1/5 + 1 - 1;
     if variable = 0.2;
run;

?

thank you,
Marco

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

1/5 cannot be exactly represented as a binary number.  The order of operations matter because by adding 1 to 1/5 you have moved where the first 1 is in the binary representation, thus leaving fewer bits to represent the value of 1/5.

View solution in original post

6 REPLIES 6
data_null__
Jade | Level 19

I don't know why order of operations matters here but it does.  It's a floating point thing.

data _null_;
   variable1 =
1/5;
   variable2 =
1 - 1 +1/5;
   variable3 =
1/5 + 1 - 1;
  
put (variable:) (=hex16. / );
   run;


variable1=3FC999999999999A
variable2=3FC999999999999A
variable3=3FC9999999999998
Tom
Super User Tom
Super User

1/5 cannot be exactly represented as a binary number.  The order of operations matter because by adding 1 to 1/5 you have moved where the first 1 is in the binary representation, thus leaving fewer bits to represent the value of 1/5.

Lupacante
Calcite | Level 5

Thanks Tom, that's interesting.

Do you know how I might be able to rewrite the code to make it work regardless of the order or operations?

Thank you,

Marco

data_null__
Jade | Level 19

Just round it.

data _null_;
   v1 =
1/5;
   v2 =
1 - 1 +1/5;
   v3 =
1/5 + 1 - 1;
   r1 = round(v1,
1e-6);
   r2 = round(v2,1e-6);
   r3 = round(v3,1e-6);
   put (v: r:) (=hex16. / );
   run;

v1=3FC999999999999A
v2=3FC999999999999A
v3=3FC9999999999998
r1=3FC999999999999A
r2=3FC999999999999A
r3=3FC999999999999A
TomKari
Onyx | Level 15

Approach this with caution. These comparisons are inherently wobbly in computing systems, and what works for one example may fail in another.

Why are you trying to make this equality comparison? There may be a way to do it that will avoid this difficulty.

Tom

ballardw
Super User

Note behavior for this as well which does yield expected results.

data strange4;
     variable = 1/5 + (1 - 1);
     if variable = 0.2;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1234 views
  • 7 likes
  • 5 in conversation