signed value raw to physical error

Issue #6 resolved
Former user created an issue

If a signal is less than 64 bit width and it's physical value is negative,then calculate physical value just by

double Signal::rawToPhysicalValue(double rawValue)
{
    /* physicalValue = rawValue * factor + offset */
    return rawValue * factor + offset;
}

will result in a hugn value, which is not the real physical value.

The singed bits '1' filled in function

uint64_t Signal::decode(std::vector<uint8_t> & data)

are all recognized as valid value bits.

Hope reply,thank you!

Comments (12)

  1. Tobias Lorenz repo owner

    I'll check it on the weekend.

    Currently I don't see the issue in the library, although the data types are different.

    So filling in signed 1 bits for a negative value is correct for a uint64_t in decode, right? And doing calculations of positive/negative values with a double also seems correct in rawToPhysicalValue.

    I guess, the issue is the cast in between from uint64_t to double. So how do you do this? Is the rawValue as double correct, after the cast?

  2. Rock Shawn

    I wrote my own rawToPhysicalValue function, in which i did the mandatory type conversion to rawValue according to the signal size and signed type before

    return rawValue * factor + offset;
    

    That's may not a nice solution, but i got the correct physical Value by doing it.

  3. Tobias Lorenz repo owner

    Ok, so you basically followed my suggestion. You wrote an own function that does signal size and signed type casts before passing it to rawToPhysicalValue?

  4. Rock Shawn

    Yes, you can downgrade it. I only want to report my trouble in using this library. So, is that indeed a problem, or i am wrong using it ?

    Thanks!

  5. Tobias Lorenz repo owner

    Yes, maybe it's wise to add some examples on how to do the type conversions properly.

    Also the unit test (test_Signal.cpp) only shows the functions separately: decode/encode and rawToPhysicalValue/physicalToRawValue.

  6. Tobias Lorenz repo owner

    I extended the test_Signal.cpp to show how decode and rawToPhysicalValue works in combination. I used a union instead of an explicit cast to access the raw data with the right signedness. I hope that gives an appropriate example.

  7. Log in to comment