为什么在C ++中(int)55 == 54? [英] Why does (int)55 == 54 in C++?

查看:121
本文介绍了为什么在C ++中(int)55 == 54?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在学习C ++。我有我的C ++编程语言和有效的C ++出来,我通过项目欧拉。问题1 ... dunzo。问题2 ...不是这么多。我在VS2008的Win32控制台应用程序上工作。



斐波纳契序列的所有偶数项的和在400万以下是什么?



它不工作,所以我剪下一个测试用例100 ...



我写了...

  // Problem2.cpp:定义控制台应用程序的入口点。 
//

#includestdafx.h
using namespace std;

int _tmain(int argc,_TCHAR * argv [])
{
cout< Project Euler Problem 2:\\\
\\\
;
cout<< Fibonacci序列中的每个新术语通过添加前两个术语生成。从1和2开始,前10个术语将是:\\\
\\\
;
cout<< 1,2,3,5,8,13,21,34,55,89,... \ n \ n;
cout<< 找到序列中不超过四百万的所有偶数值术语的总和。
cout<< Answer:<<解决();
}

double Solve(){
int FibIndex = 0;
double result = 0.0;
double currentFib = GenerateNthFibonacciNumber(FibIndex);
while(currentFib< 100.0){
cout<< currentFib< < (int)currentFib< < (int)currentFib%2 < \\\
;
if((int)currentFib%2 == 0){
result + = currentFib;
cout<<(int)currentFib;
}
currentFib = GenerateNthFibonacciNumber(++ FibIndex);
}
return result;
}

double GenerateNthFibonacciNumber(const int n){
//这将使用Binet的公式生成第n个斐波纳契数字
const double PHI =(1.0 + sqrt ))/ 2.0;
return((pow(PHI,n)-pow(-1.0 / PHI,n))/ sqrt(5.0));
}

这里是输出...


项目Euler问题2:



Fibonacci
序列中的每个新术语都是通过将
前两个术语。从1
和2开始,前10个字词为:



1,2,3,5,8,13,21,34,55 ,89,...



找到
不超过四百万的序列中所有偶数值的
条款的总和。 / p>

0 0 0
1 1 1
1 1 1
2 2
0
3 3 1
5 5 1
8 8 0
13
13 1
21 21 1
34 34 0
55 54
0
89 89 1
答案: 99


所以我有三列调试代码...从generate函数返回的数字,(int)generatedNumber ,and(int)generatedNumber%2



所以在第11个字词上,我们有



55,54,0



为什么(int)55 = 54?



感谢

解决方案

强制转换为 int 截断数字,就像调用 floor currentFib)。所以即使 currentFib 54.999999 ...(一个接近55的数字, ),(int)currentFib 将产生54个。


So I'm learning C++. I've got my "C++ Programming Language" and "Effective C++" out and I'm running through Project Euler. Problem 1...dunzo. Problem 2...not so much. I'm working in VS2008 on a Win32 Console App.

Whats the Sum of all even terms of the Fibonacci Sequence under 4 million?

It wasn't working so I cut down to a test case of 100...

Here's what I wrote...

// Problem2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Project Euler Problem 2:\n\n";
    cout << "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\n\n";
    cout << "1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n\n";
    cout << "Find the sum of all the even-valued terms in the sequence which do not exceed four million.\n\n";
    cout << "Answer:  " << Solve();
}

double Solve() {
    int FibIndex = 0;
    double result = 0.0;
    double currentFib = GenerateNthFibonacciNumber(FibIndex);
    while (currentFib < 100.0){
    	cout << currentFib << " " << (int)currentFib << " " << (int)currentFib % 2 << "\n";
    	if ((int)currentFib % 2 == 0){
    		result += currentFib;
    		cout<<(int)currentFib;
    	}
    	currentFib = GenerateNthFibonacciNumber(++FibIndex);
    }
    return result;
}

double GenerateNthFibonacciNumber(const int n){
    //This generates the nth Fibonacci Number using Binet's Formula
    const double PHI = (1.0 + sqrt(5.0)) / 2.0;
    return ((pow(PHI,n)-pow(-1.0/PHI,n)) / sqrt(5.0));
}

And here's the output...

Project Euler Problem 2:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

0 0 0
1 1 1
1 1 1
2 2 0
3 3 1
5 5 1
8 8 0
13 13 1
21 21 1
34 34 0
55 54 0
89 89 1
Answer: 99

So I have three columns of debug code...the number returned from the generate function, (int)generatedNumber, and (int)generatedNumber % 2

So on the 11th term we have

55,54,0

Why does (int)55 = 54?

Thanks

解决方案

Casting to int truncates the number - same as if you'd called floor(currentFib). So even if currentFib is 54.999999... (a number so close to 55 that it will be rounded up when printed), (int)currentFib will produce 54.

这篇关于为什么在C ++中(int)55 == 54?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆