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/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.
I don't know why order of operations matters here but it does. It's a floating point thing.
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.
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
Just round it.
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
Note behavior for this as well which does yield expected results.
data strange4;
variable = 1/5 + (1 - 1);
if variable = 0.2;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.