LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

quotient & remainder bug

How does a simple hand held calculator do it?  The only issues I see with a calculator is when the display is not large enough to show the entire number (repeating decimals, value of pi, etc), or an internal overload (number too large).
- tbob

Inventor of the WORM Global
0 Kudos
Message 21 of 95
(3,061 Views)
Hi guys
 
Although I first thought in an other way, now I have to agree with the ones who claim the Q&R vi works properly. This is due to the following points:
 
1. The computer does not know, what is a logical output for humans (2, 1.99, 1.9999, ...)
2. If the input were strings and needed a user to input a value - do you think it is user friendly to tell him "if you need three digits of precision enter 2.000"? I think not - so you then have to do the conversion on your own.
3. If you deal with floating point numbers you simply have to be aware of the fact of inaccuracy in at least the last digit.
4. If the Q&R vi was changed, all the other function which could deal floats have to be changed to - if one is often in this forum, he knows how many times unexprerienced programmers or people with another background, who just have to use LV due to a certain problem, come up with the question "I compare two numbers on equality. 1.1 and the other is a result from a division. So if I get 1.1 from the division it should give true as a result, but it doesn't - why". What do we answer them? I never answered "Oh yes you're right - the equality vi should be changed because it does not behave 'logical'". I always answer that one has to be aware of the representation of floats in a computer and so on.
5. What if the Q&R vi was changed and one expects this inaccuracy? He would come up with the same point "the Q&R vi does not give a logical result".
 
So - this is my point on this.
 
Thomas
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
Message 22 of 95
(3,105 Views)
Oh my... What did I start?  Smiley SurprisedSmiley Wink
 
 
I'll try to address everything that came up...
 
tbob hit the nail on the head.  Why does a simple calculator give the expected results?
The reason why a normal calculator gives the correct anwser, is that it uses reals the way they are supposed to be used.   The calculator will shows maybe 10 digit on the display, for input and output.   However, it calculates internally with a higher accuracy, for example 13 digits.  After it calculation is done, it rounds the result back to 10 digits.   This rounding is essential to getting meaningfull results. 
 
This is probably the whole issue...    Obviously, the real representation is inaccurate.   It cannot even represent the number 0.2 correctly.  It makes 2.000000000000001 E-1 from it...     The reason why we still can work with it comfortably, is that we use the reals in such a way that this inherent finite accuracy doesn't bother us.   The way to do that, is to make sure that the internal calculation occurs at a higher accuracy then the input and output, and then round the end result.    We don't care that the computer transform the numer 0.2 into 2.000000000000001 E-1, because it's rounded back to 0.2     
 
When properly used, you do not notice the finite accuracy of the real number representation in the end results!   If you do get deviations, then you should have used a higher internal accuracy.
 
 
That's where people make mistakes...  
 
Jean-Pierre said that my routine doesn't work when you really want to divide 1 by 2.000000000000001 E-1.    However, you've allready made a fundamental mistake if you try to do that using DBL's.  You cannot calculate that kind of number in a DBL.  From the number representation of the number 0.2, it's allready clear that the last digit is totaly unusable. Performing operations on it, (that themselves also have a finite accuracy) will increase the number of inaccurate digits.  You aren't bothered by it, as long as these quantization errors don't influence the end result!   Obviously, the only way to do that, it to make sure that the internal represenation of the number has (much) more digits than the required end-result.  Clearly, to calculate 1 by 2.000000000000001 E-1 you would need EXT as the internal representation.  (And you would need a corresponding (polymorphic) QR vi that uses EXT as input, and reduces (x/y) to DBL)
 
 
Normally, you don't round of the result, until the last moment.  (Typically, an indicator on your front panel, or the output in a file) However, the QR routine is a very special case.   Here, you must loose the digits with the quantization errors in (x/y), before performing the 'floor' operation.   Otherwise, the result is meaningless, as we have seen.  That's not a general problem of reals in Labview.  It's just a specific feature of the QR routine! (And maybe of the 'floor' routine...)
 
 
GerdW
I very much doubt that a QR routine is specified in IEEE854.   If it is, I'm sure it does not work like the current Labview implementation.
 
JoeLabView
Have you tried the second QR vi that I posted?  As far as I can see, it's works without any problem with numbers up to 7 digits.  Which is logical, as it converts DBL's back to SGL in it's calculation.   Could make the vi polymorphic, to do the same with EXT to DBL.
 
You could ofcourse also create a QR vi that has an input asking at which digit to round (x/y).    There's no rule where the quantization errors start to occur...   If you've done very complex operations, than you might easily loose 6 digits...
 
 
Message 23 of 95
(3,078 Views)
Becktho
1.  The computer makes the quantization error.  Not the user.    The input and output should always be reduced such, that you loose this quantization error.   The programmer ofcourse has to make sure that this happens at the correct spots.    That includes the programmers who wrote the built-in Labview vi's, such as the QR vi!   
 
4.  Indeed, you should be carefull comparing reals. If you really want to compare the result of a division with an equality operator, then you certainly must remove the quantization errors yourself first. One way would be to convert the DBL from the division to SGL before the comparison.  Then it will work fine.   (I hope that's also the answer you give to such questions.)
There are other techniques, all with their advantages and disadvantage.  Clear is however, the real equality is not an easy subject.  But it's solvable, using the standard vi's.
 
However, such an option does not exit for the QR vi !   You cannot change it's behaviour yourself, other than to write a new vi...  Converting to SGL before the QR vi does not help!   The QR is simply useless for reals.
 
 
You could write a "logical" equality for reals, to suit your requirements.  In my view, the QR actually, is such a kind of "logical" real devision vi. The vi is about getting an integer quotient, from a real division.... that doesn't look like a normal real operation to me. It cannot simply ignore quantization errors, because it's results become meaningless.  And unlike the real equality, there's nothing we can do about it.
 
5.  I would like to meet the programmer that relies on a (arbitrary) quantization error for the funtionality of his program! Smiley Very Happy
 
 
 
Tell me: 
Do you see any use for a QR vi that gives:   1 / 0,2 = 4   as a result?         (Except for accountants.  Smiley Wink )

Message Edited by Anthony de Vries on 01-31-2006 04:21 PM

0 Kudos
Message 24 of 95
(3,058 Views)
Removing quantization errors by converting a double to a single does maybe work, but you still have floating point numbers which could still cause problems.
 
Well, this discussion could be continued endlessly.
 
I just think 1%0.2 must be expected not to be 5, due to the fact that the computer does not know what you want to see as a result or how it should be formatted.
 
At the end (maybe this sounds a bit offensive - but believe me, I don't want to be) you see different levels of programmers. If one complains about number represenations, he shall use just variants as in VB - pretty easy to learn and write a program, but the sooner or later you get one runtime error after the other.
 
To come to an end - I never had any use for the example you used (or something similar x%y.z) and I am aware of the floating point topic. That's enough for me, to write a program, that works as expected and does not have calculation errors, which once could cause problems, based on an unexpected number conversion or so.
 
Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
0 Kudos
Message 25 of 95
(3,051 Views)

On the same computer that mistakenly outputs 4 for Q&R of 1/0.2 using Labview's Q&R function, I can write a C program using the floor(x/y) function and floor(1/0.2) comes out to be 5.  Now who is to blame?  Clearly the Labview Q&R function is wrong.  Sure the computer does not interpret human intentions, but the programmers must make the necessary corrections.  I'm not talking about you or me making these corrections, I'm talking about the Labview function writers.  Calculators do it right, C does it right, Labview does it wrong.  There is no suitable arguement against this fact.

One could argue that Q&R is correct because the actual input is not 0.2, instead it is 0.200000001 or so.  For this input Q&R is right on.  However, the Q&R function writers are not taking into account that the computer representation of a floating point input is inexact.  The function was written for humans to use.  So computer innacuracies must be accounted for, or the function is useless.  Same with the equality function.  It is useless for floats.  We have all gotten around this by using greater than or less than with some 20 digits of precesion, but this is a band aid approach.  The LV functions do not work with floats as intended to be used by humans.  As Anthony said, the function writers should compute the results using a higher precesion and then round off.  This is the way calculators do it, and this is the way a C compiler does it.  You don't see any arguements amongst calculator users or C programmers because there is no problem there.  Changing Q&R to work this way will not upset anyone and will not cause a problem for previously written code.  How can improved accuracy cause any problems?

You could argue, well what if someone wants to calculate 1/0.200000000001?  This calculation should be done using 64 bit numbers then rounded to a 32 bit number, or whatever.  Bottom line is to make the Q&R function calculate in a higher precision than the input, and output at a precision equal to the input, as Anthony said.  Same for equality.  Would anyone be against NI making this change?

- tbob

Inventor of the WORM Global
0 Kudos
Message 26 of 95
(3,030 Views)
I agree with JPD that there is nothing wrong with the current implementation of QR.
0 Kudos
Message 27 of 95
(3,022 Views)
Seems kind of silly that a $5000 program can't perform as well as a $10 calculator.
Message 28 of 95
(3,032 Views)

This is worth < $0.02 **, but with all these comparisons to calculators ...

While binary/floating point calculators exist, most physical calculators work internally with binary coded decimal (BCD) ... or at least they used to.  Apologies if I'm behind the times on this.

(** both in binary and BCD)

 

=====================================================
Fading out. " ... J. Arthur Rank on gong."
0 Kudos
Message 29 of 95
(3,012 Views)


@unclebump wrote:
Seems kind of silly that a $5000 program can't perform as well as a $10 calculator.



LOL!! 😄 😄

That's a good comeback Unclebump!!

LOL!! 😄

But we have to get back into context... LV was designed as a computer language not as a calculator..  so similar (computer language) rules apply.  But there ins nothing that prevents having a Math VI that breaks those rules for practicality.  😉  

In reality, the computer has no clue what the number really is, it only sees a finite number of 1's & 0's and interprets its value from that...  But of course we all know this...

😄

 

Message 30 of 95
(3,011 Views)