I have the function which I believe will convert an int into a floating point value split into the sign exponent and fraction components of the value. Using IEEE 754 to represent Float values.
unsigned test(unsigned x) {
// split the given bits of sign exponent and fraction, combine to return
unsigned int sign = (x & 0x80000000) >> 31;
unsigned int expo = (x & 0x7F800000) >> 23;
unsigned int frac = (x & 0x007fffff);
return (sign << 31) | (expo << 23) | frac;
}
I'm unsure however how I could compute the halved or doubled values from this floating point representation.
unsigned doubled(unsigned x) {
// get float
// float = unsigned int to float
// doubleFloat = 2*f
// if float is not a number
// return unsigned float
// else return unsigned integer of half float
unsigned int sign = (x & 0x80000000) >> 31;
unsigned int expo = (x & 0x7F800000) >> 23;
unsigned int frac = (x & 0x007fffff);
if (expo == 0xff)
return uf;
else ...
}
To answer your question, you can add a float to a double and vice versa. Generally, the result will be made into a double , and you will have to cast it back to a float if that is what you want.
1.2 is a double (8 bytes). 1.2f is a float (4 bytes). Show activity on this post. Any literal number in your code which includes a decimal point is interpreted as a double , not a float , unless you mark it as a float by appending f .
A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .
When dividing by zero with floating point division, the result will be NaN (Not a Number). [4] When dividing mixed integer and floating point numbers, the floating point values ( float or double) are automatically converted to the double type when dividing. [5]
The floating point / double comparison is not similar to the integer comparison. To compare two floating point or double values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.
The first URL I linked to above shares several other good rules. This first one is about float and double values: All floating point values ( float and double) in an arithmetic operation (+, −, *, /) are converted to double type before the arithmetic operation in performed.
Float and Double in C Float Float is a datatype which is used to represent the floating point numbers. It is a 32-bit IEEE 754 single precision floating point number ( 1-bit for the sign, 8-bit for exponent, 23*-bit for the value. It has 6 decimal digits of precision.
It seems that you are using IEEE 754 to represent Float values.
If you want to double the binary representation, you just need to increment by 1 the expoent. The same is true if you want to half, just decrement by 1
Remember that this is true only if your number is in normal range, including even after doubling or halving
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With