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
  • Hi,

    If you open definition of macros NRF_LOG_FLOAT, then you see:  

    /**
     * @brief Macro to be used in a formatted string to a pass float number to the log.
     *
     * Use this macro in a formatted string instead of the %f specifier together with
     * @ref NRF_LOG_FLOAT macro.
     * Example: NRF_LOG_INFO("My float number" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(f)))
     */
    #define NRF_LOG_FLOAT_MARKER "%s%d.%02d"
    
    /**
     * @brief Macro for dissecting a float number into two numbers (integer and residuum).
     */
    #define NRF_LOG_FLOAT(val) (uint32_t)(((val) < 0 && (val) > -1.0) ? "-" : ""),   \
                               (int32_t)(val),                                       \
                               (int32_t)((((val) > 0) ? (val) - (int32_t)(val)       \
                                                    : (int32_t)(val) - (val))*100)

    This means that any numbers will be displayed with an accuracy of two decimal places.
    Change these definitions, for example like this and you will see 6 decimal places:

    /**
     * @brief Macro to be used in a formatted string to a pass float number to the log.
     *
     * Use this macro in a formatted string instead of the %f specifier together with
     * @ref NRF_LOG_FLOAT macro.
     * Example: NRF_LOG_INFO("My float number" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(f)))
     */
    #define NRF_LOG_FLOAT_MARKER "%s%d.%06d"
    
    /**
     * @brief Macro for dissecting a float number into two numbers (integer and residuum).
     */
    #define NRF_LOG_FLOAT(val) (uint32_t)(((val) < 0 && (val) > -1.0) ? "-" : ""),   \
                               (int32_t)(val),                                       \
                               (int32_t)((((val) > 0) ? (val) - (int32_t)(val)       \
                                                    : (int32_t)(val) - (val))*1000000)

Reply
  • Hi,

    If you open definition of macros NRF_LOG_FLOAT, then you see:  

    /**
     * @brief Macro to be used in a formatted string to a pass float number to the log.
     *
     * Use this macro in a formatted string instead of the %f specifier together with
     * @ref NRF_LOG_FLOAT macro.
     * Example: NRF_LOG_INFO("My float number" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(f)))
     */
    #define NRF_LOG_FLOAT_MARKER "%s%d.%02d"
    
    /**
     * @brief Macro for dissecting a float number into two numbers (integer and residuum).
     */
    #define NRF_LOG_FLOAT(val) (uint32_t)(((val) < 0 && (val) > -1.0) ? "-" : ""),   \
                               (int32_t)(val),                                       \
                               (int32_t)((((val) > 0) ? (val) - (int32_t)(val)       \
                                                    : (int32_t)(val) - (val))*100)

    This means that any numbers will be displayed with an accuracy of two decimal places.
    Change these definitions, for example like this and you will see 6 decimal places:

    /**
     * @brief Macro to be used in a formatted string to a pass float number to the log.
     *
     * Use this macro in a formatted string instead of the %f specifier together with
     * @ref NRF_LOG_FLOAT macro.
     * Example: NRF_LOG_INFO("My float number" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(f)))
     */
    #define NRF_LOG_FLOAT_MARKER "%s%d.%06d"
    
    /**
     * @brief Macro for dissecting a float number into two numbers (integer and residuum).
     */
    #define NRF_LOG_FLOAT(val) (uint32_t)(((val) < 0 && (val) > -1.0) ? "-" : ""),   \
                               (int32_t)(val),                                       \
                               (int32_t)((((val) > 0) ? (val) - (int32_t)(val)       \
                                                    : (int32_t)(val) - (val))*1000000)

Children
No Data
Related