[Solved]Variable displaying incorrectly

Understanding the language, error messages, etc.

[Solved]Variable displaying incorrectly

Postby Sushi » Tue Oct 14, 2014 11:25 pm

I have two variables, shots and score. To determine the variable accuracy, or acc, I do this:
Code: Select all
float acc = 1;
void loop(){
    acc = score / shots;
}

Is anything wrong with this? It displays as -0.00 or just 0.00. Here is my full code:
Code: Select all
#include <Gamebuino.h>
#include <SPI.h>

Gamebuino gb;

byte PROGMEM doge[] =
{
  16,16,
  B00000011,B11000000,
  B00000111,B11100000,
  B00001111,B11110000,
  B00011111,B11111000,
  B00111111,B11111100,
  B01110111,B11101110,
  B01110111,B11101110,
  B11110001,B10001111,
  B11110001,B10001111,
  B11111001,B10011111,
  B01111000,B00011110,
  B00000000,B00000000,
  B00000000,B00001100,
  B00011000,B00111110,
  B00011100,B01111110,
  B00001110,B01111100,

};
byte PROGMEM logo[] =
{
  24,24,
  B11111111,B11111111,B11111111,
  B10000000,B00000000,B00000001,
  B10000000,B00000000,B00110001,
  B10000000,B00001000,B01110001,
  B10000000,B00000100,B00100001,
  B10000000,B00000000,B10000001,
  B10000000,B00001100,B00000001,
  B10000000,B00001010,B00000001,
  B10000000,B00010001,B00100001,
  B10000000,B00100010,B00010001,
  B10000000,B01000100,B00000001,
  B10000000,B10001000,B00000001,
  B10000001,B00010000,B00000001,
  B10000010,B00101000,B00000001,
  B10001100,B01110000,B00000001,
  B10000110,B11100000,B00000001,
  B10000001,B11110000,B00000001,
  B10000001,B11110000,B00000001,
  B10000000,B11110000,B00000001,
  B10000000,B11100000,B00000001,
  B10000000,B00000000,B00000001,
  B10000000,B00000000,B00000001,
  B10000000,B00000000,B00000001,
  B11111111,B11111111,B11111111,

};
byte PROGMEM bushes[] =
{
  88,20,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00111111,B00000000,B00000000,B01111100,B00000000,B00000000,B11111110,B00000000,B00000000,B00000000,B11110000,
  B01111111,B11111000,B00000011,B11111111,B00000000,B11111111,B11111111,B00011111,B11000000,B11111111,B11111111,
  B11111111,B11111100,B00001111,B11111111,B11000111,B11111111,B11111111,B11111110,B10111111,B11111111,B11111111,
  B11111111,B11111110,B00111111,B11111111,B11111111,B11111111,B11111111,B11111111,B01111111,B11111111,B11111111,
  B11111111,B11111111,B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11110111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11100011,B11111111,B11111111,B11111111,B11111111,B11110111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11110111,B11111111,B11111111,B11111111,B11111111,B11100011,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11110111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11110111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11100011,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11110111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
  B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,
};
byte PROGMEM player[] =
{
  8,3,
  B01000000,
  B11100000,
  B01000000,

};
float thing_x = LCDWIDTH/2;
float thing_y = LCDHEIGHT/2;
float thing_vx = 0;
float thing_vy = 0;
float thing_size = 7;
float acc = 1;
int score = 0;
int highscore = 0;
int scorefin = 0;
int time = 0;
int shots = 0;

void setup(){
  gb.begin();
  gb.titleScreen(F("Goomba Hunt"),logo);
  gb.popup(F("Shotgun those goombas!"), 20);

}

int count = 0;
int dogepos = -10;
int dogey = -10;

void loop(){
  if(gb.update()){
    acc = score / shots;
    gb.display.println(acc);
    gb.display.drawBitmap(thing_x, thing_y, player);
    gb.display.println(score);
    gb.display.println();
    gb.display.drawBitmap(0,28,bushes);
    if (dogepos >=0) {
      gb.display.drawBitmap(dogepos,20,doge);
    }
    if(thing_x < 0){
      thing_vx = -thing_vx;
    }
    if(thing_x + thing_size > LCDWIDTH){
      thing_vx = -thing_vx;
    }
    if(thing_y < 0){
      thing_vy = -thing_vy;
    }
    if(thing_y + thing_size > LCDHEIGHT){
      thing_vy = -thing_vy;
    }
    if(gb.buttons.repeat(BTN_UP,2)){
      thing_vy = thing_vy - 1;
    }
    if(gb.buttons.repeat(BTN_DOWN,2)){
      thing_vy = thing_vy + 1;
    }
    if(gb.buttons.repeat(BTN_RIGHT,2)){
      thing_vx = thing_vx + 1;
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      thing_vx = thing_vx - 1;
    }
    if(gb.buttons.pressed(BTN_A)){
      thing_y = thing_y - 10;
      thing_vy = 2;
      shots = shots + 1;
      gb.sound.playTick();
    }
    if(gb.buttons.pressed(BTN_B)){
      thing_vx = 0;
      thing_vy = 0;
      gb.popup(F("Velocities Reset"),50);
    }
    thing_x = thing_x + thing_vx;
    thing_y = thing_y + thing_vy;
  }
  if (count++ % 5 == 0) {
    thing_vx = thing_vx * 0.999;
    thing_vy = thing_vy * 0.999;
  }
  if ((count % 20000 == 0)){
    if ((random(0,1) < 0.05)) {
      dogepos = random(0,76);
      dogey = random(10,20);
    }
    else if (random(0,1) < 0.2) {
      dogepos = -1;
      dogey = -1;
    }
  }
  if((gb.collideBitmapBitmap(thing_x,thing_y,player,dogepos,dogey,doge)&&(gb.buttons.pressed(BTN_A)))){
    score = score + 1;
    dogepos = -10;
    dogey = -10;
    gb.sound.playOK();
  }
}





Last edited by Sushi on Wed Oct 15, 2014 10:11 am, edited 1 time in total.
User avatar
Sushi
 
Posts: 52
Joined: Tue Oct 07, 2014 12:12 am

Re: Variable displaying incorrectly

Postby Myndale » Wed Oct 15, 2014 6:08 am

C math operators evaluate the right-hand side of the equation using whichever operand type contains the highest accuracy. In your case 'score' and 'shots' are both integers so the operation is done using an integer divide as well. The fact that the result is destined to be stored in a float is irrelevant because evaluation of the right-hand side of the equation is always done before assignment to the left; the compiler simply converts the resulting integer value to a float and stores it in acc.

To get around this you need to force one of your operands to be a float by using a cast:

Code: Select all
void loop(){
    acc = (float)score / shots;
}
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Variable displaying incorrectly

Postby jonnection » Wed Oct 15, 2014 7:10 am

And its unnecessary to calculate acc in every game loop. Divide is a slow operation on the atmega328 processor, and a float divide is at least 8 times slower.

You should calculate acc when you add 1 to score.
User avatar
jonnection
 
Posts: 317
Joined: Sun May 04, 2014 8:21 pm

Re: Variable displaying incorrectly

Postby rodot » Wed Oct 15, 2014 7:29 am

Hey there, it seems that your question already have been answered, but here are my 2 cents:

You could use unsigned int instead of float by storing the accuracy value in percents:
Code: Select all
acc = (100 * score) / shots;


Make sure you multiply by 100 before you divide by the number or shot, or you'll get 0 as Myndale explained. Parenthesis are not useful here but for the sake of clarity. Also you have to make sure that (100 * score) won't overflow before it gets divided (unsigned int overflows at 64k, so the score has to be under 640).
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: [Solved]Variable displaying incorrectly

Postby Sushi » Wed Oct 15, 2014 10:14 am

Thanks for all the answers! Sorry that this was a repost though...
User avatar
Sushi
 
Posts: 52
Joined: Tue Oct 07, 2014 12:12 am

Re: [Solved]Variable displaying incorrectly

Postby Myndale » Wed Oct 15, 2014 10:50 am

Sushi wrote:Sorry that this was a repost though...


Rodot didn't mean it was a repost, he was just saying that although I had already answered it he had more useful information to contribute to the discussion. And for performance purposes his method is indeed better because the right-hand calculation is all done as integers, you just gotta remember to make the destination variable on the left-hand side an integer as well if you want maximum performance otherwise the result will still be needlessly converted to a float.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 7 guests