tokkun I'll die. This is just week 5 homework in an intro course, there's no way it's that complicated!Double precision floating point is a binary format. I do not recommend trying to process it using decimal arithmetic. It will be a lot easier to avoid rounding errors if you work with base-2 arithmetic.
I would suggest denormalizing the number by repeatedly multiplying or dividing it by 2 until you have converted the mantissa to an unsigned 64-bit integer and have the exponent for the denormalized number.
Alternatively, you could do this by bit casting the double to a long int and extracting the bit fields, but it sounds like you are saying you are not allowed to use pointers.
From that point, you should be able to separate the the mantissa into the integral and fractional parts using bit operations. At that point, it is safe to do the conversion with base-10 arithmetic.
There are a couple things to be careful about with this approach. Don't forget about the implicit leading one. Make sure to take the absolute value of the input number. Figure out whether you are expected to handle special cases, like zero, subnormals, NaN, or infinity.
Let me also add that doing a C-style cast of double to int
is not safe, because it is possible for a double to hold values that are larger than the maximum int value.Code:(int) my_double
I desperately wanted to avoid this, but maybe they wanted me to do the entire calculation while going over the buffer with a single char?
Took a quick look at it:
meh