A
A
Alex_Tysel2014-10-11 21:02:21
C++ / C#
Alex_Tysel, 2014-10-11 21:02:21

C++: How to solve the problem about the mantissa and the sign of a float?

Hello, there is a problem: For the float data type, provide the ability to access both the whole number and separately its sign and mantissa. Actually the question is how to solve it, my attempts were unsuccessful, unfortunately. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-10-11
@Alex_Tysel

unfortunately my attempts were not successful.

not very informative. What are attempts? What exactly did not work out?
Look here and write:
struct float_parts {
    unsigned fraction:23;
    unsigned exponent:8;
    unsigned sign:1;
};

union float_and_parts {
    float f;
    struct float_parts parts;
};

UPD: corrected for little endian.

G
Gregory, 2014-10-12
@difiso

The math.h file contains frexp functions with signatures

float frexp( float num, int *exp);
double frexp( double num, int *exp);
long double frexp( long double num, int *exp);

Use like this:
double num, r;
int exp;
num = 6.0;
r = frexp( num, &exp);
printf("The matissa returned is::%lf",r);
printf("The exponent value stored in exp is::%d",exp);

Result:
The matissa returned is::0.750000
The exponent value stored in exp is::3

I can’t vouch for the performance of the example (copy-paste from the Internet at the request of How to get mantissa c++ ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question