KSP*_*las 0 c++ g++ integer-overflow
因此,我决定测试一个新的 C++ 海龟式图形库。我决定通过制作斐波那契螺旋来测试它。但是,运行该程序后,它会在中间停止。我检查了发生了什么,斐波那契值已经反转为-2147483647,就好像它是用32位编译的一样。有人可以帮忙吗?我在 Windows 上用 g++ 编译了它。注意:我使用向量来存储值。这是我的代码:
//Includes and namespaces
#include "Cturtle.hpp"
#include<iostream>
namespace ct = cturtle;
std::vector<long int> fibonacci = {1, 2}; //This vector stores the fib sequence
ct::TurtleScreen scr; //Create a new Screen
ct::Turtle turtle(scr); //Create a new turtle on the screen
int main() {
turtle.speed(ct::TS_FASTEST); //Set the turtle's speed to max
while(true) {
for(int i = 0; i < fibonacci.back(); i++) {
fibonacci.push_back(fibonacci.back() + fibonacci.back()-1); //Add a new value to the fibonacci vector based off the previous two
std::cout << fibonacci.back() << '\n'; //This is here for debug
turtle.forward(1); //Move the turtle 1 step forward
turtle.right(90 / fibonacci.back()); //Turn according to 90 divided by the current fibonacci value
}
}
}
Run Code Online (Sandbox Code Playgroud)