Problem in solving mathematical operation involving float.

Hey guys.

I am currently developing a project using Arduino Uno. One of the functions requires solving a mathematical operation. So, I try to TEST the program contains only the math “part”.

Here is my code:

#include <math.h>

float pointerX;
float FileSizeX;
unsigned long LoopX;
float WriteBiasa;
float DigitX;
unsigned int Vax;

void setup() {
 
  Serial.begin(9600);                        

  FileSizeX = 245;
  pointerX = 85;
  
  LoopX = pointerX/20;                        //i need the answer in whole number
  DigitX = pointerX/20; 
  WriteBiasa = (DigitX - (float)LoopX)*20;

  Serial.print("\nLoopX :");
  Serial.print(LoopX);
  Serial.print("\nWriteBiasa :");  
  Serial.print(WriteBiasa);
  Vax = fabs (WriteBiasa); 
  Serial.print("\nVax:");  
  Serial.print(Vax);  
  Vax = int (WriteBiasa); 
  Serial.print("\nVax:");  
  Serial.print(Vax);

  LoopX = (FileSizeX-(pointerX+11))/20;
  DigitX = (FileSizeX-(pointerX+11.0))/20.0; 
  WriteBiasa = (DigitX - (float)LoopX)*20.0;

  Serial.print("\nLoopX :");
  Serial.print(LoopX);
  Serial.print("\nWriteBiasa :");  
  Serial.print(WriteBiasa);
  Vax = fabs (WriteBiasa); 
  Serial.print("\nVax:");  
  Serial.print(Vax);   
  Vax = int (WriteBiasa); 
  Serial.print("\nVax:");  
  Serial.print(Vax);   
}

void loop() {
  // put your main code here, to run repeatedly:

}

The result of the code is as attached.

I’m quite confused here, is there anything wrong with how I wrote my program.
It keeps on showing the wrong result, when I try to convert float to int using both methods (fabs() and int()).

And I noticed it only happen when the result is 9 as the ones. (e.g. 9,19).

I really appreciate any help as I am new to Arduino software. TQ very much.

What are you trying to achieve?

input?
output?

The idea is to prove my “mathematical operation” works.

The mathematical problem is as follows:

/Fixed Inputs**/

  1. FileSizeX = 245;
    pointerX = 85;

/Mathematical Operation**/

1st Set–> LoopX = pointerX/20;
DigitX = pointerX/20;
WriteBiasa = (DigitX - (float)LoopX)*20;

2nd Set–> LoopX = (FileSizeX-(pointerX+11))/20;
DigitX = (FileSizeX-(pointerX+11.0))/20.0;
WriteBiasa = (DigitX - (float)LoopX)*20.0;

/Answer Needed in (int)***/

1st Set–> LoopX = 4
WriteBiasa = 5

2nd Set–> LoopX = 7
WriteBiasa = 9

/Answer Given in (int)***/

1st Set–> LoopX = 4
WriteBiasa = 5

2nd Set–> LoopX = 7
WriteBiasa = 8

  • The mathematical operation involves float numbers, but the final answers are needed in (int).
    However, converting from float to int (e.g.: from (float) 9.00 to (int)) for certain number produces a wrong answer.

Actually what your are doing is calculating the rest of a division

1st Set--> LoopX = pointerX/20;
DigitX = pointerX/20;
WriteBiasa = (DigitX - (float)LoopX)*20;

WriteBiasa is the rest of the division of pointerX by 20, that you can simply write

int FileSizeX = 245;
int pointerX = 85;
int WriteBiasa = pointerX % 20;

2nd Set--> LoopX = (FileSizeX-(pointerX+11))/20;
DigitX = (FileSizeX-(pointerX+11.0))/20.0;
WriteBiasa = (DigitX - (float)LoopX)*20.0;

Similarly, you can write

WriteBiasa = (FileSizeX-(pointerX+11)) % 20;

Thank you for your help. I really appreciate it. I have to admit it really blown my mind how simple that program can be written. Hahaha..

Still need to learn a lot..

Anyway, THANKS again!