I want to multiply number 123456789.123456789 by 1000000000.0 and as a result of this operation I expect 123456789123456789 as int or float 123456789123456789.0, but I got:
res: 123456789123456791.04328155517578125
int_res: 123456789123456791
Should I do it in other way ?
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace bmp = boost::multiprecision;
int main()
{
bmp::cpp_dec_float_100 scalar{1000000000.0};
bmp::cpp_dec_float_100 a{123456789.123456789};
bmp::cpp_dec_float_100 res = a * scalar;
bmp::cpp_int int_res = res.convert_to<bmp::cpp_int>();
std::cout << " res: " << res.str() << std::endl;
std::cout << "int_res: " << int_res.str() << std::endl;
return 0;
}
Code: https://wandbox.org/permlink/xB8yBWuzzGvQugg7
See https://www.boost.org/doc/libs/1_79_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/fp_eg/floatbuiltinctor.html.
You are initialising a with a double literal. 123456789.123456789 can't be represented by a double so you get the closest approximation which is 123456789.12345679104328155517578125. If you want to initialise precisely use a string literal instead:
bmp::cpp_dec_float_100 a{"123456789.123456789"};
You are using the double literal to initialise a which can't give you the value you want for the obvious reason, use String literal instead, it will be good to go.
Related
When I execute this code the value of ans1, ans2 is 50002896 and 50005000.
I know there is some issues with ceil function but was not able to figure out the exact cause.
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long ans1 = 0, ans2 = 0;
for (long long i = 1; i <= 10000; i++)
{
ans1 = ans1 + ceil((float)i / 1);
ans2 = ans2 + i;
}
cout << ans1 << " " << ans2 << endl;
}
The source of the problem is not the ceil function, but rather that not all integers can be represented accuratly as floating point values.
Some more info about floating point representation: Wikipedia IEEE 754. And a related post: Which is the first integer that an IEEE 754 float is incapable of representing exactly?.
The following code is a minimal demonstration of the same issue that causes your issue:
float f1 = 100000000;
f1++;
std::cout << std::to_string(f1) << std::endl;
[Wrong] Output (expected: +1):
100000000.000000
One approach would be to use double instead of float.
This will not solve the principle issue, but will make the range of representable integers a lot bigger:
double f1 = 100000000;
f1++;
std::cout << std::to_string(f1) << std::endl;
Output:
100000001.000000
Some side notes:
better to avoid #include <bits/stdc++.h> - see here: Why should I not #include <bits/stdc++.h>?.
better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
First, try to use specific headers like #include , in this case, .because #include <bits/stdc++.h> will bring lots of junk.
So the issue is with float not ceil explained below
floating-point values do not represent exact values.
Code:-
#include <iostream>
#include <iomanip>
using namespace std;
// Driver Code
int main()
{
float num1 = 10000.29;
float num2 = 10000.2;
// Output should be 0.0900000000
cout << std::setprecision(15)
<< (num1 - num2);
return 0;
}
Output :-
0.08984375
So I'm trying to learn more about C++ and I'm practicing by making a calculator class for the quadratic equation. This is the code for it down below.
#include "QuadraticEq.h"
string QuadraticEq::CalculateQuadEq(double a, double b, double c)
{
double sqrtVar = sqrt(pow(b, 2) - (4 * a * c));
double eqPlus = (-b + sqrtVar)/(2 * a);
double eqMinus = (-b - sqrtVar) / (2 * a);
return "Your answers are " + to_string(eqPlus) + " and " + to_string(eqMinus);
}
I'm trying to make it so that the double variables eqPlus and eqMinus have only two decimal points. I've seen people say to use setprecision() but I've only seen people use that function in cout statements and there are none in the class because I'm not printing a string out I'm returning one. So what would I do here? I remember way before learning about some setiosflags() method, is there anything I can do with that?
You can use stringstream instead of the usual std::cout with setprecision().
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
std::string adjustDP(double value, int decimalPlaces) {
// change the number of decimal places in a number
std::stringstream result;
result << std::setprecision(decimalPlaces) << std::fixed << value;
return result.str();
}
int main() {
std::cout << adjustDP(2.25, 1) << std::endl; //2.2
std::cout << adjustDP(0.75, 1) << std::endl; //0.8
std::cout << adjustDP(2.25213, 2) << std::endl; //2.25
std::cout << adjustDP(2.25, 0) << std::endl; //2
}
However, as seen from the output, this approach introduces some rounding off errors when value cannot be represented exactly as a floating point binary number.
I am trying this:
std::cout << boost::lexical_cast<std::string>(0.0009) << std::endl;
and expecting the output to be:
0.0009
But the output is:
0.00089999999999999998
g++ version: 5.4.0, Boost version: 1.66
What can I do to make it print what it's been given.
You can in fact override the default precision:
Live On Coliru
#include <boost/lexical_cast.hpp>
#ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
# error unsupported
#endif
template <> struct boost::detail::lcast_precision<double> : std::integral_constant<unsigned, 5> { };
#include <string>
#include <iostream>
int main() {
std::cout << boost::lexical_cast<std::string>(0.0009) << std::endl;
}
Prints
0.0009
However, this is both not supported (detail::) and not flexible (all doubles will come out this way now).
The Real Problem
The problem is loss of accuracy converting from the decimal representation to the binary representation. Instead, use a decimal float representation:
Live On Coliru
#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <string>
#include <iostream>
using Double = boost::multiprecision::cpp_dec_float_50;
int main() {
Double x("0.009"),
y = x*2,
z = x/77;
for (Double v : { x, y, z }) {
std::cout << boost::lexical_cast<std::string>(v) << "\n";
std::cout << v << "\n";
}
}
Prints
0.009
0.009
0.018
0.018
0.000116883
0.000116883
boost::lexical_cast doesn't allow you to specify the precision when converting a floating point number into its string representation. From the documentation
For more involved conversions, such as where precision or formatting need tighter control than is offered by the default behavior of lexical_cast, the conventional std::stringstream approach is recommended.
So you could use stringstream
double d = 0.0009;
std::ostringstream ss;
ss << std::setprecision(4) << d;
std::cout << ss.str() << '\n';
Or another option is to use the boost::format library.
std::string s = (boost::format("%1$.4f") % d).str();
std::cout << s << '\n';
Both will print 0.0009.
0.0009 is a double precision floating literal with, assuming IEEE754, the value
0.00089999999999999997536692664112933925935067236423492431640625
That's what boost::lexical_cast<std::string> sees as the function parameter. And the default precision setting in the cout formatter is rounding to the 17th significant figure:
0.00089999999999999998
Really, if you want exact decimal precision, then use a decimal type (Boost has one), or work in integers and splice in the decimal separator yourself. But in your case, given that you're simply outputting the number with no complex calculations, rounding to the 15th significant figure will have the desired effect: inject
std::setprecision(15)
into the output stream.
I wanted to do calculation among large integers and double, for example,
1245.....889 * 3.14
I think we cannot construct a cpp_int from 3.14 because of
http://www.boost.org/doc/libs/1_56_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html
Also I am not sure if I can use cpp_dec_float because cpp_dec_float needs to specify the number of significant bits which cannot be arbitrarily large.
Does it mean I should use cpp_rational? But I have to convert 3.14 in a rational number first like?
how can I extract the mantissa of a double
Do we have any better way to represent double like 3.14 and large int together?
Thank you,
Your question seems amply confused, but here goes:
You can use the gmp_float with dynamic precision by specifying 0 for the precision:
Live On Coliru
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
using Int = boost::multiprecision::cpp_int;
using Float = boost::multiprecision::number<boost::multiprecision::gmp_float<0>>;
Float fake_pi;
boost::multiprecision::default_ops::calc_pi(fake_pi.backend(), 2000);
Int value("12345678901234567890123456789012345678901234567890");
std::cout << std::fixed << value << " * " << fake_pi << " = " << Float(value.convert_to<Float>() * fake_pi);
}
Prints
12345678901234567890123456789012345678901234567890 * 3.141593 = 38785094139697029053093797030280437291228399875653.959648
#include <iostream>
int main()
{
float test = 12535104400;
std::cout << test;
std::cin.get();
return 0;
}
//on msvc 2010 this ouputs: 1.25351e+010
I would like it to output just "12535104400" or in other words, the human readable format which has no leading zeros, but outputs the full value of a number.
The particular number cannot be accurately represented, for example try the following:
float v = 12535104400;
cout.precision(0);
cout << fixed << v << endl;
You'll see it outputs: 12535104512
You will need to include <iomanip> :
int main()
{
const double test = 12535104400;
std::cout << std::fixed << std::setprecision(0) << test;
std::cin.get();
return 0;
}
std::fixed is the manipulator which uses fixed-point precision (not scientific notation)
std::setprecision(0) sets how many digits to display after the decimal point
float test = 12535104400;
This should be a compiler error if your compiler doesn't support long long and int is 32-bit. Use floating literals instead of integer literals e.g 1234.0f vs 1234
#include <iostream>
#include <iomanip>
int main()
{
float test = 12535104400.0f;
std::cout << std::setiosflags(ios::fixed) << std::setprecision(0) << test;
std::cin.get();
return 0;
}
should print what you want. But beware that float isn't that precise
You are out of luck, 4-byte float can store cca 7 digits. Use double or long for such numbers.
In order to format the output in iostream, you'll need manipulators
If you're willing to lose precision, you can typecast it to an integer.
cout << int(test);
or
cout << (int)test;