V
V
Vadimm10012018-06-15 11:45:22
Atmel AVR
Vadimm1001, 2018-06-15 11:45:22

How to display floating point numbers on LCD?

There are two functions, one displays an integer, the other a single number after the dot. The second function does not work, that is, it simply displays zero, but if an argument without an integer is passed to the second function, then everything works without problems.

void return_celoe(double data)
{
  double ostatok, celoe,data1;
  unsigned char otvet,mass[10],count=1, mass1;
  ostatok=modf(data, &celoe);
  otvet=celoe;
  data1=celoe;
  if(otvet==0)
  lcd_dat(0x30);
  while(otvet>0)
  {
  data1/=10.0;
  otvet=data1;
  ostatok=modf(data1, &celoe);
  mass1=ostatok*10.0+0x30;
  mass[count]=mass1;
  count++;
  }
  while(count>1)
  {
  lcd_dat(mass[--count]);	
  }
}
void return_ostatok(double data)
{
  
  double ostatok, celoe, data1=data;
  int otvet, sto;
  ostatok=modf(data1, &celoe);
  otvet=ostatok*10.0;
  lcd_dat(otvet+0x30);
}


int main(void)
{
  lcd_init();
  return_celoe(7990324.9); /*НА РАЗНИЦУ МЕЖДУ ЦЕЛЫМИ ЧИСЛАМИ НЕ ОБРАЩАЙТЕ ВНИМАНИЯ*/
  lcd_dat('.');
  return_ostatok(7999.9); /*ЗДЕСЬ ЕСЛИ НАПИСАТЬ НЕ 7990324.9 А 0.9 ТО ВСЕ РАБОТАЕТ, ТАКЖЕ ЕСЛИ ПЕРЕДАТЬ 7999.9 ТО НА ЭКРАН ВЫВОДИТСЯ 7999.8
              А ЕСЛИ 799.9 ТО ТАК ЖЕ 799.9 ПОЧЕМУ ТАК?																												 */
  
}

As I think, the larger the integer, the more the remainder is stretched

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maria Maltseva, 2018-06-15
@Vadimm1001

As far as I understand, the problem is that the remainder is issued with plus or minus one?
This is due to problems with precision in floating point numbers: for example, the number 7.9 can be stored in computer memory as 7.9000000..., or maybe as 7.8999999
You can round to the nearest integer instead of a simple assignment:
otvet=round(ostatok*10.0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question