getting wrong answer by logarithm in c++ - c++

Well i was creating a program in c++ .Then i noticed that there were some error due to which my program was not functioning correctly
compiler used:- Dev c++
Error was this:-
Suppose n1=274877906944 which is 2^38. so log(n1) should be 38 which is correct. now let n2=274877906942 which is smaller than n1.
So that means if we calculate log(n2) then it must give me less than log(n1).But log(n2) gave me the same result as log(n1) which is 38.
Which is wrong!!!
Someone please explain this stuff..

You see the result that you do because of rounding. If you increase your precision, it'll be okay (note that the log2 function in <cmath> will cast your integers to double):
std::cout << std::fixed;
std::cout << std::setprecision(16);
std::cout << static_cast<double>(274877906944) << std::endl;
std::cout << static_cast<double>(274877906942) << std::endl;
std::cout << static_cast<double>(274877906948) << std::endl;
std::cout << log2(274877906944) << std::endl;
std::cout << log2(274877906942) << std::endl;
std::cout<< log2(274877906948) << std::endl;
Produces:
274877906944.0000000000000000
274877906942.0000000000000000
274877906948.0000000000000000
38.0000000000000000
37.9999999999895053
38.0000000000209965
Demo

Related

"Double" is not printing more than 6 significant digits even after setprecision

I'm self learning C++ and for some reason "double" doesn't print more than 6 significant digits even after std::setprecision. Do I need to do something else? Most recent version of codeblocks if that helps. This is all the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
std::setprecision(9);
double A = 654321.987;
cout << A << endl;
return 0;
}
You need to feed the result of std::setprecision(9) to std::cout. Otherwise it has no way of knowing what output stream it applies to (and so it won't apply to anything).
std::cout << std::setprecision(9) << A << std::endl;
Or if you prefer you can do it separately:
std::cout << std::setprecision(9);
std::cout << A << std::endl;

boost rational cast to double

Using the following bit of code compiled against boost 1.62:
#include <boost/rational.hpp>
#include <iostream>
int main() {
auto val = boost::rational<int64_t>(499999, 2);
std::cout << val << std::endl;
std::cout << boost::rational_cast<double>(val) << std::endl;
}
I get the following output:
499999/2
250000
I would expect rational_cast to output 249999.5
Can anyone explain what I am doing wrong?
Modify the default formatting for floating-point input/output:
std::cout << std::fixed << boost::rational_cast<double>(v) << std::endl; add std::fixed to it.

is there an EIGEN equivalent for gsl_matrix_isnull?

Is there a function in Eigen for testing whether a matrix vanishes? Like gsl_matrix_isnull in GSL? I have searched for quite a while, but found nothing such.
If not, is there anything faster than straightforwardly checking each element?
Thank you and sorry if I overlooked it.
I don't think that there is an exact equivalent, but there are multiple ways of an equivalent expression. Consider the following:
Eigen::MatrixXd mt(4, 4);
std::cout << mt << "\n";
std::cout << mt.array().isApproxToConstant(0.0, 1e-15) << "\n";
mt *= 0.;
std::cout << (mt.array().abs() < 1e-15).all() << "\n";
std::cout << mt.array().isApproxToConstant(0.0, 1e-15) << "\n";
std::cout << mt.array().cwiseEqual(0.0).all() << "\n";
The isApproxToConstant compares all the elements to the first argument with a precision of the second. (mt.array().abs() < 1e-15).all() checks the same, but has to be near zero. If you want exactly 0, then use mt.array().cwiseEqual(0.0).all().
To complete Avi's answer, there is isZero(eps) which is essentially a shortcut to (mat.array().abs()<=eps).all(), for instance:
if(mat.isZero()) { ... }

std::cout gives different output from qDebug

I am using Qt, and I have an unsigned char *bytePointer and want to print out a number-value of the current byte. Below is my code, which is meant to give the int-value and the hex-value of the continuous bytes that I receive from a machine attached to the computer:
int byteHex=0;
byteHex = (int)*bytePointer;
qDebug << "\n int: " //this is the main issue here.
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
}
This gives perfect results, and I get actual numbers, however this code is going into an API and I don't want to use Qt-only functions, such as qDebug. So when I try this:
int byteHex=0;
byteHex = (int)*bytePointer;
std::cout << "\n int: " //I changed qDebug to std::cout
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
}
The output does give the hex-values perfectly, however the int-values return symbols (like ☺, └, §, to list a few).
My question is: How do I get std::cout to give the same output as qDebug?
EDIT: for some reason the symbols only occur with a certain Qt setting. I have no idea why it happened but it's fixed now.
As others pointed out in comment, you change the outputting to hex, but you do not actually set it back here:
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
You will need to apply this afterwards:
std::cout << std::dec;
Standard output streams will output any character type as a character, not a numeric value. To output the numeric value, convert to a non-character integer type:
std::cout << int(*bytePointer);

first setw with double is left aligned, subsequent are right aligned

I was trying to affirm my knowledge about how streams work with doubles and various manipulators, and stumbled upon G++ doing something strange:
int main() {
double v = 10.0/3.;
//std::cout << v << '\n';
std::cout << std::setw(5) << std::setprecision(2) << v << '\n';
std::cout << std::setw(5) << std::setprecision(2) << std::fixed << v << '\n';
}
Output:
3.3 //why is this left aligned?
3.33 //why is this right aligned? which is right?
See it live here
Then I uncommented that first cout and got different results!
3.33333 //which alignment is this?
3.3 //now this is right aligned?!
3.33 //that implies right-aligned is correct
Subsequent tests show that the first double I stream out is left aligned, and all subsequent doubles are right aligned:
double v = 10.0/3.;
std::cout << std::setw(10) << v << '\n';
std::cout << std::setw(5) << std::setprecision(2) << std::fixed << v << '\n';
std::cout << std::setw(5) << std::setprecision(2) << v << '\n';
std::cout << std::setw(5) << std::setprecision(2) << v << '\n';
Output:
3.33333 //well, this is left aligned
3.33
3.3
3.3 //all subsequent tests are right aligned
Clang++ on Coliru is doing the same thing, I presume because they're using the same library.
I know the answer to "is this a G++ bug" is no 99.9% of the time, so can someone potentially explain the behavior I'm seeing?
After running this on other IDEs I would conclude that this is a problem not with g++/clang++, nor is any standard behavior, but a problem with the Coliru editor itself. I ran this on several different sites including Rextester and Ideone and I got the correct output for all of them. This seems to be a problem only belonging to the Coliru editor.
Streams are, by default, right-justified. When setting the width using std::setw(), the stream will insert padding characters as specified by out.fill() into the beginning (or end) of the output stream. I've noticed that Coliru doesn't add any padding characters on the first output operation when the stream is using the default fill character (a simple space). But when I change the fill character to anything other than a space, it works just fine.