Hi. This short program has been puzzling me. the _fcvt function adds a 2 to my number during

conversion. I'm using Microsoft Visual C++ 4.0. What else can I use to convert to a string. I tried evct and I

get the same result. Thanks.

#include <stdlib.h>

void main(){

int sign, decimal_spot;

char* answer;

double startNo = 999999999999.9;

answer = _fcvt(startNo, 15, &decimal_spot, &sign) ;

// answer = 99999999999990020000000000

// the answer has a 2 in it, why? Is there a work around

}

conversion. I'm using Microsoft Visual C++ 4.0. What else can I use to convert to a string. I tried evct and I

get the same result. Thanks.

#include <stdlib.h>

void main(){

int sign, decimal_spot;

char* answer;

double startNo = 999999999999.9;

answer = _fcvt(startNo, 15, &decimal_spot, &sign) ;

// answer = 99999999999990020000000000

// the answer has a 2 in it, why? Is there a work around

}

double will store about 15 digits accurately. (That is precisely what you have above!)

long double will store about 19 digits accurately. That gives you about 4 more digits than you have now.

mainframes have larger floating points with even more accuracy, but you probably need a cray computer for 26 digits of floating point accuracy.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

The number after the decimal point is effectively stored as a fraction with the denominator being a power of 2. Change .9 to .5 or .75 or a fraction that can be respresented as something over a power of 2 (.5=1/2, .75=3/4, etc) and you'll find the fraction will be prescise. But .9 doesn't divide evenly into any powers of two until the powers get large, so when it converts it to a fraction there's a rounding error. I am not certain what fraction your .9 is, but here's an example that is close:

9 * 2^13 is 7372.8 / 8192,

which it rounds off to 7373 / 8192 for storage,

which when you convert back to decimal is 0.900024414

Changing the total value of the entire number also affects this: making the number smaller means more space is available to store the fraction, so the better the chance there's be a large power of two which can represent the fraction exactly, or at least more precisely.

The reason you don't normally see rounding errors like this is that programs usually set the digits of precision much smaller.... so that _fcvt rounds off properly when converting back to decimal. So maybe that's the real answer as to how you can avoid this problem.

Programs which need absolute precison, such as financial programs where being off by a penny is a problem, don't use floating point because of the above. They use integers, sometimes building their own math packages so that they can get 100s of digits of precision.

Hope this explains it, let me know if anything isn't clear!