machine precision problem in code
6 views (last 30 days)
Show older comments
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 Comments
Answers (4)
Jan
on 2 Nov 2012
Edited: Jan
on 2 Nov 2012
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.)
Walter Roberson
on 28 Oct 2012
2 Comments
Matt Fig
on 28 Oct 2012
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;
SB
on 2 Nov 2012
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?
0 Comments
James Tursa
on 2 Nov 2012
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?
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!