简体   繁体   English

C ++ unsigned int进行双重转换

[英]C++ unsigned int to double conversion

What is the right way of converting unsigned int to double ? unsigned int转换为double的正确方法是什么? I need this is for QCPCustomPlot for data setting, as it takes in QVector<double> precisely as parameter for creating a graph. 我需要这是用于QCPCustomPlot用于数据设置,因为它需要在QVector<double> 精确地参数创建的曲线图。

EDIT: Silly me. 编辑:愚蠢的我。 The 'memory leak' error issue was caused because I initialised QVector<double> x(time), y(ipv4int) wrongly. 由于我错误地初始化了QVector<double> x(time), y(ipv4int)导致了“内存泄漏”错误问题。 Changed the time value to the same as ipv4int (the data occurence frequency) and the x and y matched correctly. time值更改为与ipv4int相同(数据出现频率),并且xy正确匹配。

Done with the time variable, now the actual issue regarding the conversion. 完成time变量,现在是有关转换的实际问题。 How can I convert it to double format that holds value such as 1855919686 instead of in the format of 1.85592e+09 ? 如何将其转换为保留值的double 1855919686格式(例如1855919686而不是1.85592e+09的格式?

QCustomPlot requires double but it seems that the QVector<double> couldn't take in value like 1.85592e+09 QCustomPlot需要double但是QVector<double>似乎不能像1.85592e + 09那样取值

Updated code: 更新的代码:

QVector<double> x(i), y(totalIP); //i=236052
for(int o = 0; o <= i; o++){
    double dSec = arrayIPxTime[o][0] - startSecond; //arrayIPTime[o][0] holds time in second
    double dMin = dSec/60;
    double ipv4addr = arrayIPxTime[o][1]; //arrayIPTime[o][0] holds ipaddr in integer format
    x[o] = dMin;
    //y[o] = ipv4addr; this is the line that causes crash. 
    qDebug()<<"Count "<<o<<" time "<< x[o] <<" ipv4 "<<ipv4addr<<" arrayIPxTime[o][1] "<<arrayIPxTime[o][1];
}

Current output: 电流输出:

Count  236048  time  62.3167  ipv4  1.85592e+09  arrayIPxTime[o][1]  1855919686
Count  236049  time  62.3167  ipv4  1.85592e+09  arrayIPxTime[o][1]  1855919686
Count  236050  time  62.3167  ipv4  1.85592e+09  arrayIPxTime[o][1]  1855919686
Count  236051  time  62.3167  ipv4  1.85592e+09  arrayIPxTime[o][1]  1855919686

Rest assured that converting an unsigned int to a double does not cause a memory leak. 放心,一个转换unsigned intdouble 引起了内存泄漏。

Writing double b = a; double b = a; is more than sufficient, or just pass a into the function that requires a double as a parameter. 绰绰有余,或者只是将a传递a需要将double用作参数的函数。

Note that the conversion is exact for an unsigned int up to the 53rd power of 2, for an IEEE754 double precision floating point type. 请注意,对于IEEE754双精度浮点类型,此转换对于不超过2的53次幂的unsigned int精确的

What's the value of time ? time的价值是什么? It looks like you do: 看起来像您这样做:

QVector<double> x(time);
// [...]
x[o] = dMin;

If time == 166, no doubt x[166] is going to fail, since the vector is initialized with a size of time ! 如果time == 166,无疑x[166]将失败,因为矢量是用time大小初始化的!

And by the way, nothing related to casting to double . 顺便说一句,与铸造double无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM