Click to See Complete Forum and Search --> : Double to String
wey97
Oct 31st, 2001, 12:29 AM
there are functions like itoa( ) and ltoa( ) to go from ints and longs
to strings but not a dtoa( ) to go from double to string. What is
the best way to do this - keeping all sig figs?
kedaman
Oct 31st, 2001, 01:06 AM
_ecvt - Convert double to string of specified length
_fcvt - Convert double to string with specified number of digits following decimal point
_gcvt - Convert double number to string; store string in buffer
amac
Oct 31st, 2001, 06:07 AM
I found the best... and easiest way was to use sprintf()
char strVal[32] = "";
double dblVal = 32.124;
sprintf ( strVal, "%f", dblVal );
I would also take at the format specification to take a look at precision. But this here will do it just fine.
wey97
Oct 31st, 2001, 11:03 AM
The problem is, I want to output as many digits of precision as
possible. I have not used sprintf( ) much, but I tried this:
#include<windows.h>
#include<math.h>
#include<iostream.h>
void main(){
double x = sqrt(2);
char* a = new char[50];
_gcvt(x, 50, a);
cout << a << endl;
x = sqrt(999999999999999);
_gcvt(x, 50, a);
cout << a << endl;
}
and get ~ 1.4142135623730951 which is decent
but ~ 31622776.60168377
Windows Calculator gives:
1.4142135623730950488016887242097
31622776.601683777508600634602427
but calc is using a different data structure of some sort for
the calculations.
jim mcnamara
Oct 31st, 2001, 11:10 AM
Precision -
There's something you need to know about the double datatype.
Just because there is a value in the 10th decimal position does not mean it's valid.
Double does not do decimal math perfectly. If it were a BCD datatype it would not have slop problems, but it's a binary representation. These are not rounding errors, either.
Try printf("%f\n", 2 * sqrt(2));
You will NOT get back 2.00 - 1.9999999999 or 2.000000034 maybe.
The message: don't get carried away with believing those long strings of decimal numbers mean anything.
kedaman
Oct 31st, 2001, 12:55 PM
In other words, they are approximations, you can round of the last digits (not truncation) and you get rid of those 1.9999... cases at least.
wey97
Nov 1st, 2001, 06:33 PM
Originally posted by jim mcnamara
Precision -
There's something you need to know about the double datatype.
Just because there is a value in the 10th decimal position does not mean it's valid.
Double does not do decimal math perfectly. If it were a BCD datatype it would not have slop problems, but it's a binary representation. These are not rounding errors, either.
Try printf("%f\n", 2 * sqrt(2));
You will NOT get back 2.00 - 1.9999999999 or 2.000000034 maybe.
The message: don't get carried away with believing those long strings of decimal numbers mean anything.
I think you mean sqrt(2) ^ 2
I never said all of the decimals are exact.
I don't disagree that the double type rounds. It has too at some
point. It would go on forever otherwise.
You are going to get roundoff errors either way, theres no way to
prevent that on a repeating decimal.
2.141...whatever cannot possibly be represented so that when
you square it you get exactly 2 returned.
#include <windows.h>
#include <iostream.h>
#include <math.h>
void main(){
cout.precision(20);
double a = sqrt(2);
double b = a*a;
cout << a << endl << b << endl;
char* x = new char[20];
char* y = new char[20];
_gcvt(a, 20, x);
_gcvt(b, 20, y);
cout << x << endl << y << endl;
delete[] x;
delete[] y;
}
gives:
1.4142135623731
2
1.4142135623730951
2.0000000000000004
The only reason cout seems to work is because it just happens
to round differently.
jim mcnamara
Nov 1st, 2001, 08:45 PM
First off - it's not rounding error, my friend.
Second off - I hope you never program where you have to balance
money. You will go insane, and it's not a pretty sight
Third off - I tried. Go for 40 digits of
precision. I quit.
kedaman
Nov 2nd, 2001, 01:08 AM
Maybe you could have use of a fraction datatype ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.