问题
I have proceeded to state when I need to turn IEEE-754 single and double precision numbers into strings with base 10
. There is FXTRACT
instruction available, but it provides only exponent and mantissa for base 2, as the number calculation formula is:
value = (-1)^sign * 1.(mantissa) * 2^(exponent-bias)
If I had some logarithmic instructions for specific bases, I would be able to change base of 2exponent - bias part in expression, but currently I don't know what to do. I was also thinking of using standard rounded conversion into integer, but it seems to be unusable as it doesn't offer precise conversion. Does anybody know what is the way/basic principe for doing it? Please help.
I finally found another solution (it's in Java)
{
/* handling -infinity, +infinity and NaN, returns "" if 'f' isn't one of mentioned */
String ret = "";
if ((ret = getSpecialFloats(f)).length() != 0)
return ret;
}
int num = Float.toRawIntBits(f);
int exponent = (int)(((num >> 23) & 0xFF)-127); //8bits, bias 127
int mantissa = num & 0x7FFFFF; //23bits
/* stores decimal exponent */
int decimalExponent = 0;
/* temporary value used for calculations */
int sideMultiplicator = 1;
for (; exponent > 0; exponent--) {
/* In this loop I'm calculating the value of exponent. MAX(unsigned int) = 2^32-1, while exponent can be 2^127 pr st like that */
sideMultiplicator *= 2;
/* because of this, if top two bits of sideMultiplicator are set, we're getting closer to overflow and we need to save some value into decimalExponent*/
if ((sideMultiplicator >> 30) != 0) {
decimalExponent += 3;
sideMultiplicator /= 1000;
}
}
for(; exponent < 0; exponent++) {
/* this loop does exactly same thing as the loop before, but vice versa (for exponent < 0, like 2^-3 and so on) */
if ((sideMultiplicator & 1) != 0) {
sideMultiplicator *= 10;
decimalExponent--;
}
sideMultiplicator /= 2;
}
/* we know that value of float is:
* 1.mantissa * 2^exponent * (-1)^sign */
/* that's why we need to store 1 in betweenResult (another temorary value) */
int betweenResult = sideMultiplicator;
for (int fraction = 2, bit = 0; bit < 23; bit++, fraction *= 2) {
/* this loop is the most important one: it turns binary mantissa to real value by dividing what we got in exponent */
if (((mantissa >> (22-bit)) & 1) == 1) {
/* if mantissa[bit] is set, we need to divide whole number by fraction (fraction is 2^(bit+1) ) */
while (sideMultiplicator % fraction > 0 && (betweenResult >> 28) == 0) {
/* as we needed it before: if number gets near to overflow, store something in decimalExponent*/
betweenResult *= 10;
sideMultiplicator *= 10;
decimalExponent--;
}
betweenResult += sideMultiplicator/fraction;
}
}
/* small normalization turning numbers like 15700 in betweenResult into 157e2 (storing zero padding in decimalExponent variable)*/
while (betweenResult % 10 == 0) {
betweenResult /= 10;
decimalExponent++;
}
/* this method gets string in reqested notation (scientific, multiplication by ten or just normal)*/
return getExponentedString(betweenResult, decimalExponent);
回答1:
Formatting a floating point number is rather non-trivial. Search e.g. for the Dragon4 algorithm (here is one result).
Very, very naively, you could try this:
Handle NaN and Infinity.
Print the sign (check
< 0
). Assume henceforth the number is positive real.If
>= 1
, truncate and use familiar integer formatting to print the integral part. (There should be a machine instruction for that on any hardware that has a floating point unit.)Print the decimal separator; now keep multiplying by 10 and print the truncated integral digit.
Stop when you've reached the desired precision; think about rounding the last digit correctly.
回答2:
If it is acceptable to print as 1.d1d2d3d4d5…*2^e1e2e3, then converting a floating-point number to decimal(-ish) representation can be simple. An implementation can be found here.
If you need a scientific 1.d1d2…*10^e1e2e3 representation, then the naïve approach to repeatedly divide by 10 and extract digits from the number that you have in floating-point format. You will need some sort of multi-precision integer library. (Repeatedly multiply by 10 to extract the digits after the point.)
回答3:
Kerrek SB's solution is correct. But you can do it faster without any loop (or less number of loops). Just multiply the fraction part with 10precision. Reducing the number or multiplications also reduces the cumulative error if you do the maths in a floating-point type. For precise conversion you have to use a higher precision floating-point type.
For example you want to convert 0.1234567 with 5 digits of precision, multiply the number with 10000 and get the int part. If rounding is needed, multiply it by 100000 and round the last number
来源:https://stackoverflow.com/questions/17632150/turn-float-into-string