小编Edu*_*rdo的帖子

C++中的long double是IEEE的binary128的实现吗?

来自https://en.wikipedia.org/wiki/Long_double

在 C++ 中,long double指的是通常比双精度更精确的浮点数据类型。然而,与 C++ 的其他浮点类型一样,它不一定映射到 IEEE 格式。

...

使用 GNU C 编译器,long double无论该类型使用何种物理存储(可以是 96 位或 128 位),x86 处理器上的精度都是 80 位。在某些其他架构上,long double可以是double-double(例如在 PowerPC 上)或 128 位四倍精度(例如在 SPARC 上)。从 gcc 4.3 开始,x86 上也支持四倍精度,但作为非标准类型__float128而不是long double.

Linux 上的 gcc 默认为 80 位扩展精度;在一些 BSD 操作系统(FreeBSD 和 OpenBSD)上,双精度模式是默认的,长双精度操作被有效地减少为双精度。

另一方面,面向 x86 的英特尔 C++ 编译器默认启用扩展精度模式。在 OS X 上,long double 是 80 位扩展精度。

看起来确实long double可能不是 IEEE 的 binary128 的实现,但为什么不这样做呢?为什么在某些情况下默认使用 80 位表示?

floating-point double ieee-754 long-double c++11

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

什么时候应该使用静态数据成员与const全局变量?

声明const全局变量已证明对确定API的某些功能参数很有用.例如,在我的API上,运算符的最小数值精度顺序为2; 因此,我宣布:

const int kDefaultOrderAccuracy{2};
Run Code Online (Sandbox Code Playgroud)

作为全球变量.将它作为static const描述这些运算符的类的公共数据成员会更好吗?一般来说,选择一个比另一个更好?

declaration global-variables static-members linkage c++11

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

Python单元测试:如何对包含数据库操作的模块进行单元测试?

我使用pymysql客户端库连接到真正的数据库.我在模块的功能,在这里我在python这个功能连接到使用pymysql数据库,并只做数据库插入operations.How单元测试没有击中真实的数据库?

import pymysql

def connectDB(self):

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db')

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('newuser@some.com', 'newpassword'))


    connection.commit()
Run Code Online (Sandbox Code Playgroud)

我的python版本是2.7.

python unit-testing python-2.7 python-unittest python-unittest.mock

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

理解 C++ 中的数值溢出

我试图更好地理解溢出在 C++ 中的行为。考虑以下MWE(必须检查整数文字):

#include <cstdint>
#include <iostream>
#include <iomanip>

int main() {
    uint64_t known    = 6049417284;  // Known solution to operation.
    uint32_t option_1 = 77778u;      // Using 32 bits for operands.
    uint64_t option_2 = 77778ull;    // using 64 bits for operands.

    uint64_t sol_option_1 = option_1*option_1;
    uint64_t sol_option_2 = option_2*option_2;

    std::cout << std::boolalpha << (sol_option_1 == known) << std::endl;
    std::cout                   << (sol_option_2 == known) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

执行:

false
true
Run Code Online (Sandbox Code Playgroud)

为什么它会溢出使用 32 位的操作数,即使我明确要求 64 位来接收解决方案也很困难?

我的印象是,在运行时,C++ 会创建一个临时右值,其精度来自操作数,即 32 位。此溢出,并将此溢出的结果复制到 …

c++ int integer-overflow fixed-width

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

为名称空间中定义的类重载ostream运算符(<<)

考虑bar.h:

#include <iostream>

namespace foo {

class Bar {
 public:
  friend std::ostream& operator <<(std::ostream& output, const Bar&);
 private:
  int xx_;
};

}
Run Code Online (Sandbox Code Playgroud)

考虑bar.cc:

#include "bar.h"

std::ostream& operator<<(std::ostream& output, const foo::Bar &in) {

  output << in.xx_ << std::endl;

  return output;
}
Run Code Online (Sandbox Code Playgroud)

为什么不能实现operator <<Bar类访问私有成员?那是:

$ icpc -c bar.cc
bar.cc(5): error #308: member "foo::Bar::xx_" (declared at line 9 of "bar.h") is inaccessible
    output << in.xx_ << std::endl;
                 ^

compilation aborted for bar.cc (code 2)
Run Code Online (Sandbox Code Playgroud)

我通过在 …

c++ namespaces operator-overloading ostream

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

为什么这个C代码产生双重免费或损坏?

为什么这个用于计算两个向量的内积的代码产生双重释放或损坏错误,当编译时:

ejspeiro@Eduardo-Alienware-14:~/Dropbox/HPC-Practices$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Run Code Online (Sandbox Code Playgroud)

代码来自此参考.

// Computation of the inner product of vectors aa and bb.

#include <stdio.h>
#include <stdlib.h>

int main() {

  size_t nn = 100000000;
  size_t total_mem_array = nn*sizeof(double);

  double *aa;
  double *bb;
  double ss = 0.0;

  aa = (double *) malloc(total_mem_array);
  bb = (double *) malloc(total_mem_array);

  int ii = 0;

  for (ii = 0; ii < nn; ++ii) {
    aa[ii] = 1.0;
    bb[ii] = 1.0;
  }

  double sum1 = 0.0;
  double …
Run Code Online (Sandbox Code Playgroud)

c memory-corruption double-free

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

C/Fortran混合代码以奇怪的方式使用MPI/ScaLAPACK seg故障

我试图将这个代码从Fortran 翻译成C.

这是我到目前为止:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "mpi.h"

#define PRINT_NOTHING             0
#define PRINT_UP_TO_MESGS         1
#define PRINT_UP_TO_ARRAYS        2
#define PRINT_UP_TO_MATRICES      3
#define PRINT_LEVEL               PRINT_UP_TO_MATRICES

/*! Parameters: */
#define TOTMEM                    425053208
#define INTMEM                    13107200
#define NTESTS                    20
#define DLEN_                     9
#define DBLESZ                    8               /*! Size of a DOUBLE PRE. */
#define MEMSIZ                    425053208/8     /*! Memory for DOUBLE PRE. */

#define MASTER                    0
#define MPI_ERROR_CODE_ERROR_GRID 1
#define MPI_ERROR_CODE_NOT_FACT   2
#define MPI_ERROR_CODE_NOT_SOLVE  3

#define BLACS_USER_WONT_FIN_MPI   0
#define …
Run Code Online (Sandbox Code Playgroud)

c fortran mpi segmentation-fault scalapack

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