Home:ALL Converter>Typecasting uint64 into double precision

Typecasting uint64 into double precision

Ask Time:2022-04-22T02:14:42         Author:Dave Reagan

Json Formatter

I'm receiving a list of bytes, in an array called "info," into the subroutine shown below. They were received serially and gathered up into the array. Elements [6] - [13] of the array represent one double precision number. They were necessarily split into individual bytes for serial transmission. The method I'm using to concatenate and convert this list of bytes into a single double precision quantity is, I think, pretty standard.

The number 0x3FE3333333333333, expressed as a uint64 type should be 0.6 (decimal) when converted to double. Decimal 0.6 is the torque setpoint for a motor control scheme. I am, however, seeing the number 4.17232506e-08 (decimal), when the number is converted in this program. Can anyone tell me what's gone wrong here? Here's the code:

void
    put_speed_and_torque_together(Uint8 *info, Uint32 crc_rx, Uint32 crc_calc)
    // ***************************************************************************************
    // * Procedure Name: put_speed_and_torque_together
    // * Purpose:   Combine separate bytes of Speed Setting and Torque setting 
    // *  into Uint32 and  double-precision
    // *            data types, respectively.
    // *            Call assign_rx_to_struct.
    // * Date created: 04/01/2021
    // * By: DDR
    // ***************************************************************************************
    {
    Uint64 temp_word;
    Uint32 speed_setpoint;
    double torque_setpoint;

    if (crc_rx == crc_calc) // If CRC checks out...
    {
        // Combine group of 4 bytes representing speed_setpoint into one 32-bit word.
        speed_setpoint = ((Uint32)info[17])<<24 | ((Uint32)info[16])<<16 | ((Uint32)info[15])<<8  | (Uint32)info[14];
        set_SpeedSetpoint(speed_setpoint); // Using sets and gets to protect these variables.

        // Concatenate 8 bytes representing torque_setpoint into one word (temp_word).
        temp_word = ((Uint64)info[13])<<56 | ((Uint64)info[12])<<48 | ((Uint64)info[11])<<40 | ((Uint64)info[10])<<32 | ((Uint64)info[9])<<24 | ((Uint64)info[8])<<16 | ((Uint64)info[7])<<8  | (Uint64)info[6];

        torque_setpoint = *(double*)&temp_word;
        // Reading from right to left...
        // Point to location of temp_word ("&temp_word" is a pointer).
        // Typecast the pointer to the double type (double*).
        // Retrieve the contents of the converted pointer (*).
        // Assign that value to torque_setpoint (torque_setpoint =).
        set_TorqueSetpoint(torque_setpoint); // Using sets and gets to protect these variables.

       assign_rx_to_struct(speed_setpoint, torque_setpoint);
    }
    else
    {
        InitArrays();   // Clear all relevant data. @@
                        // Obviously something went wrong here.
                        // Should I send some kind of message to LCD?
    }
} // End put_speed_and_torque_together

Author:Dave Reagan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71959115/typecasting-uint64-into-double-precision
yy