我试图编译ExprTk库的精度比long double.我认为简化会尝试GCC,__float128但我得到以下编译错误,我不知道如何纠正它.
exprtk.hpp: In instantiation of ‘static T exprtk::details::and_op<T>::process(exprtk::details::and_op<T>::Type, exprtk::details::and_op<T>::Type) [with T = __float128; exprtk::details::and_op<T>::Type = const __float128&]’:
exprtk.hpp:28439:10: required from ‘void exprtk::parser<T>::load_binary_operations_map(exprtk::parser<T>::binary_op_map_t&) [with T = __float128; exprtk::parser<T>::binary_op_map_t = std::map<exprtk::details::operator_type, __float128 (*)(const __float128&, const __float128&), std::less<exprtk::details::operator_type>, std::allocator<std::pair<const exprtk::details::operator_type, __float128 (*)(const __float128&, const __float128&)> > >; typename exprtk::details::functor_t<T>::bfunc_t = __float128 (*)(const __float128&, const __float128&)]’
exprtk.hpp:15660:51: required from ‘exprtk::parser<T>::parser(std::size_t) [with T = __float128; std::size_t = long unsigned int]’
mathtof.cpp:18:33: required from here
exprtk.hpp:9923:105: error: call of overloaded ‘is_true(const …Run Code Online (Sandbox Code Playgroud) 如果我想编译我的项目-flto是否足以建立gcc --enable-gold或者我还需要构建黄金并用它替换ld?我还需要其他标志吗?即我这样做
gcc -flto one.c two.c
Run Code Online (Sandbox Code Playgroud) 当从C++ 11的开头迭代std::vector到第二个到最后一个元素时,首选的样式是什么?
std::vector<const char*> argv;
std::string str;
Run Code Online (Sandbox Code Playgroud)
应该使用这种更多的C++ - esque方法
for (const auto& s: decltype(argv)(argv.begin(), argv.end()-1)) {
str += std::string(s) + ' ';
}
Run Code Online (Sandbox Code Playgroud)
还是应该采用更传统的方式?
for (size_t i = 0; i < argv.size() - 1; ++i) {
str += std::string(argv[i]);
}
Run Code Online (Sandbox Code Playgroud) 我一直在浏览本Ada 95教程。我读到的是,可以定义一个范围与标准范围不同的类型,如果程序试图超出该范围,则将引发错误。在我自己的程序上工作时,我注意到,如果定义中范围的末尾落在其基础类型的边界上,那么当分配超出该范围的值时,程序将不会提高CONSTRAINT_ERROR。取而代之的是它将快乐地继续前进,然后环绕。我编写了一个程序来明确显示这一点。
有人知道解释这种行为的Ada规则吗?
-柯克
这是我终端的输出,源代码在下面。
me@acheron:~/Dropbox/programs/ada$ gnatmake constraints.adb -f
gcc-4.6 -c constraints.adb
gnatbind -x constraints.ali
gnatlink constraints.ali
me@acheron:~/Dropbox/programs/ada$ ./constraints
Type ON has size: 7
It has a min/max of: 0 127
It's base has a min/max of: -128 127
Type UNDER has size: 7
It has a min/max of: 0 126
It's base has a min/max of: -128 127
The value of No_Error is: 245
raised CONSTRAINT_ERROR : constraints.adb:58 range check failed
me@acheron:~/Dropbox/programs/ada$
Run Code Online (Sandbox Code Playgroud)
源代码:
with Ada.Text_IO, Ada.Integer_Text_IO;
use …Run Code Online (Sandbox Code Playgroud) 在Windows上的Python中,我可以创建一个大文件
from mmap import mmap
f = open('big.file', 'w')
f.close()
f = open('big.file', 'r+')
m = mmap(f.fileno(), 10**9)
Run Code Online (Sandbox Code Playgroud)
现在big.file是(约)1千兆字节.但是,在Linux上,这将返回ValueError: mmap length is greater than file size.
有没有办法在Linux上获得与Windows相同的行为?也就是说,能够使用mmap?来增加文件的大小?
有没有比内置numpy.random.rand(count)函数更快的方法来获取填充随机数的 numpy 数组?我知道内置方法是使用 Mersenne Twister。
我想使用 numpy 进行蒙特卡罗模拟,并且获取随机数需要花费很大一部分时间。一个简单的例子,通过蒙特卡罗积分与 200E6 随机数计算 pi,通过我的程序仅处理约 116.8 MB/s。使用 xor128() 作为生成器的 C++ 编写的类似程序可处理数百 MB/秒。
编辑:错误计算的发电率
当我尝试使用以下 Cython 代码时,我收到了我在最后发布的关于operator()未定义的错误。当我尝试使用运算符时,Cython 似乎没有将其解释为成员函数(注意 C++ 源代码中没有成员访问)。如果我尝试调用,prng.operator()()那么 Cython 将无法翻译。
在 Cython 中使用运算符重载是否有特殊需要?
import numpy as np
cimport numpy as np
cdef extern from "ratchet.hpp" namespace "ratchet::detail":
cdef cppclass Ratchet:
Ratchet()
unsigned long get64()
cdef extern from "float.hpp" namespace "prng":
cdef cppclass FloatPRNG[T]:
double operator()()
cdef FloatPRNG[Ratchet] prng
def ratchet_arr(np.ndarray[np.float64_t, ndim=1] A):
cdef unsigned int i
for i in range(len(A)):
A[i] = prng()
def ratchet_arr(np.ndarray[np.float64_t, ndim=2] A):
cdef unsigned int i, j
for i in range(len(A)):
for j …Run Code Online (Sandbox Code Playgroud) 我从维基百科(下面看到的树)下载了一个测试图像,以在 python 中进行比较Pillow和OpenCV(使用cv2)。从感知上看,这两个图像看起来相同,但它们各自的md5哈希值不匹配;如果我减去两个图像,结果甚至不接近纯黑色(原始图像下方显示的图像)。原始图像是 JPEG。如果我先将其转换为 PNG,则哈希值匹配。
最后一张图显示了像素值差异的频率分布。
正如Catree 指出我的减法导致整数溢出。我更新为dtype=int在减法之前也进行转换(以显示负值),然后在绘制差异之前取绝对值。现在差异图像在感知上是纯黑色的。
这是我使用的代码:
from PIL import Image
import cv2
import sys
import md5
import numpy as np
def hashIm(im):
imP = np.array(Image.open(im))
# Convert to BGR and drop alpha channel if it exists
imP = imP[..., 2::-1]
# Make the array contiguous again
imP = np.array(imP)
im = cv2.imread(im)
diff = im.astype(int)-imP.astype(int)
cv2.imshow('cv2', im)
cv2.imshow('PIL', imP)
cv2.imshow('diff', np.abs(diff).astype(np.uint8))
cv2.imshow('diff_overflow', diff.astype(np.uint8))
with open('dist.csv', 'w') as …Run Code Online (Sandbox Code Playgroud) 我编写了一个模板类,用于在整数中存储多个bool.现在,设置和获取每个bool都是通过显式函数完成的
bool isBitSet(int index)
{
return static_cast<bool>((block_ >> index) % 2)
}
void setBitOn(int index)
{
block_ |= 1 << index;
}
Run Code Online (Sandbox Code Playgroud)
我相信以下内容对于获取值是有效的,但是如果设置工作,我们不能直接返回一点引用?
const bool operator [] (int index) const
{
return static_cast<bool>((block_ >> index) % 2);
}
Run Code Online (Sandbox Code Playgroud) 我的印象是,在Python中,原始字符串的编写方式r'this is a raw string'会省略任何转义字符,并准确打印引号之间的内容。我的问题是,当我尝试时,print r'\'我得到了SyntaxError: EOL while scanning string literal。print r'\n'不过,打印正确\n。