Output showing ovf

When I executed the below code in arduino due board its showing the output ovf. can anyone help me how to solve this problem. The value of adc_G will be in range 0 to 1023 and gas_range_u8 will be from 0 to 8 maximum.

double compensate_G_double(int16_t adc_G, uint8_t gas_range_u8)
{
  double G = 0;
  G = 1.0 / ((0.000000125) * (double)(1 << gas_range_u8) *
  (((double)(adc_G)-512.00)+(1024.0 / 0.75)) / (1024.0/0.75));
  return G;
}

Thank you for your help.

Regards,
sarathkumar.

The “1 << gas_range_u8” got my attention.
Do you want to shift a byte, then 8 times shift to the left is too much. Perhaps you can try to use an extra variable.

  double G = 0.0;
  int range = 1 << gas_range_u8;

  G = 1.0 / ((0.000000125) * (double) range * (((double)(adc_G)-512.00)+(1024.0 / 0.75)) / (1024.0/0.75));

Maybe you have to rewrite the 0.000000125 to 125.0e-9

Koepel, you are on the right track imho

just make the 1 explicit an unsigned long and it should work

1 << gas_range_u8

==>

1UL << gas_range_u8

but I think you can optimize that function quite a bit

double compensate_G_double(int16_t adc_G, uint8_t gas_range_u8)
{
  double G = 0;
  G = 1.0 / ((0.000000125) * (double)(1UL << gas_range_u8) * (((double)(adc_G)-512.00)+(1024.0 / 0.75)) / (1024.0/0.75));
  return G;
}

==>

double compensate_G_double(int16_t adc_G, uint8_t gas_range_u8)
{
  double G = 0;
  double T = 1024.0 / 0.75;
  G = 1.0 / ((0.000000125) * (double)(1UL << gas_range_u8) * (((double)(adc_G)-512.00)+ T ) / T );
  return G;
}

==>

double compensate_G_double(int16_t adc_G, uint8_t gas_range_u8)
{
  double G = 0;
  double T = 1024.0 / 0.75;
  G = T / ((0.000000125) * (double)(1UL << gas_range_u8) * (((double)(adc_G)-512.00)+ T ) );
  return G;
}

removed some expensive float operations.

Hi Koepel, Robtillaart,

I tried all your suggestions but its still showing ovf or 0.00 only. Can you please tell me is there any other solution to get solve this.

Thank you lot.

Hi,

Its working fine now. Thank you so much.

Regards, sarathkumar.

Can you show how you solved it? For future reference?

Hi,

Actually I made a mistake in class. I forgot to include class name:: to the function which I defined, so only I got OVF.