Solved

how to handle  float numbers as keys to MAP containr?

Posted on 2011-02-17
11
243 Views
Last Modified: 2012-05-11
Hi,
I read Data to my program, numbers like 1.3434 100.34  etc
I put them inside floats variables.
1.3434 can be stored in the memory as 1.3433999 or 1.3433998 etc.

1) I want to use these numbers as keys in a map container.
How can I do it?
For example I want to put 1.3434 as a key in a MAP container,When I do calculations I get this number as 1.34339998 so converting it to 1.3434 would not be easy because I have many data types and I need to check how many digits after the decimal point each one of them has.

2) Are there special libraries for Visual C++ 2010 to represent these numbers in a different form ? (not as floats)
Thanks
0
Comment
Question by:star90
  • 6
  • 5
11 Comments
 

Author Comment

by:star90
ID: 34915667
I want to add that the data is from the Forex market.
Maybe there are special libraries to handle these numbers?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 350 total points
ID: 34915703
Two options come to mind :

(a) Don't use floating point values. A fixed point data type doesn't have these comparison issues (and avoids other rounding/precision related issues as well).

(b) Specify a custom comparator for the map that compares two floats with an epsilon (ie. maximal difference between the two values that would still consider both equal).
0
 

Author Comment

by:star90
ID: 34915778
Hi Infinity08,
1) How do I use fixed point data type in VC++? How do I declare them?
2) Can you please give me an example for f the custom comparator for the map?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34915839
>> 1) How do I use fixed point data type in VC++? How do I declare them?

You can either implement your own, or find an existing implementation that suits your needs.


>> 2) Can you please give me an example for f the custom comparator for the map?

Sure, something like :
struct CompareFloat {
  bool operator()(const float& left, const float& right) const {
    return (left < (right - 0.0001f));
  }
};

std::map<float, Data, CompareFloat> myMap;

Open in new window

0
 

Author Comment

by:star90
ID: 34915898
If I write this fixed point data type then I need also to write the basic operations like - * + == etc?
Why is it not part of the C++ language or the Visual C++ libraries?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:Infinity08
ID: 34915934
>> If I write this fixed point data type then I need also to write the basic operations like - * + == etc?

Yes.


>> Why is it not part of the C++ language or the Visual C++ libraries?

There are plenty of things that could have been in the C++ language heh. In the end, a decision was made as to what was included in it, and what was left to extensions or third party libraries. Fixed point datatypes are not commonly included by default in programming languages.

I don't know if an implementation is availabel in Visual C++ - I don't use it enough for that heh.
0
 

Author Comment

by:star90
ID: 34915946
Thanks.
0
 

Author Comment

by:star90
ID: 34916091
Assuming I write a fixed point data type using Lists of one digit numbers.
How do I tell MAP to recognize it?
Do I need to write a comparator for it? or how is it done?
0
 

Author Comment

by:star90
ID: 34916197
OK I thought about it I understand that I need to write a comparator.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34916443
>> Do I need to write a comparator for it? or how is it done?

You can implement an operator== for your new data type, and that would be sufficient.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34916450
Sorry, I meant operator<
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now