integer and identity code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
John Jamison
am 1 Feb. 2017
Kommentiert: Star Strider
am 3 Feb. 2017
Can you guys help me with this one?
Thanks
1 Kommentar
Roger Stafford
am 2 Feb. 2017
Bearbeitet: Roger Stafford
am 2 Feb. 2017
In case it is a mystery to you why the described percentage is not one hundred percent, it should be realized that if x is any integer other than a power of two, the reciprocal 1/x cannot be achieved exactly in the binary numbers, which your computer uses, with a finite number of binary digits and must therefore approximate that reciprocal by rounding. It is then a matter of chance whether the multiplication x*(1/x) will turn out to be exactly one or off by some tiny amount.
Akzeptierte Antwort
Star Strider
am 2 Feb. 2017
It seems straightforward. Use the ‘double equal’ operator == to test for equality.
A 1 means the condition is met, a 0 indicates it was not. To understand the nature of the problem (if it’s not already been explained to you) see Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link).
7 Kommentare
Star Strider
am 3 Feb. 2017
The problem in the ‘Screen Shot 2017-02-01 at 5.55.24 PM.png’ that you posted wants to calculate: x*(1/x) and test to see if it equals 1. That is the reason it’s not just 1./[1:N].
Weitere Antworten (1)
Image Analyst
am 2 Feb. 2017
How about a simple for loop? Here's a start
equalityCount = 0;
for i = 1 : 100000
if i * (1/i) == 1
I trust you can finish it. I get 86884 = 86.884% Empirically it seems to be higher than the theoretical number Roger suggests. Not sure why.
>> format long
>> 17 * (1/17)
ans =
1
>> 17 * (1/17) == 1
ans =
logical
1
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!