DATA Step, Macro, Functions and more

Multiplying floating point numbers

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 136
Accepted Solution

Multiplying floating point numbers

Hi SAS Users,

 

Today I came across very weird issue in SAS. I just multplied two floating point values and the result is not accurate to actual values.

 

OUTPUT.PNG

 

Capture.PNG

 

If we calculate 0.014x88683.537=1241.569, but SAS give different answer.

 

DATA test2;
set test;
Mul=num1*num2;
RUN;

 

Thanks,
Suryakiran

Accepted Solutions
Solution
‎07-05-2017 09:44 PM
Frequent Contributor
Posts: 136

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

Figured out the issue: num1 is shown as rounded to 3 decimal values, but when performing the calculation it is not rounded to 3 decimals. 

Thanks,
Suryakiran

View solution in original post


All Replies
SAS Super FREQ
Posts: 291

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

Use a format like best16. instead of 5.3 and 12.3 and you will get values closer to the true values.  Multiple them and see if you get something closer to the expected results.

Frequent Contributor
Posts: 136

Re: Multiplying floating point numbers

Posted in reply to WarrenKuhfeld

Still the same results after using best16.

 

OUTPUT.PNG

Thanks,
Suryakiran
SAS Super FREQ
Posts: 291

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

The SAS product will be the same.  The point is if you multiply the numbers together using a calculator, your results will come closer than they did before.

Solution
‎07-05-2017 09:44 PM
Frequent Contributor
Posts: 136

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

Figured out the issue: num1 is shown as rounded to 3 decimal values, but when performing the calculation it is not rounded to 3 decimals. 

Thanks,
Suryakiran
SAS Super FREQ
Posts: 291

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

Right.  That is why I wanted you to display the numbers with more precision before multiplying them together to compare to the product that SAS reports.

PROC Star
Posts: 1,759

Re: Multiplying floating point numbers

Posted in reply to SuryaKiran

SAS gives the correct answer for your example.

data t;
  x=0.014*88683.537;

  putlog x= 32.16;
run;

x=1241.5695180000000000

 

Go to https://www.wolframalpha.com/input/?i=0.014x88683.537

for a calculation with many more digits than SAS or your calculator will display.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 129 views
  • 0 likes
  • 3 in conversation