I thought that was what the boost multiprecision library took care of.
It was to use base 10. But see below, my demo code doesn't make sense to me.
I understand what 0.1f ends up looking like in binary, but I don't
understand how to get around the problem.
>> I don't want to just round, because I'll have other cases where rounding
>> might not be the best choice.
>
> If you can't switch to using cents, and thus integer types
> and arithmetic, you have to very carefully analyze what you
> actually need and write your code to do exactly what you want
> it to do. So what's the exact scenario where rounding won't
> do?
Well, rounding will probably do in most scenarios I can think of, even
though I'd have to round to a different decimal place depending what I
am dealing with. Money - 2 digits, Time clock punches - maybe 4,
performance timer - 8... something like that.
I was really hoping there was just some C++ equivalent to the decimal
type they have in other languages.
I read the other's posts about existance of libraries. I thought the
boost multiprecision was one such library.
>> What is a widely used C++ library for this kind of thing?
>> I see boost::multiprecision, but I'm not really clear on the
>> licenses bit of the documentation there.
>
> As fas as I can see the Boost license is rather liberal, allowing
> you to do whatever you want with their code (even including it
> into a closed source program and selling it). The more pressing
> question is if "multiprecision" is of any use in this case, since
> also a "multiprecision" value can't help a lot if your require-
> ments call for an infinite amount of memory for storing just a
> single number;-)
> Regards, Jens
>
I tried the following demo code and the output makes no sense to me:
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <iostream>
int main()
{
typedef boost::multiprecision::cpp_dec_float_50 Decimal;
Decimal numberBoost = 2.13f;
float numberFloat = 2.13f;
double numberDouble = 2.13;
std::string numberAsText("2.13");
std::cout << "The float number is: " << std::fixed <<
std::setprecision(12) << numberFloat << std::endl;
std::cout << "The double number is: " << std::fixed <<
std::setprecision(12) << numberDouble << std::endl;
std::cout << "The boost number is: " << std::fixed <<
std::setprecision(12) << numberBoost << std::endl;
std::istringstream converter(numberAsText);
converter >> numberDouble;
std::cout << "The number converted from string and back is " <<
numberDouble << std::endl;
return 0;
}
Output:
The float number is: 2.130000114441
The double number is: 2.130000000000
The boost number is: 2.130000114441
The number converted from string and back is 2.130000000000
The double seemed to print to console as the original number, while I
expected it wouldn't. The debugger say's it is holding 2.12999999
The boost number did not print to console matching the original number.
The float printed with a error in positive contrary to what the debugger
told me about the double, which had error in the negative.
I read over the link Victor posted, but I am honestly pretty lost now.
I thought I understood what was going on, but evidently I don't.