Results 1 to 9 of 9

Thread: Double to String

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Double to String

    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?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    _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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    amac
    Guest
    I found the best... and easiest way was to use sprintf()

    Code:
    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    The problem is, I want to output as many digits of precision as
    possible. I have not used sprintf( ) much, but I tried this:
    Code:
    #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.

  5. #5
    jim mcnamara
    Guest
    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.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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.

    Code:
    #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.
    Last edited by wey97; Nov 2nd, 2001 at 02:43 AM.

  8. #8
    jim mcnamara
    Guest
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Maybe you could have use of a fraction datatype
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width