Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!brl-adm!adm!Schauble@mit-multics.arpa From: Schauble@mit-multics.arpa Newsgroups: comp.lang.c Subject: Floating point equality Message-ID: <4915@brl-adm.ARPA> Date: Thu, 12-Mar-87 01:21:43 EST Article-I.D.: brl-adm.4915 Posted: Thu Mar 12 01:21:43 1987 Date-Received: Fri, 13-Mar-87 03:55:59 EST Sender: news@brl-adm.ARPA Lines: 40 Branner at TCGOULD.TN.CORNELL.EDU writes In many numerical programs you do not want to repeat some calculation if some floating-point variable has not changed. Therefore, double x,y; x = y; ... if (x==y) ... SHOULD be available (and work correctly) even in implementations where (x*y == y*x) fails. And it should be no problem to implement, since x and y are two copies of the same number in the same internal format. (This is very different from the common mistake of expecting infinite precision when using FP variables in loop termination criteria...) - Moshe Braner 2-------------------- It's rare that the two numbers are just exact copies of the same variable. Usually one goes through slightly different calculation paths. Also, as an extreme case, I know of one compiler where x=y if(x==y) would fail because the x=y did rounding. None of this mentions the countless times I've seen people using equal to end loops, mathematical types who were surprised that sin(PI) != 1.0, annd so on. I'll stand by my original comment. Test for equality on floating point numbers is machine and compiler dependant. the only question is do we tell people about this. For your example, you should have written if (fabs(x-y) < EPSILON) Paul Schauble at MIT-Multics.edu