小编Yiy*_*Lee的帖子

为什么std :: abs(9484282305798401ull)= 9484282305798400?

我正在编写一个模板化辅助方法,它可以将C编号(包括unsigned long long)转换为GMP库中的mpz_class数字.在两者之间,有一个电话std::abs.

然而,事实证明,对于C++ 17(g ++ 6.3.1),

#include <iostream>
#include <cmath>

int main()
{
    std::cout << (unsigned long long)std::abs(9484282305798401ull);
}
Run Code Online (Sandbox Code Playgroud)

给出不正确的输出9484282305798400.

正如我从cmath所理解的那样,std::abs首先将参数转换为双精度.

根据C++文档,double有52个尾数位,这意味着我必须严格小于2^52 = 4503599627370496任何精度损失之前的最大整数值.

我是否正确地说,由于9484282305798401超过此限制,std::abs最终会丢弃精确度以给出错误的答案?

为了澄清,我完全清楚,要求无符号整数的绝对值是完全没有意义的.但是,我希望模板化函数适用于一般C编号,而不必分别为每个有符号和无符号类型专门创建特殊化.

c++ floating-point precision c++11 c++17

7
推荐指数
2
解决办法
480
查看次数

C++ Arguments Heap vs Stack

假设我创建了两个向量,一个在堆上,另一个在堆栈上:

Vector<int> vector1;
Vector<int>* vector2 = new Vector<int>;
Run Code Online (Sandbox Code Playgroud)

然后vector1,我转到两个函数,比如,foo1(Vector<int>)foo2(Vector<int>&).我也vector2进入了foo3(Vector<int>*).

由于我是C++的新手,我对这里的行为差异感到困惑.

  1. 我是否正确地说,因为foo1整个vector1被复制,因为foo2只有引用vector1被传递到函数中?

  2. 但是,不是vector1,在堆栈上声明,应该在其他地方(即从内部foo2)无法访问,除了它的创建范围?

  3. 还有,修改vector1里面的内容foo1,foo2会影响原始矢量吗?

  4. 并且vector1在其范围的末尾自动销毁,还是我们必须手动删除它?

c++ heap stack arguments argument-passing

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

附加属性-“在类型..上找不到样式属性..”

我目前正在尝试定义用户控件内的对象可使用的附加属性。

附加属性在该控件的代码背后注册:

public partial class MapEditor : UserControl
{
    public static readonly DependencyProperty LatitudeProperty =
        DependencyProperty.RegisterAttached("Latitude", typeof(double), typeof(MapEditor));

    //...
 }
Run Code Online (Sandbox Code Playgroud)

然后,我尝试绑定到ItemsControl中的这些属性:

<UserControl xmlns:Controls="clr-namespace:TestProject.Controls" 
             ...>
    <Grid>
         ...
         <ItemsControl ItemsSource={Binding Items}>
             <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Controls:MapEditor.Latitude"
                            Value="1" />
                </Style>
            </ItemsControl.ItemContainerStyle>
         </ItemsControl>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

但是,出现以下错误:

Error   6   Cannot find the Style Property 'Latitude' on the type 'TestProject.Controls.PhaseEditor'
Run Code Online (Sandbox Code Playgroud)

有什么解决办法吗?我尝试在单独的类文件中定义附加的属性,但相同的问题仍然存在。

wpf wpf-controls attached-properties

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

将一个子对象的更改传播到另一个子对象

我遇到过几种情况,其中我有一个具有多个对象的父对象,并且一个子对象中的信息更改会影响其他对象.

例如,请考虑以下情况:

interface IUniverse
{
    IStorage ShirtStorage;
    IList<IHuman> Humans;
}

Interface IStorage:
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
}

Interface IHuman
{
    string Name;
    int Age;
    IList<IShirt> Shirts;
}
Run Code Online (Sandbox Code Playgroud)

我想在我的宇宙中从ShirtStorage中移除一件特定的衬衫,但与此同时,由于衬衫已经从生存中移除,所以它也应该从所有人身上移除.

我想到了3种方法:


首先,我们可以介绍Add(IClothing)Remove(IClothing)方法IStorage<T>IHuman.

interface IUniverse
{
    IStorage ShirtStorage;
    IList<IHuman> Humans;
}

Interface IStorage
{
    string Location;
    int Capacity;
    IList<IShirt> Items;
    **void Add(IShirt);**
    **void Remove(IShirt);**
}

Interface IHuman
{
    string Name;
    int Age;
    IList<IShirt> Shirts;
    **void AddShirts(IShirt);**
    **void RemoveShirts(IShirts);**
}
Run Code Online (Sandbox Code Playgroud)

之后,上面的接口的实现将没有任何东西在从ShirtStorage中移除时从所有人中移除特定衬衫.

这种设计的缺点是,每当程序员从宇宙中移除衬衫时,他将不得不手动移除每个人的每一个参考. …

oop design-patterns

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