09-18-2013 08:38 AM
Hi Bais,
LabVIEW can calc that one too:
0.5 can be stored with no rounding error as it's a power of 2.
But: the problem is limited accuracy of floats!
As soon as rounding errors come into play you shouldn't compare floats for equality, no matter of the used programming language...
09-18-2013 08:38 AM
And if you compare two constants set equal to 0.5 in LabVIEW, you'll find that LabVIEW also finds they are equal.
The problem is when you do calculations that you mathematically expect to be the same number wind up coming from different paths when the miniscule errors that are present in representing numbers in a binary language start to accumulate.
If you have meaningful examples to post to this thread, then comment please.
09-18-2013 08:44 AM
@RavensFan wrote:
And if you compare two constants set equal to 0.5 in LabVIEW, you'll find that LabVIEW also finds they are equal.[...]
If display format does NOT mask any differences that is.......
Norbert
09-18-2013 09:15 AM
The attached VI shows the problem. As mentioned, 0.1 x 10 doesn't yield 1, just like 0.111 x 9 times doesn't equal 1. It's close, but not close enough for Equal (as you can see in the VI, the bit pattern differs).
It's a well known limitation of limited precision, whether it be 1, 7 or 15 digits of precision. Just like you cannot write PI or 1/9 as a decimal number ...
For floats, you'll need account for the total insecurity, like X+/-Epsilon or round to 10 significant figures or something.
As mentioned, it has nothing to do with LV, but with floats and binary representation.
/Y
09-18-2013 10:09 AM - edited 09-18-2013 10:37 AM
this is what matlab does
type
a = 0.5;
b = 0.5;
a == b
it prints out 1 ( which is true)
-- so yea i found out 0.5 = 0.5 in labview as well ( so i was wrong) its because there one data line had a very small error apparently which i didn't epect.
btw i wasn't running my code on matlab, it was actually on a mathscript node. my mathscript node produced a different result to the same code re-written in labview g-code with the same unit test. Mathscript syntax is the same as matlab syntax and you can actually run matlab code in mathscript.
09-18-2013 10:14 AM - edited 09-18-2013 10:16 AM
If there is a difference between MatLab and LabVIEW at all regarding this topic, then the explanation is:
LabVIEW is a programming language, as C/C++ or C#.
MatLab isn't.
So, MatLab isn't bound to rules of programming languages and therefore can insert *some* stuff as feature. Programming languages often support *libraries* which do also provide API functions with such features.
EDIT: And if there is no such library available, one can implement this on him-/herself. I *think* that MatLab supports interfaces for feature additions like this as well to the customer, but still it is no programming language....
Norbert
09-18-2013 10:15 AM
And as already noted, the exact same thing happens in LabVIEW. The moment you start doing some calculations, both LabVIEW and matlab perform the same. A high level language such as C, C++, LabVIEW are not designed to be idiot proof. The programmer must be familiar with limitations of digital computers and make those sort of adjustments for the idiot user.
09-18-2013 10:36 AM
Floating Point Comparisons in Matlab.
Funny how the Matlab experts say the same thing as the LabVIEW experts.
09-18-2013 10:39 AM
yea matlab and labview handling of floats is exactly the same.
I guess there was problem with my translation of mathscirpt to labview g code or something else...
09-18-2013 11:08 AM
@Sentinal_Bais wrote:
btw i wasn't running my code on matlab, it was actually on a mathscript node. my mathscript node produced a different result to the same code re-written in labview g-code with the same unit test. Mathscript syntax is the same as matlab syntax and you can actually run matlab code in mathscript.
It is very likely that the two appraches (LabVIEW vs MathScript) don't result in binary identical code. Slight changes in the order of operations can propagate floating point limitations differently and thus lead to non-identical results. Nothing new here.
You can even get slight result variations with the same code. See here for an interesting case study.