Thread: Need mathematical help

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Need mathematical help

    Hi. I'm working on a text-displaying function in a graphics mode, and I'm having trouble displaying double, long double and float
    (since there are no direct double-to-string functions as far as I know).

    I split the number into several parts. The part that is left is to convert a number between 0 and 1 like this:

    0.0234 -> 234
    0.934000 -> 934
    0.00000008 -> 8

    (you cut the zeros in front and back)

    I tried to do something like this, but I get floating point errors:
    Code:
    double Number=0.02370;
    
    while((Number-(double)(long int)Number)!=0)
    {
      Number*=10;
    }
    Help please?

    Or if someone know a better way to convert double -> string, I'm all ears .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    #include <sstream>
    template <class T> std::string toString(const T& val) {
     std::ostringstream strWriter;
     strWriter << val;
     return strWriter.str();
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by SilentStrike
    Code:
    #include <sstream>
    template <class T> std::string toString(const T& val) {
     std::ostringstream strWriter;
     strWriter << val;
     return strWriter.str();
    }
    I don't have sstream.h, sorry . Any help with the number transformation?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't have sstream.h, sorry
    Get a newer compiler

    >Or if someone know a better way to convert double -> string, I'm all ears
    Code:
    double d = 12345.67;
    char a[10] = {0};
    sprintf ( a, "%.2f", d );
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    I completley agree with prelude on using sprintf(). I used that alot with console graphics b/c outtextxy() only could accept strings as the argument of text to display and I was dealing with numbers alot (blackjack program).

    Plus the beauty with sprintf() is that you can round with the format specifier.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Prelude
    >I don't have sstream.h, sorry
    Get a newer compiler
    Just upgraded to 5.01 . Got template support, but still no sstream.h . No plan on switch compiler though, Borland is great...

    Originally posted by Prelude
    >Or if someone know a better way to convert double -> string, I'm all ears
    Code:
    double d = 12345.67;
    char a[10] = {0};
    sprintf ( a, "%.2f", d );
    -Prelude
    I tried this and it works pretty well. However, I want the number of digits in the decimal part to be automatically set (not just 2 as in the above example). Is this possible?

    46.890000 -> 46.89
    21.046200 -> 21.0462
    23.00 -> 23

    Anyway, thanks a lot for your help .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Don't attach the .h.. that's old, non-standard C++.

    std::ostringstream::str() (from <sstream>) returns a std::string
    std::ostrstream::str() (from <strstream>) returns a char*.

    The amount of decimals can be set with setprecision.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I tried this and it works pretty well. However, I want the number of digits in the decimal part to be automatically set (not just 2 as in the above example). Is this possible?

    Use %g.
    Code:
    double d = 12345.67;
    char a[10] = {0};
    sprintf ( a, "%g", d );

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want the number of digits in the decimal part to be automatically set
    Of course, take out the .2 format flag and just use %f.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Prelude
    >I want the number of digits in the decimal part to be automatically set
    Of course, take out the .2 format flag and just use %f.

    -Prelude
    OF COURSE I did!!! , but then it displays with maximum amount of ending zeros ( 3.56 -> 3.5600000000) which is not what I wanted...
    However, I'm now using 4 decimals. It's not pretty with integers, but it will do . Thanks again!

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Oh, by the way

    Do you know some page where you can read about "new standards", like SilentStrike said. I've used this compiler and libraries for some years now so they are probably old .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mathematical Libraries
    By kurko82 in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2008, 06:27 AM
  2. Mathematical Operation
    By madahmad1 in forum C Programming
    Replies: 29
    Last Post: 07-30-2008, 09:58 AM
  3. help with mathematical problem in C
    By feelseez in forum C Programming
    Replies: 3
    Last Post: 09-10-2006, 11:44 AM
  4. writing mathematical operations in C
    By p_s_ross in forum C Programming
    Replies: 2
    Last Post: 06-13-2003, 03:04 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM