小编rah*_*hul的帖子

使用 float / double 作为循环变量

在 x64 架构上运行,Visual Studio 2013 Windows 8.1

我正在尝试运行一个使用浮点数作为循环变量的循环,问题是在增量操作期间十进制值丢失。

例如:

float incrementValue = 360 / 14;
for (float i = 0; i <= 360; i = i + incrementValue)
{
    cout << endl << " I " << i;
}
Run Code Online (Sandbox Code Playgroud)

我需要 i >= 360 的值。但它停在 350 处。由于 360/14 =25.7142857 但看起来增量的步长为 25 。如果我对任何整数做同样的事情,它都可以正常工作,这个问题仅适用于 xx.yyyy 形式的任何数字

我尝试查找有关此问题的各个网站,但找不到任何可以帮助我/回答我的问题的内容。

c++ computer-science

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

将双指针转换为int指针

当我打印b并且d他们都持有相同的地址(地址a).那么为什么*b打印0*d打印5?

 void main() 
 {
    double a = 5.0;
    double *d = &a;
    int *b = (int*)d;
    int a1 = 10;
    cout << "Val of D : " << d << " Address of d :" << &d
         << " Value of *d :" << *d << endl;
    cout << "Val of B : " << b << " Address of B :" << &b
         << " Value of …
Run Code Online (Sandbox Code Playgroud)

c++ pointers type-punning

0
推荐指数
2
解决办法
4795
查看次数

标签 统计

c++ ×2

computer-science ×1

pointers ×1

type-punning ×1