I have two variables.
variable_1 = 01000001000011110101110000101001(0x410f5c29 in IEEE-754 Floating Point format) which is 8.96 in Decimal.
float variable_2 = 0.2
I want to add them like
float variable_3 = variable_1 + variable_2; // in C++
I just need to know how will I assign them to variables so that they are taken what they are. This will include declaring variable_1 and assiging it the value.
Set an unsigned integer to the desired bits and use std::memcpy
to copy them into a float
:
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
int main()
{
uint32_t au = 0x410f5c29;
float a;
static_assert(sizeof (float) == sizeof (uint32_t),
"sizes of float and uint32_t must be the same");
std::memcpy(&a, &au, sizeof a);
float b = .2;
float c = a+b;
std::cout << std::setprecision(99);
std::cout << "a = " << a << ".\n";
std::cout << "b = " << b << ".\n";
std::cout << "c = " << c << ".\n";
}
Sample output:
a = 8.96000003814697265625. b = 0.20000000298023223876953125. c = 9.159999847412109375.