Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » compare float numbers with precision
compare float numbers with precision [message #486305] Thu, 17 September 2009 08:33 Go to next message
Stella Levin is currently offline Stella LevinFriend
Messages: 89
Registered: July 2009
Member
Hi, in my rcp I need a way to compare files with floating numbers with
certain precision, i.e. if difference between numbers in some place is
less than precision value - don't show the difference. What is the
simplest way to do it? I looked to org.eclipse.compare plugin, I don't
find extension point to change a way to calculate difference.
Any suggestions for the way to do it? Thanks, Stella
Re: compare float numbers with precision [message #486326 is a reply to message #486305] Thu, 17 September 2009 09:38 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
Stella wrote:
> Hi, in my rcp I need a way to compare files with floating numbers with
> certain precision, i.e. if difference between numbers in some place is
> less than precision value - don't show the difference. What is the
> simplest way to do it? I looked to org.eclipse.compare plugin, I don't
> find extension point to change a way to calculate difference.
> Any suggestions for the way to do it? Thanks, Stella

Please define "simple". The most simple approach is e.g. an
implemention Comparator#compare with an absolute threshold:

public class AbsToleranceComparator implements Comparator<Double> {

private final double threshold;

public AbsToleranceComparator(double threshold) {
assert threshold >= 0;
this.threshold = threshold;
}

@Override
public int compare(Double o1, Double o2) {
double diff = o1 - o2;
return Math.abs(diff) < threshold ? 0 :
Double.compare(diff, 0.0);
}

}

This is "simple", but it does not properly consider null values,
overflow, NaNs and Infs. It has only a special use-case, where
absolute differences are correct, but that doesn't mean that
this is your use-case. I know many use-cases, which would be e.g.
be based on a relative difference criterion, where maybe support
for nullable values or maybe for infinities and NaNs requires
special handling.

HTH & Greetings from Bremen,

Daniel Krügler
Re: compare float numbers with precision [message #486349 is a reply to message #486326] Thu, 17 September 2009 11:35 Go to previous message
Stella Levin is currently offline Stella LevinFriend
Messages: 89
Registered: July 2009
Member
Thanks for the quick reply. How may I integrate this comparator to
org.eclipse.compare? Thanks, Stella
Previous Topic:incomplete automatic(silent) update in eclipse 3.4
Next Topic:How to change the menu items according to the selected view
Goto Forum:
  


Current Time: Thu Apr 25 19:55:43 GMT 2024

Powered by FUDForum. Page generated in 0.03187 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top