# float division problem (x / y = inf)

Hi everyone, i’m working on a calculator project but i can’t understand how to make division work.
It’s probably caused by the usage of 2 float and factors and another float since when i write make a division between two set variable i get a correct result.
I will post the entire code in case it’s needed, but the function that gives me problems is “calcolo”.
I’m sorry if the comments are not in english, but i can explain the code if needed.

#include <LiquidCrystal.h>

const byte ROWS = 4; //quattro righe
const byte COLS = 5; //cinque colonne
char keys[ROWS][COLS] = {
{'1','2','3','+','-'},
{'4','5','6','*','/'},
{'7','8','9','R','E'},
{'.','0','#','P','L'}
};
byte rowPins[ROWS] = {38, 36, 34, 32}; //dichiara i pin delle quattro righe della tastiera
byte colPins[COLS] = {30, 28, 26, 24, 22}; //dichiara i pin delle cinque colonne della tastiera

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //dichiara i pin del display

float A = 0;
char OP;
float B = 0;
float TOT = 0;

void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
}

void loop(){
lcd.clear();
delay(10);

A = letturafattore();
lcd.setCursor(8,0);
OP = letturaop();
lcd.setCursor(10,0);
if (OP != 'L'){
B = letturafattore();
}
lcd.setCursor(0,1);
TOT = calcolo (OP);
Serial.print(TOT);
attesa();

}

void attesa(){

int conferma = 0;
while (conferma == 0){
if (key == '#') conferma = 1;
}
}

float calcolo ( char op){    // FUNZIONE DEL CALCOLO

float a = 1.00;
float b = 2.00;
float ris = 0;
float esp = 0;
Serial.println(a);
Serial.println(b);

switch (op){
case '+':
ris = a + b;
break;
case '-':
ris = a - b;
break;
case '*':
ris = a * b;
break;
case '/':
if (b = 0){
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("DIVISIONE PER 0!");
errloop();
}
else {
ris = a / b;
}
break;
case 'R':
if (a < 0){
lcd.clear();
lcd.setCursor (0,0);
errloop();
}
else if (b = 0){
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("B UGUALE A 0!");
errloop();
}
else {
ris = pow (a, 1/b);
}
break;
case 'E':
ris = pow(a, b);
break;
case 'P':
ris = (a / 100) * b;
break;
case 'L':
if (a <= 0){
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("A NON POSITIVO!");
errloop();
}
else{
ris = log10(a);
}
break;
}

/*if(ris != check(ris)){
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("TROPPO GRANDE!");
errloop();
}*/
Serial.print("Risultato = ");
Serial.print(ris);
if(ris > 9999999999999){
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("TROPPO GRANDE!");
errloop();
}

return ris;
}

float check(float A){    // FUNZIONE DI CONTROLLO

Serial.println (A);
A = A*pow(10,-23);
Serial.println (A);
A = A*pow(10,23);
Serial.println (A);
return A;
}

float letturafattore (){    // FUNZIONE LETTURA FATTORI

int conferma = 0;
int dot = 0;
int arr [6] = {0,11,11,11,11,11}; //vettore del primo numero
bool pos = HIGH; //segno del primo numero
float fattore = 0;

for(int i = 0; i < 6 && conferma != 1; i++){                //INSERIMENTO DEL PRIMO FATTORE

if(key == '#') conferma = 1;
else if(key == '+' && i == 0) {   //assegna a pos il valore + controllando che sia in prima posizione
pos = HIGH;
arr[i] = 0;
lcd.print("+");
}
else if(key == '-' && i == 0){    //assegna a pos il valore - controllando che sia in prima posizione
pos = LOW;
arr[i] = 0;
lcd.print("-");
}
else if(key == '.' && dot == 0) {    //scrive il punto e segna la posizione con un 10. Impedisce che venga scritto due volte
arr[i] = 10;
dot = 1;
lcd.print(".");
}
else if(key == NO_KEY || key == '*' || key == '/' || key == 'R' || key == 'E' || key == 'P' || key == 'L' || key == '+' || key == '-' || key == '.') i=i-1;
else{
arr[i] = key - 48;
lcd.print(arr[i]);
}
}

Serial.print("FATTORE ACQUISITO = ");
Serial.print(arr[0]);
Serial.print(arr[1]);
Serial.print(arr[2]);
Serial.print(arr[3]);
Serial.print(arr[4]);
Serial.println(arr[5]);
fattore = arrtoint (arr, pos);
Serial.print("DOPO CONVERSIONE = ");
Serial.println(fattore);

return fattore;
}

char letturaop(){    // FUNZIONE LETTURA OPERAZIONE

int conferma = 0;
char op;

while (conferma !=1){
if (key == '#'){
conferma = 1;
}
else if (key == NO_KEY || key == '0' || key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' );
else {
lcd.setCursor(8,0);
lcd.print(key);
op = key;
}
}

return op;
}

float arrtoint (int arr[6], bool pos){  //CONVERTE LA MATRICE IN UN NUMERO INTERO

int x = 0;
int x1 = 0;
float result = 0;
float dec = 0;
int n = 0;
int z = 1;

for (int i = 0; i < 6 && arr[i] != 11; i++){

if ( arr[i] < 0 || arr[i] > 10 ){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ERR NON DEFINITO");
errloop();
}
else if (arr[i] == 10){ //Controlla se nella posizione c'è il punto
x = 1;
x1 = 1;
}
else if (x == 1 && x1 == 1){ //Converte la prima cifra dopo la virgola
delay (10);
result = result /10;
x1 = 0;
dec = arr[i];
dec = dec / pow(10,z);
result = result + dec;
z++;
}
else if (x == 1){ //Converte le successive cifre dopo la virgola
delay (10);
dec = arr[i];
dec = dec / pow(10,z);
result = result + dec;
z++;
}
else { //Converte le cifre prima della virgola
delay (10);
result = result + arr[i];
result = result * 10;
}
}

if (x == 0) result = result/10; //Corregge la conversione nel caso non ci sia la virgola

if (pos == LOW) result = result * -1; //Assegna il segno negativo

return result; //Restituisce il risultato della conversione
}

void errloop(){    //VA IN LOOP

int a = 0;
lcd.setCursor(0,1);
lcd.print("RIAVVIARE");
while(a == 0) delay (1000);
}

The main problem is that every number i assign from the other function to A and B the division between the two will be “inf”

That’s an example of the serial monitor when i run the program

FATTORE ACQUISITO = 11111111111
DOPO CONVERSIONE = 1.00
FATTORE ACQUISITO = 51111111111
DOPO CONVERSIONE = 5.00
1.00
2.00
Risultato = inf

I will be very glad to anyone who helps me, since that’s for an exam and it’s the only error i get

= means set a variable to a value, == means test for equality. if (b = 0) is wrong.

vaj4088: = means set a variable to a value, == means test for equality. if (b = 0) is wrong.

unless you want to assign zero to 'b' and make absolutely sure that the outcome is false.

I can’t believe it was an error so stupid :facepalm:
I also checked the entire code for these little f*ckers before, maybe i added these after that…

Oh, and of course now it works.
Thank you so much

indeed - what happens in this code

if (b = 0){
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("DIVISIONE PER 0!");
errloop();
}
else {
ris = a / b;
}

is that you set b to 0

(b = 0) being an expression that returns the value being set, returns 0

the if is evaluating this as if (0) so execute the code in the else statement but because b was set to zero, you get a nice division by zero

:)

nice bug. always hard to see those. great catch by vaj4088!

always hard to see those.

Unless you get the compiler to do the hard workif (0 = b) will give you a build-stopping error.

Put the constant first.

smart!

AWOL: Unless you get the compiler to do the hard workif (0 = b) will give you a build-stopping error.

Put the constant first.

Didn't know that Thanks for the tip :o

It's just a small hint; it doesn't work for variables, so if you wrote if (a=b), you're in the lap of the compiler.