필터 지우기
필터 지우기

machine precision problem in code

조회 수: 3 (최근 30일)
Ben
Ben 2012년 10월 27일
my function is supposed to take any rational number as an input, and it's supposed to output the number of digits after the decimal point. I keep on rechecking my code, but what am i doing wrong with my code?
function y = digits_debugged(x)
x = abs(x); %in case of negative numbers
y = 0;
while (floor(x)~=x)
y = y+1;
x = x*10;
end
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 27일
I am getting 5. what did you get?
Ben
Ben 2012년 10월 27일
oh i am getting 5 as well. I am confused as to why my instructor told me my function has several bugs. thank you!

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

답변 (4개)

Jan
Jan 2012년 11월 2일
편집: Jan 2012년 11월 2일
Walter has mentioned, that the number of digits is not well defined, because the displayed output use the base 10 while the internal storage uses the base 2. The conversion of the bases effects the number of digits due to the limited precision of the internal representation of doubles. In consequence, there cannot be a program, which replies the "number of digits after the decimal point" for "any rational number".
It is not a question of fixing the code, the task is not possible. Therefore any program, which needs this, has a structural problem.
Remark: The efficient and smart V8 java engine uses a C-library for the stable conversion between numbers and strings, e.g. str2double. It has 8MB(!) of source code, which is a sign that this task is not trivial and therefore it cannot be implemented reliably in a few lines.
(For all cracks: Yes, I know that code size is not a 100% perfect measurement of the complexity of the task. A counter-example is writing a letter to your grandma with Windows Office 2010 Professional Plus. In this case even the name of the program is a strong indicator already.)
  댓글 수: 1
Ben
Ben 2012년 11월 2일
편집: Ben 2012년 11월 2일
oh okay, so would I use something like
(floor(11.111*10^3))-11.111*10^3 = eps(11.111*10^3)*2
in my function instead? thanks for telling my I had to rewrite it I have been going crazy just trying to debug!

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


Walter Roberson
Walter Roberson 2012년 10월 28일
  댓글 수: 2
Matt Fig
Matt Fig 2012년 10월 28일
Yep. I think that in general this assignment is not possible to complete as described. For example, how many digits are after the decimal for N = 1.1? Let's check:
N = 1.1;
sprintf('%17.17f',N)
% Try for N = 1.21335;
Ben
Ben 2012년 11월 2일
so I just checked with my lab instructor,and you guys are write. How would I go about fixing my code? My instructor suggested using eps() and I haven't learnt that yet/can't find anything on google on how to use it. Do either of you know how to use eps() in this situation?

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


SB
SB 2012년 11월 2일
Hey, it should be something like this:
%
x = abs(x); %in case of negative numbers
y = 0;
n=0
while (floor(x*10^n)~=x*10^n)
u=floor(x)~=x
y = y+1
n=n+1
end
By the way, could you show me how to do the decimal to binary conversion question?

James Tursa
James Tursa 2012년 11월 2일
Ben, as many others have already pointed out, this assignment is ill conceived and cannot be accomplished per the reasons others have already given. For example, you can use the num2strexact function from the FEX to see the exact decimal conversion for any single or double precision number:
Given that conversion, what exactly is it that your instructor wants to see for an output? Sure, one could use eps in some fashion, but that is only going to give you a number that is about 1e-15 smaller than the number you started with. How is that going to be related to the "number of digits after the decimal point"? Once you start going down this path of what "number of digits after the decimal point" means you will quickly head in the direction of the code/algorithms that Jan has alluded to. I.e., is it the "minimum number of digits that will, when read in via "standard" algorithms, reproduce the input number exactly"? Or what?
  댓글 수: 1
Ben
Ben 2012년 11월 2일
I asked my instructor and told him about how many digits we were supposed to go to, and he said to use eps() which implies 1e-15. I just don't know how to correctly implement eps() and google isn't helping me either.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by