Go Down

### Topic: Problem with dividing two integers (Read 1 time)previous topic - next topic

#### Webca

##### Feb 08, 2013, 08:04 am
Her is my code:

Code: [Select]
`#include <Wire.h> #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); #define ledPin  13                  // LED connected to digital pin 13 #define buttonPin 9                 // button on pin 4 #define alfaButton 8 #define charlieButton 6 #define deltaButton 10 int value = LOW;                    // previous value of the LED int buttonState;                    // variable to store button state int lastButtonState;                // variable to store last button state long interval = 100;                // blink interval - change to suit long previousMillis = 0;            // variable to store last time LED was updated long startTime ;                    // start time for stop watch long elapsedTime ;                  // elapsed time for stop watch int fractional;                     // variable used to store fractional part of time int fractional2; unsigned long LastDebounceTime = 0;unsigned long DebounceDelay = 50;unsigned long LastDebounceTime2 = 0;unsigned long DebounceDelay2 = 50;unsigned long LastDebounceTime3 = 0;unsigned long DebounceDelay3 = 50; boolean test = false; boolean start = false; boolean lockout = false;            //**NEW BOOLEAN** byte lastbuttonState2 = 0;          // new global variable byte buttonState1 = 0; byte buttonState2 = 0; int Points = 0; int Faktor = 0; int SwitchState =0;int SwitchDebounce;int LastSwitchState=HIGH;int LastSwitchDebounce=LOW;int LastSwitchState2=HIGH;int LastSwitchDebounce2=LOW;int LastSwitchState3=HIGH;int LastSwitchDebounce3=LOW;int Counter=0;int Counter2=0;int Counter3=0; void setup() {   Serial.begin(9600);   lcd.begin(16, 4);   // Print a message to the LCD.  lcd.print("Test");    lcd.setCursor(0,1);          lcd.print("A:");    lcd.setCursor(-4,2);       lcd.print("C:");        lcd.setCursor(-4,3);    lcd.print("D:");       pinMode(ledPin, OUTPUT);         // sets the digital pin as output   pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground. } void loop() {  // no more debounce   buttonState2 = digitalRead(buttonPin);   if(buttonState2 == HIGH){     if(lockout == false){ // #1       start = true;       startTime = millis();       lastButtonState = buttonState2;       lockout = true; // (The lockout) this will make sure it does not go back to this IF statement     }   }   if(buttonState2 == HIGH && start == true){ // #2     elapsedTime =   millis() - startTime;              // store elapsed time     lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time     lcd.setCursor(9,1);     lcd.print(" ");     // routine to report elapsed time     lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print     lcd.print(".");     fractional = (int)(elapsedTime % 1000UL);     lcd.print(fractional);  // print fractional part of time   }   //else lcd.clear(); // #3    //_____________________________________________________                 int CurrentSwitch = digitalRead(alfaButton);  if (CurrentSwitch != LastSwitchDebounce)  {    LastDebounceTime = millis();  }   if ((millis() - LastDebounceTime) > DebounceDelay)   {    if (CurrentSwitch != LastSwitchState)     {      if (CurrentSwitch == HIGH)      {        Counter=Counter+5;        lcd.setCursor(2,1);         lcd.print(Counter);      }     }    LastSwitchState=CurrentSwitch;  }  LastSwitchDebounce = CurrentSwitch; //----------------------------------------int CurrentSwitch2 = digitalRead(charlieButton);  if (CurrentSwitch2 != LastSwitchDebounce2)  {    LastDebounceTime2 = millis();  }   if ((millis() - LastDebounceTime2) > DebounceDelay)   {    if (CurrentSwitch2 != LastSwitchState2)     {      if (CurrentSwitch2 == HIGH)      {        Counter2=Counter2+3;        lcd.setCursor(-2,2);         lcd.print(Counter2);      }     }    LastSwitchState2=CurrentSwitch2;  }  LastSwitchDebounce2 = CurrentSwitch2;  //----------------------------------------int CurrentSwitch3 = digitalRead(deltaButton);  if (CurrentSwitch3 != LastSwitchDebounce3)  {    LastDebounceTime3 = millis();  }   if ((millis() - LastDebounceTime3) > DebounceDelay)   {    if (CurrentSwitch3 != LastSwitchState3)     {      if (CurrentSwitch3 == HIGH)      {        Counter3=Counter3+1;        lcd.setCursor(-2,3);         lcd.print(Counter3);      }     }    LastSwitchState3=CurrentSwitch3;  }  LastSwitchDebounce3 = CurrentSwitch3;   //----------------------------------------          Points=Counter+Counter2+Counter3;       // Points=Points * 1000;                lcd.setCursor(6,2);         lcd.print((int)(Points));               lcd.setCursor(5,1);       lcd.print("TID:");               lcd.setCursor(1,2);       lcd.print("PNG:");               lcd.setCursor(1,3);       lcd.print("FAK:");              // Faktor = Points / elapsedTime;       lcd.setCursor(6,3);       lcd.print(elapsedTime / Points);                                              }`

This works: lcd.print(elapsedTime / Points);
But this doesnt: lcd.print(Points / elapsedTime);

What is causing this strange behavior?

#### Nick Gammon

#1
##### Feb 08, 2013, 08:29 am
Quote
This works:

Define "works". What happened? What did you expect?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### michinyon

#2
##### Feb 08, 2013, 08:33 amLast Edit: Feb 08, 2013, 08:36 am by michinyon Reason: 1
If I divide  7 by 3,  I get 2
If I divide 3 by 7,  I get zero.

It is not too hard to figure out how "integer division" on computers works.

Your Points seems to be an int   and your elapsed time  is a long.

The behaviour when you mix types in an expression is probably defined somewhere,  but doesn't
necessarily have to be consistent,   and is best avoided  unless you do it all the time and remember
exactly how it works on your platform.

#### Webca

#3
##### Feb 08, 2013, 08:39 am
michinyon:
Well, all of this is pretty new to me, and i have no previously knowledge of programming...

Do you know how i can get my equation to work?

Nick Gammon:
I can see on a lcd what the values are so it is easy to check if i get the same as on a calculator

#### AWOL

#4
##### Feb 08, 2013, 08:44 am
Quote
Do you know how i can get my equation to work?

As Nick said, you have to define "work".
If you want fractional results, it is easiest to use fractional representations, like the "float" datatype.
Make sure your arguments to float calculations are cast to "float".
e.g.
Code: [Select]
`int one = 1;int two = 2;float half = (float) one / (float) two;`
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### UKHeliBob

#5
##### Feb 08, 2013, 08:53 am
Try this example to see what is going on
Code: [Select]
`int iA = 123;int iB = 456;float fA = 123;float fB = 456;void setup() {  Serial.begin(9600);  Serial.print("ints\t");  Serial.println(iA/iB);  Serial.print("floats\t");  Serial.println(fA/fB);}void loop() {}`
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### Webca

#6
##### Feb 08, 2013, 09:14 am
Thank you UKHeliBob for making that "easy to learn" example. I got it working:)

#### Nick Gammon

#7
##### Feb 08, 2013, 09:22 am

Nick Gammon:
I can see on a lcd what the values are so it is easy to check if i get the same as on a calculator

Yes, but that doesn't answer my question. What values did you see and what values did you get on the calculator?

That's like saying "I got a sock out of my sock drawer and it wasn't the colour I expected."

Right. What colour did you get, and what colour did you expect to get?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Webca

#8
##### Feb 08, 2013, 09:27 am

I expected to get 4.4 when Points =88 and elapsedTime =20.00

#### Nick Gammon

#9
##### Feb 08, 2013, 09:39 am
I see. I can't reproduce that:

Code: [Select]
`void setup ()  {  Serial.begin (115200);  int Points = 88;  long elapsedTime = 20;    Serial.println (Points / elapsedTime);  }  // end of setupvoid loop () { }`

Output:

Code: [Select]
`4`
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Webca

#10
##### Feb 08, 2013, 09:49 am
I got it to work with this:

Code: [Select]
`      float Faktor = 0; float elapsedTime2 ;elapsedTime2 = elapsedTime;      Faktor = (float)Points / (float)elapsedTime2;       lcd.setCursor(6,3);       lcd.print( (float)Faktor * 1000); `

Go Up

Please enter a valid email to subscribe