有没有一种简单的方法来确定浮点数的符号?
我进行了实验并想出了这个:
#include <iostream>
int main(int argc, char** argv)
{
union
{
float f;
char c[4];
};
f = -0.0f;
std::cout << (c[3] & 0x10000000) << "\n";
std::cin.ignore();
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其中(c [3]&0x10000000)为负数给出的值> 0但我认为这要求我做出以下假设:
如果任何这些假设错误或者我错过了任何假设,请纠正我.
我一直在制作一个矩阵类(作为一个学习练习),并且在测试我的反函数时遇到了问题.
我输入一个任意矩阵:
2 1 1
1 2 1
1 1 2
Run Code Online (Sandbox Code Playgroud)
并得到它来计算逆,我得到了正确的结果:
0.75 -0.25 -0.25
-0.25 0.75 -0.25
-0.25 -0.25 0.75
Run Code Online (Sandbox Code Playgroud)
但当我尝试将两者相乘以确保我得到单位矩阵时,我得到:
1 5.5111512e-017 0
0 1 0
-1.11022302e-0.16 0 1
Run Code Online (Sandbox Code Playgroud)
为什么我得到这些结果?我会理解,如果我将奇怪的数字乘以我可以理解的一些舍入误差,但它所做的总和是:
2 * -0.25 + 1 * 0.75 + 1 * -0.25
Run Code Online (Sandbox Code Playgroud)
这显然是0,而不是5.111512e-017
如果我手动让它进行计算; 例如:
std::cout << (2 * -0.25 + 1 * 0.75 + 1 * -0.25) << "\n";
Run Code Online (Sandbox Code Playgroud)
我按预期得到0?
所有数字都表示为双打.这是我的多重过载:
Matrix operator*(const Matrix& A, const Matrix& B)
{
if(A.get_cols() == B.get_rows())
{
Matrix temp(A.get_rows(), B.get_cols());
for(unsigned m …Run Code Online (Sandbox Code Playgroud) 我试图在一个单独的线程上运行模板函数,但IntelliSense(VC++ 2010 Express)一直给我错误:"错误:没有构造函数的实例"boost :: thread :: thread"匹配参数列表"如果我尝试编译我得到这个错误:"错误C2661:'boost :: thread :: thread':没有重载函数需要5个参数"
错误只发生在我添加模板后所以我确定它与它们有关但我不知道是什么.
我传递给boost :: thread的两个参数是定义为的模板函数:
template<class F>
void perform_test(int* current, int num_tests, F func, std::vector<std::pair<int, int>>* results);
Run Code Online (Sandbox Code Playgroud)
和:
namespace Sort
{
template<class RandomAccessIterator>
void quick(RandomAccessIterator begin, RandomAccessIterator end);
} //namespace Sort
Run Code Online (Sandbox Code Playgroud)
我试着像这样调用boost :: thread:
std::vector<std::pair<int, int>> quick_results;
int current = 0, num_tests = 30;
boost::thread test_thread(perform_test, ¤t, num_tests, Sort::quick, &quick_results);
Run Code Online (Sandbox Code Playgroud) std :: vector在创建它所包含的对象的新实例时会调用哪个构造函数?我的印象是它调用了默认构造函数但是如果没有定义或者编译器为我做了什么呢?
特别是在这样的情况下:
class Foo
{
public:
Foo(int size)
{
data = new double[size];
}
~Foo()
{
delete[] data;
}
private:
double* data;
};
std::vector<Foo> myVector;
Foo bar(5);
myVector.push_back(bar);
//stuff
Run Code Online (Sandbox Code Playgroud)
当对象具有未知大小直到构建之后,它如何知道要分配多少内存?
c++ ×3
boost ×1
boost-thread ×1
constructor ×1
math ×1
object ×1
signedness ×1
templates ×1
vector ×1