在过去的几周中,我的代码中出现了一个令人沮丧的错误。我的代码可以完全按照我的计算机上的预期运行,但是一旦将其移植到HPC服务器上,它就会产生奇怪的结果。
我将其归结为:在我的计算机(iMac)上,该函数abs()使用浮点数,但在服务器上将其abs()截断为整数。
例:
服务器
abs(-1.1341234) = 1
Run Code Online (Sandbox Code Playgroud)
我的Mac
abs(-1.1341234) = 1.1341234
Run Code Online (Sandbox Code Playgroud)
现在我知道我可以使用解决此fabs()问题,这不是问题。我看了gcc两台机器上的版本,这是输出:
服务器
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/software/GCCcore/5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-gold=default --enable-plugins --enable-ld --with-plugin-ld=ld.gold --prefix=/apps/software/GCCcore/5.4.0 --with-local-prefix=/apps/software/GCCcore/5.4.0 --enable-bootstrap --with-isl=/dev/shm/GCCcore/5.4.0/dummy-/gcc-5.4.0/stage2_stuff
Thread model: posix
gcc version 5.4.0 (GCC)
Run Code Online (Sandbox Code Playgroud)
我的Mac
g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,为什么abs()在gcc和clang之间会产生不同的结果?从字面上看,这个问题使我花了3个星期的时间,所以可以想像我刚才有点咸...
我正在尝试用C++编写的代码.我有一个内部和外部循环,我想分开时间,但同时.由于某种原因,当我这样做时,其中一个实例返回1.84467e + 13并且总是这个确切的数字.
为什么会这样?
这是一个在我的机器上复制效果的最小工作示例:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
long int i, j;
clock_t start, finish, tick, tock;
double a = 0.0;
double adding_time, runtime;
start = clock();
for(i=0; i<10; i++)
{
a=0.0;
tick =clock();
for(j=0; j<10000000; j++)
{
a+=1;
}
tock= clock();
adding_time = (double)(tick - tock)/CLOCKS_PER_SEC;
cout << "Computation time:" << adding_time << endl;
}
finish = clock();
runtime = (double)(finish - start)/CLOCKS_PER_SEC;
cout << "Total computation time:" << runtime …Run Code Online (Sandbox Code Playgroud) 我对 Matlab 中的 OOP 是全新的,并且总体上对 OOP 还很陌生,但我所知道的东西是我在 C++ 中学到的。
我正在关注此处找到的 Matlab 文档属性类和大小验证。我想验证一个属性,以便它必须是一个特定的类,并且我正在使用链接中的示例。这就是我的班级的样子:
classdef simpoint
...
properties
...
outputType dataType
...
end
...
end
Run Code Online (Sandbox Code Playgroud)
在我的代码中dataType是我编写的一个类。更重要的是它是抽象的。
我收到错误
Error defining property 'outputType' of class 'simpoint':
Class dataType is abstract. Specify a default value for property outputType.
Run Code Online (Sandbox Code Playgroud)
该类dataType是抽象的,以强制用户实现某些方法。我正在尝试使用属性验证来确保outputType设置时,该类是dataType.
我真的不想设置默认值,因为忘记设置outputType会引发错误。
我如何验证outputType以确保它是 的子类dataType?在Matlab中有没有更好的方法来做到这一点?