cout << 1 as double gives 0

So I made myself a simple little queue class of doubles that goes below. Surprisingly printinq it in relative order gives different output as absolute printing (.print() method)

class DoubleQueue{
private:
double* queue;
int size;
int position;
public:

DoubleQueue(int size){
this->size = size;
queue = new double[size];
}

~DoubleQueue(){ delete[] queue; }

void insert(double value){
position = (position + 1) % size;
queue[position] = value;
}

double getLast(){
return queue[(position + 1) % size];
}

bool stagnates(double difference = 0.0){
double diff = abs(queue[(position + 1) % size] - queue[position]);
return (diff <= difference);
}

void print(bool absolute = false){
int offset = (absolute) ? 0 : position;
for(int i = 0; i < size; i++){
if (absolute && i == position){
cout << "[" << queue[offset+i] << "]<-" << endl;
}
else{
cout << "[" << queue[offset+i] << "]" << endl;
}
}
}
};

int main(int argc, char** argv) {


DoubleQueue q(10);

for( int i = 0; i < 10; i++){
q.insert(i*10);
}

for( int i = 0; i < 3; i++){
q.insert(i*i);
}

q.print(true);
cout << "-----" << endl;
q.print();
return 0;
}
====== Output ======
[0]
[1]
[4]<-
[30]
[40]
[50]
[60]
[70]
[80]
[90]
-----
[4]
[30]
[40]
[50]
[60]
[70]
[80]
[90]
[1.37962e-306]
[1.66967e-307]

As you can see, last value should be 1 in my opinion. Can anyone explain this please? 1.66967e-307 can't be == 1 even when considering precision issues right?
You're printing values from position on. The last two values are being read from memory outside the array.
Aaah of course :) Thank you filipe, I must have been tired when I overlooked it. It's so obvious!
Topic archived. No new replies allowed.