相关疑难解决方法(0)

我应该使用double还是float?

在C++中使用一个而不是另一个有什么优缺点?

c++ floating-point types double-precision

80
推荐指数
5
解决办法
8万
查看次数

对重载函数错误的奇怪模糊调用

我尝试着

void function(int y,int w)
{
    printf("int function");

}


void function(float y,float w)
{
    printf("float function");
}


int main()
{
    function(1.2,2.2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到一个错误错误,如..

error C2668: 'function' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)

当我尝试调用function(1.2,2)function(1,2.2)打印为" int函数 "时

请说清楚什么时候function(float y,float w)打电话?

c++ visual-c++

34
推荐指数
4
解决办法
7100
查看次数

为什么自动推断此变量为double而不是float?

在下面的代码片段中,auto将变量推导出为double,但是我想要float

auto one = 3.5;
Run Code Online (Sandbox Code Playgroud)

它是否始终double用于带小数点的文字?如何确定浮点数和两倍数?

c++ floating-point literals auto type-deduction

14
推荐指数
3
解决办法
872
查看次数

为什么sizeof(13.33)是8个字节?

当我给sizeof(a),其中a=13.33,浮子变量时,大小为4个字节.但如果我sizeof(13.33)直接给出,大小是8字节.

我不明白发生了什么.有人可以帮忙吗?

c c++ sizeof

8
推荐指数
5
解决办法
1640
查看次数

在C++中对float变量执行算术时是否总是需要使用float文字?

我看到很多C++代码都有如下行:

float a = 2;
float x = a + 1.0f;
float b = 3.0f * a;
float c = 2.0f * (1.0f - a);
Run Code Online (Sandbox Code Playgroud)

这些是.0f这些文字后,真的有必要吗?如果省略这些,你会失去数字准确性吗?

如果你有这样一条线,我认为你只需要它们:

float a = 10;
float x = 1 / a;
Run Code Online (Sandbox Code Playgroud)

你应该在哪里使用1.0f,对吗?

c++ floating-point performance floating-accuracy floating-point-precision

6
推荐指数
1
解决办法
222
查看次数

浮点数仅打印整数部分

我有一个这样的程序:

#include <iostream>
using namespace std;

int main ()
{
    double x;
    for (int i = 1; i < 100; i++)
    {
        cout << (x = i/100) << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在运行它时,我只得到:

0
0
0
[...]
Run Code Online (Sandbox Code Playgroud)

我在Cygwin-g ++和MinGW-g ++上测试了这个; 版本4.7.2.哪里可能是问题?

c++ floating-point

1
推荐指数
1
解决办法
198
查看次数

C++中的函数重载.不适用于float,适用于double

#include <iostream>

using namespace std;

int square(int x);
float square(float x);

int main() {
    cout<<square(3);
    cout<<square(3.14);

    return 0;
}

int square(int x) {
    cout<<"\nINT version called\n";
    return x*x;
}

float square(float x) {
    cout<<"\nFLOAT version called\n";
    return x*x;
}
Run Code Online (Sandbox Code Playgroud)

我试图用double替换函数的float版本,然后它开始工作.这里有什么问题?不能将3.14视为浮动?

错误:调用重载'square(double)'是模棱两可的
注意事项:候选者是:
注意:int square(int)
注意:float square(float)

c++ overloading

1
推荐指数
1
解决办法
1373
查看次数

如何在 C++ 中使用向量&lt;Point2f&gt;

我想使用此代码(源代码)在opencv中查找基本矩阵。

\n\n
int point_count = 100;\n    vector<Point2f> points1(point_count);\n    vector<Point2f> points2(point_count);\n\n    // initialize the points here ... */\n    for( int i = 0; i < point_count; i++ )\n    {\n        points1[i] = ...;\n        points2[i] = ...;\n    }\n\n    Mat fundamental_matrix =\n     findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我不知道如何在for循环中初始化points1、point2。\n我尝试过使用:

\n\n
points1[i] = 10.0;\npoints1[i] = (10.0, 20.0);\npoints1[i] = {10.0, 20.0};\n
Run Code Online (Sandbox Code Playgroud)\n\n

但出现这样的错误

\n\n
no match for \xe2\x80\x98operator=\xe2\x80\x99 (operand types are \xe2\x80\x98cv::Point_<float>\xe2\x80\x99 and \xe2\x80\x98double\xe2\x80\x99)\n
Run Code Online (Sandbox Code Playgroud)\n\n

在所有这些中。

\n

c++ opencv

1
推荐指数
1
解决办法
1万
查看次数

为什么编译器不会自动假设看似浮点const值为float

让我们看看以下函数重载声明

void funA(int);
void funA(float);
Run Code Online (Sandbox Code Playgroud)

然后我们调用这样的函数:

funA(1);          // this will be ok.
Run Code Online (Sandbox Code Playgroud)

然而,

funA(1.333)         // this will not ok..
Run Code Online (Sandbox Code Playgroud)

对于编译器,funA(int)和funA(float)将是不明确的..编译器将1.333值转换为整数(1)..虽然假设它是一个浮点值会更合适..

我正在使用g ++(GCC)4.8.2为什么编译器不会调用funA(float)呢?但是以下工作..

funA(static_cast<float>(1.333))
Run Code Online (Sandbox Code Playgroud)

c++ overloading

0
推荐指数
1
解决办法
85
查看次数

浮点数据类型(5.0)被分配8个字节而不是4个使用C中的sizeof运算符

为什么sizeof(5.0)我的机器为float数据类型分配了4个字节,因此返回8作为输出而不是4 ?默认情况下,是否考虑5.0double(内存分配= 8个字节),如果是的话,为什么会这样呢?

c sizeof

0
推荐指数
1
解决办法
65
查看次数