Hi,
I'd like to compare 2 floating point numbers within a given error. I'd
rather not use a absolute error but one related to the number of
values that can be represented between the two floats. I've been
reading: http://www.cygnus-software.com/paper...ringfloats.htm
where the following function is provided to do this:
bool AlmostEqual2sCo mplement(float A, float B, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
assert(maxUlps 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographical ly ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographical ly ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}
However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
So my question is this. Is there a good compiler independent method
for comparing floating point numbers with a relative error?
Apr 30 '07
14 2516
P.J. Plauger wrote:
"nw" <ne*@soton.ac.u kwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
>>I think you need to read the cited article to find out what this is about. This isn't really a "relative error", it's asking the question "is the number of representable floating-point values between given A and B less than N?"
Yes what he said. :)
Sorry, the question wasn't defined as clearly as it could have been. So is there any compiler independent method for this? Or is it perhaps not even a particularly good idea?
You need the C99 function nexttoward, which does almost exactly what
you want. It'll be a part of the next C++ Standard, but right now it's
relatively rare. See our Compleat Library, available at our web site.
There is also nextafter which is almost the same but maybe slightly more
appropriate since it takes parameters of the same type instead of
having the second parameter always as a long double for reasons
probably only numerical analysts know.
They are also included with recent GNU libc versions that are used on
most Linux systems.
--
Markus
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
nw wrote:
I'd like to compare 2 floating point numbers within a given error. [...]
However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
Right.
Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>nw wrote:
>>I'd like to compare 2 floating point numbers within a given error. [...]
>>However, as the article states, this relies on a number of compiler specific features, such as the size of int (and I guess float). It also relies on the floats using IEEE representation (I guess all compilers use this, but is it in the standard?).
>Right.
Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
Yes. Are you agreeing or disagreeing with my saying "right"?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On May 1, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
James Kanze wrote:
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
nw wrote: I'd like to compare 2 floating point numbers within a given error. [...]
>However, as the article states, this relies on a number of compiler specific features, such as the size of int (and I guess float). It also relies on the floats using IEEE representation (I guess all compilers use this, but is it in the standard?).
Right.
Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
Yes. Are you agreeing or disagreeing with my saying "right"?
It depends on what you meant by "right". The way I read it was:
>I guess all compilers use IEEE representation, but is it in the standard?
Right [meaning yes, it is in the standard].
However, I don't find "right" very idiomatic for responding to
this kind of question, so maybe you meant for it to apply to
something else.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
On May 1, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>James Kanze wrote:
>>On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@com Acast.net> wrote: nw wrote: I'd like to compare 2 floating point numbers within a given error. [...]
>>>>However, as the article states, this relies on a number of compiler specific features, such as the size of int (and I guess float). It also relies on the floats using IEEE representation (I guess all compilers use this, but is it in the standard?).
>>>Right.
>>Which standard. IEEE representation is a standard, but it's not required in C++, and is far from universal.
>Yes. Are you agreeing or disagreeing with my saying "right"?
It depends on what you meant by "right".
It is meant as a confirmation response. What else could I mean?
The way I read it was:
>>I guess all compilers use IEEE representation, but is it in the standard?
> Right [meaning yes, it is in the standard].
However, I don't find "right" very idiomatic for responding to
this kind of question, so maybe you meant for it to apply to
something else.
Good, you're doubting yourself. That's the first step to finding
the common ground.
I responded to the whole paragraph. Chiefly, it means I responded
to "this relies" and "It also relies". If I were to answer the
the "is it in the standard?" question, I'd use the word "standard"
in my reply somehow.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hi folks,
I am trying to develop a routine that will handle sphere-sphere and
sphere-triangle collisions and interactions. My aim is to develop a
quake style collision engine where a player can interact with a rich
3D environment. Seem to be 90% of the way there! My problems are
related to calculations where the result tends to zero (or another
defined limit.)
Have loads of cases where this kind of interaction occurs but this one
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.
in C there arent the simplest things present like constants, each struct and
enum...
|
by: John |
last post by:
Hi,
I encountered a strange problem while debugging C code for a
Windows-based application in LabWindows CVI V5.5, which led me to
write the test code below. I tried this code with a different compiler
and got the same erroneous result on two different PCs (with OS Win98
& Win98SE), so it appears to be a problem with ANSI C. I thought that
negative double variables could be compared as easily and *reliably*
as integers, but apparently...
|
by: michael.mcgarry |
last post by:
Hi,
I have a question about floating point precision in C.
What is the minimum distinguishable difference between 2 floating point
numbers? Does this differ for various computers?
Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be
10^-5. Does this mean that the computer cannot distinguish between 2
numbers that differ by less than this epsilon?
|
by: ma740988 |
last post by:
template <class T> inline bool isEqual( const T& a, const T& b,
const T epsilon = std::numeric_limits<T>::epsilon() )
{
const T diff = a - b;
return ( diff <= epsilon ) && ( diff >= -epsilon );
}
int main()
{
std::deque<double> pt ;
| |
by: John Smith |
last post by:
This code for the comparison of fp types is taken from the C FAQ.
Any problems using it in a macro?
/* compare 2 doubles for equality */
#define DBL_ISEQUAL(a,b) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a)))
Do the same issues involved in comparing 2 fp types for equality
apply to comparing a float to zero? E.g. is if(x == 0.0)
considered harmful?
|
by: Thomas Kowalski |
last post by:
Hi everyone,
To determine equality of two doubles a and b the following is often
done:
bool isEqual ( double a, double b ) {
return ( fabs (a-b) < THRESHOLD );
}
But this a approach usually fails if comparing doubles of different
magnitude since it's hard or not possible to find a suitable threshold
|
by: eman.abu.samra |
last post by:
Hi all,
i have encountered the strangest behavior. Check out this simple
program:
#include <stdio.h>
int main()
{
double time = 1;
|
by: saneman |
last post by:
I have a function:
int F(double a)
{
if (a = =1.0)
{
return 22;
}
return 44;
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |