小编Boa*_*ann的帖子

为什么在C++中这个double值乘以2会出现计算错误?

#include <iostream>
#include <stdint.h>
#include <bitset>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
    uint64_t int_value = 0b0000000000001101000011110100001011000000110111111010111101110011;
    double double_value = (*((double *)((void *)&int_value)));
    printf("double initiate value: %e\n", double_value);
    cout << "sign " << setw(11) << "exp"
         << " " << setw(52) << "frac" << endl;
    for (int i = 0; i < 10; i++)
    {
        stringstream ss;
        ss << bitset<64>((*((uint64_t *)((void *)&double_value))));
        auto str = ss.str();
        cout << setw(4) << str.substr(0, 1) << " " …
Run Code Online (Sandbox Code Playgroud)

c++ ieee-754 calculation

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

std::greater&lt;&gt; 如何在集合中工作?

我知道 std::greater 是如何工作的。但是,当我阅读自 C++14 以来 std::greater 的 API 时,它的默认类型为 void。因此,如果我们不将任何模板参数传递给更大的它默认为 void,如下所示。但结果是按降序排列的。

#include <iostream>
#include <set>

template< class T = void >
struct greater;

int main()
{
    std::set<int, std::greater<>> s {4, 5, 6, 7}; // This transforms to std::set<int, std::greater<void>>
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释这种专业化是如何运作的吗?

c++ c++14

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

为什么有些库使用非常量 char * 作为函数参数?

有时在我的 C++ 项目中使用纯 C 库,我会看到奇怪的(在我看来)函数声明。

例如:libldap 的ldap_search_ext()https ://linux.die.net/man/3/ldap_search_ext_s

int ldap_search_ext(
       LDAP *ld,
       char *base,
       int scope,
       char *filter,
       char *attrs[], // this one!
       int attrsonly,
       LDAPControl **serverctrls,
       LDAPControl **clientctrls,
       struct timeval *timeout,
       int sizelimit,
       int *msgidp );
Run Code Online (Sandbox Code Playgroud)

为什么 attrs[] 不能是 a const char *

像这样的声明不想改变指针的内容并产生很多问题:

// pure C
void func(char * data[])
{
  ...
}

func({"blabla"}); // won't work (corrected: yes, this is wrong syntax, but it's true for structs of pointers)

const char *d[] = …
Run Code Online (Sandbox Code Playgroud)

c c++ libs

4
推荐指数
2
解决办法
151
查看次数

为什么 ++(后增量运算符)不能是左值?

代码

#include<iostream>
int main()
{
    int a=3;
    a++=5;
    std::cout<<a;
}
Run Code Online (Sandbox Code Playgroud)

输出(如预期)

[Error] lvalue required as left operand of assignment
Run Code Online (Sandbox Code Playgroud)

1.后增量运算符 ( a++) 在表中具有最高优先级。所以它肯定会在赋值运算符 ( =)之前执行。并且根据后增量规则,变量的值a只有在执行该语句后才会增加。

那么当后增量运算符 ( ++) 在赋值运算符 ( =)之前执行时究竟会发生什么?

2.C 中,前自增运算符和后自增运算符都产生右值,但C++将前自增运算符更新为左值,同时将后自增运算符仅保留为右值。原因是我们不能让它成为左值,因为它只有旧值,而不是更新的值。但我不能正确理解这个原因。

现在a++看到右值是 3,而不是变量本身,对吧?但是如果它带来一个拥有左值的变量,那么 5 将插入其中,并且在语句结束后它的值将是 6。这有什么问题,为什么不能这样做?

c++ rvalue lvalue post-increment

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

为什么在 C++ 中允许重新声明模板成员函数?

class test{
 public:
 int call();
};

int test::call();

int main(){return 0;}
Run Code Online (Sandbox Code Playgroud)

上面的不会编译,出现这个错误:

error: declaration of 'int test::call()' outside of class is not definition 
Run Code Online (Sandbox Code Playgroud)

但是,如果这样使用模板,则允许使用相同的代码:

class test{
 public:
 template<class T>
 int call();
};

template int test::call<int>(); // what's the point of this line?

template <class T>
int test::call(){
  T in = 5;
  return in;
}

int main(){
 test t;
 
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

1-重新声明有什么意义?我在其他项目中看到过类似的代码。

2- 为什么它可以编译而我的第一个代码片段却不能?

c++

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

为什么 std::function 没有进行类型检查?

#include <functional>

void toggleOk(bool& b) { b = !b; }
void toggleBroken(bool b) { b = !b; }
void toggleInt(int i) { i = !i; }
void tooManyParams(bool b, int i) { i = !b; }

int main()
{
    typedef std::function<void(bool&)> CallbackType;
    typedef std::function<void(bool)> WrongCallbackType;

    CallbackType cb1 = [](bool b) { b = !b; }; // Should throw error - missing reference
    CallbackType cb2 = toggleOk; // Ok

    CallbackType cb3 = toggleBroken; // Should throw error - missing reference
    CallbackType …
Run Code Online (Sandbox Code Playgroud)

c++ function c++11 std-function c++17

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

为什么在外部作用域中的变量内联等待与简单的等待赋值不同?

在我问这个问题之前,我确实研究了这个问题

我不明白为什么在下面的示例中,输出在run1和 中不同run2

"use strict";

function sleep(ms) {
  return new Promise(resolve =>
    setTimeout(() => {
      resolve(ms);
    }, ms)
  );
}

const seconds = [1000, 3000, 2000];

let output1 = 0;
let output2 = 0;

console.log("start");

(async function run1() {
  await Promise.all(
    seconds.map(async sec => {
      output1 = output1 + (await sleep(sec));
    })
  );
  console.log({ output1 });
})();

(async function run2() {
  await Promise.all(
    seconds.map(async sec => {
      const res = await sleep(sec);
      output2 = …
Run Code Online (Sandbox Code Playgroud)

javascript promise async-await

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

为什么 R 无法通过词法作用域找到参数的值?

我一直在阅读 Hadley Wickham 的《Advanced R》,以便更好地了解 R 的机制及其幕后工作原理。到目前为止,我很享受它,一切都很清楚。有一个问题一直困扰着我,但我还没有找到解释。我非常熟悉 R 的作用域规则,它决定如何将值分配给自由变量。然而,我一直在努力解决为什么 R 在第一种情况下无法通过词法作用域找到形式参数的值的问题。考虑以下示例:

y <- 4
f1 <- function(x = 2, y) {
  x*2 + y
}

f1(x = 3)
Run Code Online (Sandbox Code Playgroud)

它通常会抛出错误,因为我没有为 argument 分配默认值y。然而,如果我y在函数体中创建一个局部变量,它不会抛出任何错误:我还在 Matloff 教授的书中读到,参数的行为就像局部变量,所以这就是为什么这个问题对我来说仍然是个谜。

f1 <- function(x = 2, y) {
  y <- 4
  x*2 + y
}

f1(x = 3)
Run Code Online (Sandbox Code Playgroud)

这里也没有错误,原因也很清楚:

y <- 2
f2 <- function(x = 2) {
  x*2 + y
}

f2()
Run Code Online (Sandbox Code Playgroud)

预先非常感谢您。

arguments r function lexical-scope

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

为什么我的缓冲区在堆栈上分配的内存比我要求的多?

这是我的源代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 500

int main(int argc, char** argv)
{
        if (argc != 2)
                exit(1);
        char str[MAX];
        strcpy(str, argv[1]);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

disas使用并得到了以下结果maingdb

Dump of assembler code for function main:
   0x0000000000001145 <+0>:     push   %rbp
   0x0000000000001146 <+1>:     mov    %rsp,%rbp
   0x0000000000001149 <+4>:     sub    $0x210,%rsp
   .
   .
   .
End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

这里值得注意的是:

0x0000000000001149 <+4>: sub $0x210,%rsp

我的问题是-
为什么会出现$0x210(528 字节),而它应该是$0x1f4(500 字节),正如我所要求的?

c assembly x86-64 stack-memory

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

为什么summary() 显示的标准误差与coeftest() 不同?

我使用强大的标准错误运行 glm() 。为了进行后续模型比较,我计算了两个回归模型(系数和se)的差异。对于该计算,我使用summary() 函数。然而,模型的汇总函数显示的标准误差与我从 coeftest() 获得的标准误差不同。系数值保持相同。

输入:

mod.01 <- glm(dep ~ indep1 + indep2 + indep3,
          family = binomial (link = "logit"), data = data)
coeftest(mod.01, vcov. = vcovHC, type= "HC3", df = NULL)

summary(mod.01, robust=T)
Run Code Online (Sandbox Code Playgroud)

输出:

coeftest()      
                           Estimate  Std. Error  t value  Pr(>|t|)    
    (Intercept)         -2.72917626  0.16367787 -16.6741 < 2.2e-16 ***
    indep1               0.00427870  0.41928906   0.0102  0.991859    
    indep2               2.00243724  0.19757861  10.1349 < 2.2e-16 ***
    indep3               0.36385098  0.32783817   1.1098  0.267159    


summary()
                           Estimate Std. Error z value Pr(>|z|)    
    (Intercept)          -2.7291763  0.1758744 -15.518  < …
Run Code Online (Sandbox Code Playgroud)

r summary glm

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