I am calculating the following: p15 = 0.8683*1.15
When I round to 5 decimal places round(p15,5) the answer is 0.99854 since Matlab sees full precision as 0.9985449999999999.
You can confirm this by typing sprintf('%1.30f',.8683*1.15)
ans = 0.998544999999999900000000000000
Unfortunately, I need to match the output of a standard calculator (don't ask why) and need to have the output as 0.99855. How is this possible via Matlab?

 채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 19일

1 개 추천

round(p15 * 1e6)/1e6
By the way, you cannot see the full precision on MS Windows by using sprintf(), but you can on OS-X
0.99854499999999990489385481851059012115001678466796875

추가 답변 (1개)

MHN
MHN 2016년 2월 18일

0 개 추천

What you said is not true! for multiplication the exact number is : 0.998545000000000
p15 = 0.8683*1.15;
r = round(p15,5);
then p15 = 0.998545000000000 and r = 0.998550000000000.

댓글 수: 5

Daulton_Benesuave
Daulton_Benesuave 2016년 2월 18일
No, to see full precision we must use sprintf.
Try sprintf('%1.30f',.8683*1.15)
ans = 0.998544999999999900000000000000
MHN
MHN 2016년 2월 19일
편집: MHN 2016년 2월 19일
You are right. It is an interesting question. And it seems round(p15 * 1e6)/1e6 is not the answer neither. Also it seems that it depends on Matlab version or OS that you are using, since I recieve 0.998544999999999904893854818511 for sprintf('%1.30f',.8683*1.15). But the problem is a little bigger! Consider this code:
a = 0.1;
sprintf('%1.50f',a)
the result is 0.10000000000000000555111512312578270211815834045410. So, the problem is not in multiplication or rounding. I am interested in numerical issues which rises beacuse of using floating point and I hope you can find your answer. If you have limited time and you can not find your answer, I suggest you to take a look at Fixed-point in Matlab.
MHN
MHN 2016년 2월 19일
You might need fixed-point designer. http://www.mathworks.com/help/fixedpoint/index.html
Walter Roberson
Walter Roberson 2016년 2월 19일
The MS Windows version of the C library is rubbish at printing out complete numbers. The Linux version is better now but there was historically a period during which the library used for it had an error in extended printing. I do not know of the OS-X library has always been correct, but I do not recall hearing of any problems for it in this regard.
MHN
MHN 2016년 2월 19일
I did not mean that there is a "problem" in sprintf. I meant even when we just assign a value like a=0.1; and then we use sprintf('%1.50f',a) the exact value is not 0.1. So, as long as one use sprintf and the floating point system, it will not get the result 0.100000000000000000000000000000000000000000.

댓글을 달려면 로그인하십시오.

태그

질문:

2016년 2월 18일

댓글:

MHN
2016년 2월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by