HOW CAN I SOLVE THIS?

2 Comments

Jan
Jan on 6 Mar 2021
This is a homework question. So please post, what you have tried so far and ask a specific question about Matlab.
x=1;
while(1<1+x)
x=x*0.5; %using bisection method
while(1/x*x~=1)
x
continue;
end
end
but after running it, i got nothing...

Sign in to comment.

Answers (1)

Jan
Jan on 7 Mar 2021
What are the possible numbers in IEEE 754 floating point values between 1.0 and 2.0?
x = 1.0 + k * 2^-52 with k = 0, 1, ... 2^52
Run a loop over k to find a value with x * (1 / x) ~= 1.

9 Comments

thanks for explanation~
but my codes went wrong
>>
for k=0:2^52
x = 1.0 + k * 2^-52 ;
if(x*(1/x) ~= 1)
disp(x)
end
end
i got many x=1.0000
use
format long g
x
num2hex(x)
why cant this work out?
x=1.0;
while(1<1+x)
x=x/2; %using bisection
while((1/(1+x))*(1+x)~=1)
disp(1+x)
end
end
Looks like that 2nd while statement should be an if statement:
if((1/(1+x))*(1+x)~=1)
Also, you might want to disp(x) or use fprintf( ) with lots of digits to better help you identify which x you are dealing with when the display happens.
But note that your algorithm isn't guaranteed to print out the smallest such value that meets the condition. There may be values of x that are not exactly 1/2^n that meet the condition also and are smaller than the smallest 1/2^n that meets the condition, but you aren't testing for them. Jan's method tests for all of the values, even the ones that aren't exactly of the form 1+1/2^n. You might want to put a break statement inside the if block if you use Jan's method.
thank you guys~~~ I use Jan's method and I got an output 1.0000
for k=0:2^52
x = 1.0 + k*(2^-52) ;
if(x*(1/x) ~= 1)
disp(x)
break
end
end
why is that? ><
format long g
for k=0:2^52
x = 1.0 + k*(2^-52) ;
if(x*(1/x) ~= 1)
disp(x)
disp(k)
num2hex(x)
break
end
end
1.000000057229
257736490
ans = '3ff000000f5cbf2a'
As Walter has said already, Matlab displays number with a few decimal places only as deafult. Use the format command to instruct Matlab to show more digits. I prefer fprintf to do this explicitly:
c = 2 ^ -52;
for k = 0:2^52
x = 1.0 + k * c;
if x * (1.0 / x) ~= 1.0
fprintf('%.16f %d\n', x, k);
end
end
oh i see......
Thank you so much~~~
but I have a question, how to avoid such errors in computer (x*(1/x))=!1?
Jan
Jan on 9 Mar 2021
Edited: Jan on 9 Mar 2021
This is not an error. Computers store numbers with a limited precision. This has effects on arithmetics when using them. Famous examples:
0.1 + 0.1 + 0.1 == 0.3 % FALSE
any((0:0.1:1) == 0.3) % FALSE
1e17 + 1 - 1e17 == 1 % FALSE, it is 0
1e17 - 1e17 + 1 == 1 % TRUE
Welcome to the world of numerical maths. The first chapters of text books for this field of science explain the effects and teach methods to cope with them.
The alternativ is to use symbolic maths or numbers with unlimited precision. The later will exhaust the memory as soon as an irrational number is used, because you need an infinite amout of RAM to store it.
When I started to study, the loop took 1 day to find the first match of 1 * (1/x) ~= 1. Now Matlab takes seconds only. Running the full range over 2^52 elements will still take far too long. You can determine the concerned numbers by a formula also, but it is not trivial.

Sign in to comment.

Asked:

on 6 Mar 2021

Edited:

Jan
on 9 Mar 2021

Community Treasure Hunt

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

Start Hunting!