star90
asked on
how to handle float numbers as keys to MAP containr?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
>> 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 :
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;
ASKER
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?
Why is it not part of the C++ language or the Visual C++ libraries?
>> 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.
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.
ASKER
Thanks.
ASKER
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?
How do I tell MAP to recognize it?
Do I need to write a comparator for it? or how is it done?
ASKER
OK I thought about it I understand that I need to write a comparator.
>> 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.
You can implement an operator== for your new data type, and that would be sufficient.
Sorry, I meant operator<
ASKER
Maybe there are special libraries to handle these numbers?