       RE: Comparison Error. Is ther

• To: mathgroup at smc.vnet.net
• Subject: [mg12697] RE: [mg12678] Comparison Error. Is ther
• 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:= x=0.9;
|             y=10;
|
|In:= (1-x)y
|
|Out=1.
|
|The above obviously evaluates to 1. |
|In:= (1-x)y <1
|
|Out= True
|
|The above gives the wrong answer.
|
|In:= (1-x)y<1.
|
|Out= 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 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  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:=
x=0.9;
y=10;

In:=
num=(1-x)y;
InputForm[num]

Out//InputForm=
0.9999999999999998

In:=
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:=
{num<1, num<1.0}

Out=
{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

```

• Prev by Date: RE: Comparison Error. Is ther
• Next by Date: RE: Thickness of ContourPlot[]
• Previous by thread: RE: Comparison Error. Is ther
• Next by thread: RE: Thickness of ContourPlot[]