Hi all, I have binary string like this "0011010000110000001100010011000000000000000000000011111101000011". I need to convert it to double. The string may be less that 64 digits.
Finite precision. The floating point needs to store an exponent, so you can't use all the bits for the mantissa.
Maybe longdouble could work. However you may want to use a bignum library instead.
Also, change the computation algorithm, pow is overkill an add error. Use Horner's method.
@kbw: For example i have 8 numbers, i convert each of them to 8bit binary and concat the result. the result is the input for the function above.
@ne555: Now I've changed the algorithm to something like ((A*256+B)*256+C)*256+D. Where ABC is a number. I also use unsigned long long int data type, instead of double. It works, but the problem now is I cant return -1 when there is an invalid value. return 0 is not correct. Program abort or return null is preferred. Any idea how to handle this?
I'm not really familiar with c++, but I think exception won't make me enable to return -1, right?
By the way, invalid value is very very unlikely. It just for worst case scenario. I don't want something invalid pass through unnoticed. Right now I just use exit(1).
I use the function for datastage routine, so datastage job will be aborted.
If all possible return values are valid, to return errors you can either have another parameter that callers should check before they try to use the returned value, or throw an exception and catch that.