RE: Comparison Error. Is ther

*To*: mathgroup at smc.vnet.net*Subject*: [mg12697] RE: [mg12678] Comparison Error. Is ther*From*: Ersek_Ted%PAX1A at mr.nawcad.navy.mil*Date*: Wed, 3 Jun 1998 02:20:49 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Chris wrote: | |I'm having trouble with comparisons, see below: | |In[10]:= x=0.9; | y=10; | |In[11]:= (1-x)y | |Out[11]=1. | |The above obviously evaluates to 1. | |In[12]:= (1-x)y <1 | |Out[12]= True | |The above gives the wrong answer. | |In[13]:= (1-x)y<1. | |Out[13]= False | |This gives the right answer, notice the decimal point after the 1. | |Do I have to remember to put 1. on the right hand side each time? What | if, on occasion, the left hand side turns out to be "1", instead of |"1." | Doing N[] is not a global fix, because sometimes my expression can be |0. In this case, N[0] is not "0.", it is just "0". So, the N[] to |both sides won't always work. | |Is there some global way to correct for this kind of stuff???? | Chris, You get the "wrong" answer above is because (1-x)y is slightly less than one (see the InputForm below). However, I think both of the above should be False anyway. For In[3] below I have a definitions for Less and Greater that I think are more appropriate. If you like you can put this in your (init.m) file, and this definition will always be used. Note: There may be similar problems with arbitrary precision numbers, and my patch does nothing about that. Also my patch does nothing to change what happens when the right hand side is zero. If there are any problems with that (I haven't found any) you can provide rules for Less[x_,0] and Greater[x_,0]. In[1]:= x=0.9; y=10; In[2]:= num=(1-x)y; InputForm[num] Out[2]//InputForm= 0.9999999999999998 In[3]:= Unprotect[Less, Greater]; Less[x_,y_]/; (MachineNumberQ[x] || MachineNumberQ[y])&& (y!=0)&&(Im[x]===Im[y]===0):= Positive[(y-x)/Abs[y]-2*$MachineEpsilon] Greater[x_,y_]/; (MachineNumberQ[x] || MachineNumberQ[y])&& (y!=0)&&(Im[x]===Im[y]===0):= Positive[(x-y)/Abs[y]-2*$MachineEpsilon] Protect[Less, Greater]; In[6]:= {num<1, num<1.0} Out[6]= {False,False} As far as I can tell this will work very well. If anyone can see where my patch will cause problems, please let me know. Ted Ersek