s c h e m a t i c s : c o o k b o o k

/ WebHome / TOC / NumberChapter / Cookbook.NumberRecipesComparing

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Comparing Floating-Point Numbers

Problem

You want to compare two floating-point numbers and know if they are equal. Unfortunately, floating-point arithmetic is not precise so very few results will match exactly. Consequently, we usually want to compare floating point values up to a certain number of decimal places.

Solution

Most Scheme implementations conforming to R5RS will implement the full tower of numerics, providing access to exact and inexact integers, bignum support, and a raft of other tools making this particular recipe somewhat unnecessary.

However, there are still cases where you want a "fuzzy match" on two real numbers since the difference is below some epsilon threshhold.

In these cases, you can use floating-point byte strings to represent and compare numbers:

> (define a (* 8.001e-3 9.001e5))
> a
7201.700099999999
> (define b (* 8.0011e-3 9.001e5))
> b
7201.790110000001
> (equal? a b)
#f
> (define ae (real->floating-point-byte-string a 4))
> (define be (real->floating-point-byte-string b 4))
> (equal? ae be)
#t
> (define ae8 (real->floating-point-byte-string a 8))
> (define be8 (real->floating-point-byte-string b 8))
> (equal? ae8 be8)
#f

Another option is to convert the numbers into strings and then compare the portions of the numbers of interest:

> (define nsa (number->string a))
> nsa
"7201.700099999999"
> (define nsb (number->string b))
> nsb
"7201.7001009001"
> (equal? (substring nsa 0 6) (substring nsb 0 6))
#t
> 

Discussion

It is obviously better to make use of Scheme's bignum and exact integer facilities to carry calculations through your programs with the highest level of precision. However, there are frequent cases where it is useful to provide "fuzzy" matching on numerical terms. Hopefully this recipe can help deal with those situations.

A final thought:

> (define (equal-to-digit digit numa numb)
    (let ((nsa (number->string numa))
          (nsb (number->string numb)))
      (equal? (substring nsa 0 digit) (substring nsb 0 digit))))
> (equal-to-digit 6 a b)
#t

Note: Some error handling would obviously be necessary to handle cases where the digits are insufficient for the match.

See Also

Volume 2, Section 4.2.2 of The Art of Computer Programming

CookbookForm
TopicType: Recipe
ParentTopic: NumberRecipes
TopicOrder: 020

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest