c++ - Convert float to bigint (aka portable way to get binary exponent & mantissa) -


In C ++, I have a bilent class that can have an integer for arbitrary size

I want to convert the big float or double number into bilint. I have a working method, but this is a bit of hack. I have used the IEEE 754 number specification to get exponents of binary sign, mantissa and input number.

Here is the code (the sign here is ignored, this is not important):

  float input = 77e 12; Big result; // Remove sign, exponent and mantissa, according to IEEE, 754 single exact number format unsigned int * raw = reinterpret_cast & lt; Unsigned int * & gt; (And input); Unsigned int mark = * raw & gt; & Gt; 31; Unsigned index exponent = (* raw>> 23) & amp; 0xFF; Unsigned int mantissa = * raw & amp; 0x7FFFFF; // 24th bit is always 1. Results = Mentas + 0x800000; Usage of Binary exponent has left the result or correct int shift = (23 - exponent + 127); If (shift and gt; 0) result >> >> = Shift; Other results & lt; & Lt; = -shift; Cout & lt; & Lt; Input & lt; & Lt; "" & Lt; & Lt; Results & lt; & Lt; Endl;  

It works, but it's ugly, and I do not know how much that portable it is. Is there a better way to do this? Is there a less ugly, portable way to remove binary Mantissa and exponents from a float or double?


Thanks to the answer for the future generation, here is a solution that uses the freakop. It is less efficient because of the loop, but it works for the float and repeats the same, reinterpret_cast does not use or depends on any knowledge of floating point number representation.

  float input = 77e12; Big result; Intel exponent; Double fraction = framesp (input, and exponent); Result = 0; Exponent--; For (; exponent & gt;; - expant) {fraction * = 2; If (degrees> = 1) {result = = 1; Fraction = 1; } Results & lt; & Lt; = 1; }  

Can not you generally use the value?


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -