小编Wil*_*tor的帖子

在python中打包和解包二进制浮点数

在进行二进制文件写入时,我在python中打包和解包二进制浮点数时遇到了一些麻烦.这是我做的:

import struct

f = open('file.bin', 'wb')
value = 1.23456
data = struct.pack('f',value)
f.write(data)
f.close()

f = open('file.bin', 'rb')
print struct.unpack('f',f.read(4))
f.close()
Run Code Online (Sandbox Code Playgroud)

我得到的结果如下:

(1.2345600128173828,)
Run Code Online (Sandbox Code Playgroud)

附加数字是怎么回事?这是一个舍入误差吗?这是如何运作的?

python file-io struct binaryfiles

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

具有常量参数的类声明中的类实例

我无法在另一个类的定义中声明具有常量参数的类的实例.

class Foo
{
    private:
        const int m_a, m_b;
    public:
        Foo(int a, int b) : m_a(a), m_b(b) {}
};

class Bar
{
    public:
        Foo foo1(1,2);
        Foo foo2(2,3);
};
Run Code Online (Sandbox Code Playgroud)

从这里我得到错误:

"error: expected identifier before numeric constant"
"error: expected ',' or '...' before numeric constant"
Run Code Online (Sandbox Code Playgroud)

c++ constructor class member-initialization

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

对于Numpy的循环速度

我试图让这段代码在python中快速运行但是我无法在MATLAB中运行的速度附近运行它.问题似乎是这个for循环,当数字"SRpixels"大约等于25000时,运行大约需要2秒.

我似乎无法找到任何方法来进一步削减这一点,我正在寻找建议.

下面的numpy数组的数据类型是float32,除了作为uint32的**_ Location []之外的所有数据类型.

for j in range (0,SRpixels):
    #Skip data if outside valid range
    if (abs(SR_pointCloud[j,0]) > SR_xMax or SR_pointCloud[j,2] > SR_zMax or SR_pointCloud[j,2] < 0):
        pass
    else:           
        RIGrid1_Location[j,0] = np.floor(((SR_pointCloud[j,0] + xPosition + 5) - xGrid1Center) / gridSize)
        RIGrid1_Location[j,1] = np.floor(((SR_pointCloud[j,2] + yPosition) - yGrid1LowerBound) / gridSize)

        RIGrid1_Count[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += 1
        RIGrid1_Sum[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += SR_pointCloud[j,1]
        RIGrid1_SumofSquares[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += SR_pointCloud[j,1] * SR_pointCloud[j,1]

        RIGrid2_Location[j,0] = np.floor(((SR_pointCloud[j,0] + xPosition + 5) - xGrid2Center) / gridSize)
        RIGrid2_Location[j,1] = np.floor(((SR_pointCloud[j,2] + yPosition) - yGrid2LowerBound) / …
Run Code Online (Sandbox Code Playgroud)

python numpy

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