From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109d8a,c7637cfdf68e766,start X-Google-Attributes: gid109d8a,public X-Google-Thread: 107079,c7637cfdf68e766,start X-Google-Attributes: gid107079,public X-Google-Thread: 103376,c7637cfdf68e766,start X-Google-Attributes: gid103376,public X-Google-Thread: f8362,c7637cfdf68e766,start X-Google-Attributes: gidf8362,public X-Google-Thread: f43e6,c7637cfdf68e766,start X-Google-Attributes: gidf43e6,public From: mheaney@ni.net (Matthew Heaney) Subject: floating point comparison Date: 1997/07/29 Message-ID: #1/1 X-Deja-AN: 260463465 Distribution: inet Organization: Estormza Software Newsgroups: comp.lang.ada,sci.math.num-analysis,comp.software-eng,comp.theory,sci.math Date: 1997-07-29T00:00:00+00:00 List-Id: I need to do some floating point comparisons in Ada 83 (but I'd like Ada 95 advice too), and needed someone to interpret the advice in AQ&S. My immediate problem is, How do I test 2 floating point numbers for equality? The AQ&S states that if abs (X - Y) <= Float_T'Small be used to test for "absolute equality in storage." Q: What does "absolute equality in storage" mean? Q: Why exactly is the test if X - Y a Bad Thing? Is it nonportable? Not guaranteed to work? Q: Is it ever acceptable to write "if X = Y" ? Robert Dewar has stated in previous threads (one has the same name as the subject of this post, so you can go to deja news to look it up), that it's often OK to test for equality. Robert: can you enumerate the conditions under which such a test is appropriate, or refer us to a book explaining why? The AQ&S states that if abs (X - Y) <= Float_Type'Base'Small be used to test for "absolute equality in computation." Q : What does "absolute equality in computation" mean? Q: How is equality in "computation" different from "storage"? The AQ&S states that if abs (X - Y) <= abs X * Float_Type'Epsilon be used to test for "relative equality in storage." Q: What does "relative equality in storage" mean? Q: How is "relative" equality different from "absolute" equality? The AQ&S states that if abs (X - Y) <= abs X * Float_Type'Base'Epsilon be used to test for "relative equality in computation." Q: What does "relative equality in computation" mean? Suppose I have 2 lines, and I want to make sure they aren't parallel. How should I write the predicate to compare the slopes of the lines, eg if M1 = M2 then or if abs (M1 - M2) <= Slope'Small or if abs (M1 - M2) <= abs M1 * Slope'Epsilon or Honestly, the advice in the AQ&S is way over my head. Does anyone out there understand it, and care to explain it? Is there a paper or book that explains Everything You Wanted To Know About Floating Point Comparisons? Under what circumstances would one use T'Small vs T'Epsilon? I looked around in the Ada programming FAQ and couldn't find anything about floating point comparisons. Perhaps we can use the discussion this thread engenders to add something to it. Oddly enough, none of my Ada books state how to properly write a predicate to compare floating point numbers. Even Norm's book just had a vague "compare the difference to a carefully chosen small value." Anybody have any substantive advice? -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271