printing doubles and robustness of setw()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • utab

    printing doubles and robustness of setw()

    Dear all,

    On a formatted printing operation that I am trying to accomplish for
    doubles, I have a question. I would like to print a double, d, and its
    square, d*d, but I would like to make the argument to be computed upon
    the length of the numbers(d and d*d) so that it lines up neatly in the
    columns. For this operation, I need the number of digits in a double,
    however, due to floating point representation, the double values are
    not represented exactly. So is there a way to find the number of
    digits of a double number in a robust way for print out?

    P.S. For integers I coded a similar version like the one I explained
    above but for doubles the above problem made me think on that a bit.

    Rgds,
  • Kai-Uwe Bux

    #2
    Re: printing doubles and robustness of setw()

    utab wrote:
    Dear all,
    >
    On a formatted printing operation that I am trying to accomplish for
    doubles, I have a question. I would like to print a double, d, and its
    square, d*d, but I would like to make the argument to be computed upon
    the length of the numbers(d and d*d) so that it lines up neatly in the
    columns. For this operation, I need the number of digits in a double,
    however, due to floating point representation, the double values are
    not represented exactly.
    Nit: each double (apart from special values such as nan) represents a real
    number and does so exactly. It just might happen that you are not
    interested in that number but in a number nearby. But that is not the fault
    of the double.
    So is there a way to find the number of
    digits of a double number in a robust way for print out?
    I would print the doubles into strings and then search for the decimal
    point.
    P.S. For integers I coded a similar version like the one I explained
    above but for doubles the above problem made me think on that a bit.

    Best

    Kai-Uwe Bux

    Comment

    • utab

      #3
      Re: printing doubles and robustness of setw()

      On Apr 2, 12:14 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
      utab wrote:
      Dear all,
      >
      On a formatted printing operation that I am trying to accomplish for
      doubles, I have a question. I would like to print a double, d, and its
      square, d*d, but I would like to make the argument to be computed upon
      the length of the numbers(d and d*d) so that it lines up neatly in the
      columns. For this operation, I need the number of digits in a double,
      however, due to floating point representation, the double values are
      not represented exactly.
      >
      Nit: each double (apart from special values such as nan) represents a real
      number and does so exactly. It just might happen that you are not
      interested in that number but in a number nearby. But that is not the fault
      of the double.
      Thx for the reply. Ok, but it is not exact anyway in mathematical
      sense ;)
      exact in the *closest* sense.
      >
      So is there a way to find the number of
      digits of a double number in a robust way for print out?
      >
      I would print the doubles into strings and then search for the decimal
      point.
      I forgot to tell that I wanted to constraint myself to find a solution
      without stringstreams(b y printing to strings, you mean to use a
      stringstream I suppose). Do not find this as a stupid idea, I wanted
      to develop a solutions list for the "Accelerate d C++ book" and this
      question is related to Exercise 4.4, till then on no stringstreams are
      mentioned, this is the reason of the constraint ;)

      Best,
      >
      P.S. For integers I coded a similar version like the one I explained
      above but for doubles the above problem made me think on that a bit.
      >
      Best
      >
      Kai-Uwe Bux

      Comment

      Working...