Skip to main content

Ux Binary Data to Distribution

Name

UxHwDoubleByteArrayToDistribution, UxHwFloatByteArrayToDistribution Export a distributional value to an array of bytes.

Synopsis

#include <stddef.h>
#include <uxhw.h>

double UxHwDoubleByteArrayToDistribution(const uint8_t * destByteBuffer, size_t destByteBufferSizeInBytes);
float UxHwFloatByteArrayToDistribution(const uint8_t * destByteBuffer, size_t destByteBufferSizeInBytes);

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 destByteBufferSizeInBytes bytes of the source destByteBuffer array. On such architectures, the returned value is associated with the distributional value created. It is equivalent to the particle value of the source Ux Binary data.

Parameters

  • value The distributional value whose Ux Binary data will be exported.
  • destByteBuffer The source array of unsigned bytes that contains Ux Binary data.
  • destByteBufferSizeInBytes The size of destByteBuffer in bytes.

Return Values

The UxHwDoubleByteArrayToDistribution() function returns the particle value of the source Ux Binary 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;
}