This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

when 0.000030517578125 multiplied with any number getting result as Zero

Hi

I am reading the sensor data (Irms and Vrms) via I2C protocol and multiplying the read data with 0.000030517578125 to get correct result but i am getting the result as zero

Vrms = ((m_sample[1] << 8) | m_sample[0]);
Irms = ((m_sample[3] << 8) | m_sample[2]);

float Vrms_acc = ((float)Vrms) * (0.000030517578125);
float Irms_acc = ((float)Irms) * (0.00006103515625);


NRF_LOG_INFO("-------------------\r\n");
NRF_LOG_INFO(" Recevied Data.\r\n");
NRF_LOG_INFO(" Vrms = " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(Vrms_acc));
NRF_LOG_INFO(" Irms = " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(Irms_acc));
NRF_LOG_INFO("-------------------\r\n");

---------------------------------------------------------------------------------------------------------------------------

OUTPUT

ENERGY_METER:INFO:-------------------
ENERGY_METER:INFO: Recevied Data.
ENERGY_METER:INFO: Vrms = 0.000000000000000
ENERGY_METER:INFO: Irms = 0.000000000000000
ENERGY_METER:INFO:-------------------

Thanks in advance 

Parents Reply
  • You're welcome.

    For segger, there is another solution that, in principle, works correctly.
    1. In the settings (project options -> code -> Printf/Scanf),  set the permission to output numbers with double precision through the printf function: 

    Printf Floating Point Supported = double

    Wide Characters Supported = Yes

    Note: be prepared for a slight increase in the consumption of flash and RAM memory.


    2. add header file: #include <debugio.h>

    3. use one of the following output:

    debug_printf("test: %.9f \n", 100.123456789);
    

Children
  • Thank you 

    Another problem i am facing is when i multiply Vrms and Irms with overall full scale of voltage path (i.e 230v) And

    overall full scale of current path (i.e 90 A) respectively i am getting worng result.

    float Vrms_acc = ((float)Vrms) * ((0.00003051757) * 230);
    float Irms_acc = ((float)Irms) * ((0.00006103515) * 90);

    result:

    Vrms = 0.-02088566784
    Irms = 0.-00202559488

     i am not getting why that - sign is coming.

    Thanks in advance

  • There are not enough sample values to verify.
    I can assume that there is either not enough accuracy float and you need to switch to double.

    I try this code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <debugio.h>
    
    /*********************************************************************
    *
    *       main()
    *
    *  Function description
    *   Application entry point.
    */
    int main(void) 
    {
      uint32_t Vrms = 10;
      uint32_t Irms = 10;
    
      double Vrms_acc = ((double)Vrms) * ((0.00003051757f) * 230.0f);
      double Irms_acc = ((double)Irms) * ((0.00006103515f) * 90.0f);
    
      debug_printf("Vrms : %.9f \n", Vrms_acc);
      debug_printf("Irms : %.9f \n", Irms_acc);
      
    
      for(;;)
      {
      }
    }
    
    /*************************** End of file ****************************/

    And this:

    int main(void) 
    {
      uint32_t Vrms = 10;
      uint32_t Irms = 10;
    
      float Vrms_acc = ((float)Vrms) * ((0.00003051757) * 230);
      float Irms_acc = ((float)Irms) * ((0.00006103515) * 90);
    
      debug_printf("Vrms : %.9f \n", Vrms_acc);
      debug_printf("Irms : %.9f \n", Irms_acc);
      
    
      for(;;)
      {
      }
    }

    the result is the same:

Related