473,671 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map<float, T>::find

Hello,

How does find works for a map where the key is float?

I know you cannot just simply compare floats for equality, you need to
use fabs and some epsilon. I know map does not use equality, but uses
"less". Still two floats (float a,b;) might show as a b when in fact
they are equal.

I am a bit confused.

Thanks,
Ray
Nov 5 '07 #1
2 3146
Rares Vernica wrote:
Hello,

How does find works for a map where the key is float?

I know you cannot just simply compare floats for equality,
Well, you can. It just so happens that it is _often_ not what you want to
do.
you need to use fabs and some epsilon.
Sometimes, that is what you want to do.
I know map does not use equality, but uses "less". Still two floats
(float a,b;) might show as a b when in fact they are equal.
Well, there are cases where one can get that impression. The standard allows
floating point numbers to have excess precision during computations.
However, operator< has a perfectly well-defined and natural meaning for
float objects (i.e., the corresponding regions in memory). If you make sure
that the floats that you compute are written to memory, they will compare
equal if and only if they are equal and operator< will do what you expect.
The trick part is making sure that the objects are written to memory.

Now, with regard to find(), one may hope that the keys are already written
to memory (if not, declaring the map object volatile or declaring the map
as map< volatile float, ...might do the trick). Still, we have to worry
about the parameter. I have recently learned on this list that it is enough
that the parameter is passed by value. The compiler is then required to
initialize the corresponding local object accordingly (though, I also
learned that many compilers get this wrong). Unfortunately, map::find()
takes the parameter as a const reference. What you could do is something
like this:

float rid_excess_prec ision ( float x ) {
return ( x );
}

and then call

my_map.find( rid_excess_prec ision( float x ) )

An orthogonal issue (and probably a more serious one) is how you obtain the
keys for which you want to search in the first place. Even if you get
around all the problems above, you will only find the entry in the map if
they match the stored values _exactly_ (and that is what a map is supposed
to do). With floats, it can be very tricky to compute the same value twice.
If you can guarantee precision of your computations only up to a certain
epsilon, then you might be better off using map< long, ... where you use

floor( x / epsilon )

as the key. (You could also come up with different schemes that do a more
logarithmic mapping if you need a better resolution near 0).
Best

Kai-Uwe Bux
Nov 5 '07 #2
Rares Vernica wrote:
Hello,

How does find works for a map where the key is float?

I know you cannot just simply compare floats for equality, you need to
use fabs and some epsilon. I know map does not use equality, but uses
"less". Still two floats (float a,b;) might show as a b when in fact
they are equal.

Nonsense. You're confused over the way floating point works. The
floating point relational operators for < and = are consistent. What
you may be thinking is not all numbers are exactly specifiable in the
limited precision of a machine floating point number and hence two
numbers that are close enough to be considered for some purposes as
the same are in fact slightly different.

If you have an algorithm that needs to consider this, you are free
to offer your own comparison function rather than std::less. However
BE ABSOLUTELY certain that it is consistent in the comparison,
especially transitivity.
Nov 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1773
by: sachin_mzn | last post by:
Hi, When we use find method over a STL map. which searching algorithm is used internally. Is it a hash search? Or map implementation internally decide depending on element. -Sachin
5
8735
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
13
4613
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
4
11712
by: lada77 | last post by:
All, Just wondering if one of you very helpful guru's out there could comment on some odd behaviour I am seeing. I'm betting it is something obvious but I am not experienced enough to tell right away. Here is my code snippet and the results that I am seeing: #include <map> #include <iostream> int
10
2965
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
3
2938
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
12
5823
by: jabbah | last post by:
Actually I'm quite sure I've missed something trivial here, but I just can't find it. Seemingly I cannot read from a const map& I try #include <iostream> #include <map> using namespace std;
4
3264
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain variables I'd like to display a state. So I wanted to use maps for quick look-ups... anyway here's my code for filling and verifying the contents of my map of maps: include<map> #define MAX_LENGTH 80; void gen_enums(void) { char varname =...
6
7366
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
8483
marktang
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...
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
jinu1996
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...
0
7445
agi2029
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...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.