Interesting confusion in matlab - Number format
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I found one interesting in matlab and actually this is causing a bug in my code. I am unable understand the behaviour/logic of matlab. It is something to do with how the number is represented. Please see the below example and could you please me help to understand what is the issue ?
j_values = [ 25.3850 25.3900 25.3950 25.4000 25.4050 25.4100];
time_interval = 0.005;
% ' i ' can be between 1 and 6
index = ( j_values(i) / time_interval);
When i value is 3 , i get output in exponential form (5.0790e+003) where as for other values of i i get a decimal number (5078). Why is this behaviour ??
How this causes issue is when i use ceil:
index = ceil (j_values(i) / time_interval);
So index value will be same for both i = 3 and 4.
ceil(j_values(3) / time_interval)
ans = 5080
ceil(j_values(4) / time_interval)
ans = 5080
But if i give value directly then output is different : ceil(5.0790e+003)
ans = 5079
This is really interersting confusion :P Can someone help me to understand what is happening ?
Cheers :)
0 commentaires
Réponses (3)
per isakson
le 13 Août 2014
Modifié(e) : per isakson
le 18 Août 2014
On R2013a,64bit,Win7 I get
>> j_values/time_interval
ans =
5077 5078 5079 5080 5081 5082
>> j_values(3)/time_interval
ans =
5079
What you see is a consequence of how floating point arithmetic works. See: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
 
Added later:
Replace
index = ceil(j_values(i) / time_interval);
by
index = round(j_values(i) / time_interval);
2 commentaires
per isakson
le 14 Août 2014
Modifié(e) : per isakson
le 14 Août 2014
- Did you read Cleve's piece?
- Did you notice "We like to use the term flint to describe a floating-point number whose value is an integer. Floating point operations on flints do not introduce any round- off error, as long as the results are not too large."?
- "general guidelines" No, I don't think there are any.
- My "general guideline" is simple. Never depend on that functions return doubles, the values of which are flint. There are a few exceptions, e.g. the counter in for loops. The functions round, ceil and floor return of course flint
- See http://www.mathworks.com/matlabcentral/answers/67247#answer_78702
Adam
le 13 Août 2014
Modifié(e) : Adam
le 13 Août 2014
Use
round
rather than ceil when your result is so close to the integer you want.
I have occasionally been caught by this too, it is just an effect of the way Matlab works with numbers that are double precision unless you explicitly create them as a different data type.
On my machine the example you showed works fine and gives an integer value (still in a double though obviously) for index 3.
Iain
le 13 Août 2014
Double format numbers are only accurate to about 15 significant figures. When you do sums, you can get errors at the 15th significant figure because double format numbers are NOT exact.
Eg:
1E30 + 1 - 1E30 == 1 will return false, because 1 is too small to add to 1E30 without using a format other than double.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!