0

So, I decided to test out a new C++ turtle-style graphics library. I decided to test it out by making the fibonacci spiral. however, upon running the program, it stops in the middle. I checked what was going on, and the fibonacci value had reversed to -2147483647, as if it was compiled in 32-bit. Can someone please help? I compiled it with g++ on Windows. Note: I was using a vector to store the values. Here's my code:

//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
        }
    }
}
KSP Atlas
  • 138
  • 1
  • 8
  • 1
    just because you compile as 64 bit does not mean that all datatypes automagically are 64bit wide – 463035818_is_not_an_ai Mar 10 '21 at 11:47
  • maybe this answers your question: https://stackoverflow.com/q/36051341/4117728 – 463035818_is_not_an_ai Mar 10 '21 at 11:50
  • [related question here](https://stackoverflow.com/questions/39779880/c-int-vs-long-long-in-64-bit-machine). – WhozCraig Mar 10 '21 at 11:54
  • So, It didn't work. here is my code: – KSP Atlas Mar 10 '21 at 11:56
  • oh wait it's too long – KSP Atlas Mar 10 '21 at 11:57
  • 1
    `long int` is only guaranteed to be able to represent values in the range `-2147483547` to `2147483547` (consistent with a 32-bit type). Building for 64-bit affects the (upper limit of the) amount of addressable memory available to the program. It is not related to the size of integral types. In practice, is not unusual in practice for a 64-bit compiler to still support a 32-bit `long int` type. – Peter Mar 10 '21 at 12:05
  • Unrelated to your problem, but why do you store the whole fibonacci sequence? You only need the two last values to calculate the next. – HAL9000 Mar 10 '21 at 13:07

1 Answers1

4

MinGW uses the MSVC-friendly LLP64 data model.

In LLP64, int and long int are both 32 bits, regardless of platform bitness (32/64 bit).

To get a 64-bit data type, use long long or int64_t.

e.g. std::vector<long long>. And also here: for(long long i = 0; ...

rustyx
  • 80,671
  • 25
  • 200
  • 267