Ux Binary Data to Distribution
Name
UxHwDoubleByteArrayToDistribution, UxHwFloatByteArrayToDistribution — Convert an array of bytes in the opaque UxHw wire representation, to a float or double.
Synopsis
#include <stddef.h>
#include <uxhw.h>
double UxHwDoubleByteArrayToDistribution(const uint8_t * srcByteBuffer, size_t srcByteBufferSizeInBytes);
float UxHwFloatByteArrayToDistribution(const uint8_t * srcByteBuffer, size_t srcByteBufferSizeInBytes);
Description
On architectures that associate distributional information with floating-point values, the UxHwDoubleByteArrayToDistribution() function creates a distributional value using the Ux Binary data stored in the srcByteBufferSizeInBytes bytes of the source srcByteBuffer array.
On such architectures, the returned value is associated with the distributional value created and its floating-point value is equal to the particle value of the source Ux Binary data.
Parameters
srcByteBuffer— The source array of unsigned bytes that contains Ux Binary data.srcByteBufferSizeInBytes— The size ofsrcByteBufferin bytes.
Return Values
The UxHwDoubleByteArrayToDistribution() function returns a floating-point value equal to the particle value of the source Ux Binary data, with an associated distributional value that represents the distribution encoded in that data.
✏️ Examples
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <uxhw.h>
int
main(void)
{
/*
* Example where we initialize `value` as a Uniform distribution.
* The same steps apply for any distribution associated with `value`,
* whether parametric or empirical.
*/
double originalValue = UxHwDoubleUniformDist(0.1, 1.0);
/*
* Get the minimum number of bytes that you need to store the Ux Binary
* data of `value`.
*/
size_t destByteBufferSizeInBytes = UxHwDoubleGetSizeOfDistributionByteArrayInBytes(originalValue);
uint8_t * destByteBuffer;
ssize_t destByteBufferWrittenInBytes;
double copiedValue;
destByteBuffer = (uint8_t *) calloc(destByteBufferSizeInBytes, sizeof(uint8_t));
if (destByteBuffer == NULL)
{
printf("Memory allocation for `destByteBuffer` failed. Exiting.\n");
return 1;
}
destByteBufferWrittenInBytes = UxHwDoubleDistributionToByteArray(originalValue, destByteBuffer, destByteBufferSizeInBytes);
if (destByteBufferWrittenInBytes < 0)
{
printf("Writing to destination byte buffer failed.\n");
free(destByteBuffer);
return 1;
}
printf("Wrote %zd bytes of Ux Binary data of `originalValue` in `destByteBuffer`.\n", destByteBufferWrittenInBytes);
copiedValue = UxHwDoubleByteArrayToDistribution(destByteBuffer, destByteBufferSizeInBytes);
printf("Original distribution: %lf.\n", originalValue);
printf("Copy of original distribution: %lf.\n", copiedValue);
free(destByteBuffer);
return 0;
}