The power that is C++ 
Author Message
 The power that is C++

To a fortran user this seems rather an involved way of formatting numbers.

http://www.*-*-*.com/



Fri, 28 Nov 2008 16:19:55 GMT  
 The power that is C++

Quote:

> To a Fortran user this seems rather an involved way of formatting numbers.
> http://www.devx.com/cplus/10MinuteSolution/31656/0/page/1

Another reason I never liked C++ much over C.

Java finally got a formatted output routine, though fairly recently.
(Somewhat like C's printf.)

-- glen



Fri, 28 Nov 2008 16:42:50 GMT  
 The power that is C++

Quote:


>> To a Fortran user this seems rather an involved way of formatting
>> numbers.

>> http://www.devx.com/cplus/10MinuteSolution/31656/0/page/1

> Another reason I never liked C++ much over C.

> Java finally got a formatted output routine, though fairly recently.
> (Somewhat like C's printf.)

> -- glen

Yes, I looked at the 10MinuteSolution. Don't understand why the guy
could not have used sprintf or similar to get the number formatted into
the string. Maybe he wanted to stick with stream I/O


Fri, 28 Nov 2008 19:00:32 GMT  
 The power that is C++
Quote:


> > To a Fortran user this seems rather an involved way of formatting numbers.

> > http://www.devx.com/cplus/10MinuteSolution/31656/0/page/1

> Another reason I never liked C++ much over C.

Me too. I like the printf "text template"-like style more than "concat
pieces" in C++ streams.
Of course, the latter seems to be more suitable for stream, rather than
record-based IO.

To make the discussion more Fortranic, which Fortran formatting style
do you find better, and why?
1. include text pieces in format string:
print '("The average value is ",F9.3," after ",I4," iterations.")',
av,niter
2. collect them outside
print '(a,F9.3,a,I4,a)','The average value is ','after ',' iterations'
3. Format statements
4. Other option?

I personally tend to use 1. (close to the printf style) as it usually
best represents
my "mental model" of the output line.
But it gets messy if I need to include apostrophes or quotes in the
string.
This is messy anyway, whenever I deal with strings in any language I
know.
wouldn't it be more appropriate to have _paired_ string delimiters?
Jaroslav Hajek



Fri, 28 Nov 2008 19:56:28 GMT  
 The power that is C++

Quote:

>To make the discussion more Fortranic, which Fortran formatting style
>do you find better, and why?
>1. include text pieces in format string:
>print '("The average value is ",F9.3," after ",I4," iterations.")',
>av,niter
>2. collect them outside
>print '(a,F9.3,a,I4,a)','The average value is ','after ',' iterations'

I presume you meant
print '(a,F9.3,a,I4,a)','The average value is ',av,' after ',niter, &
  ' iterations'

Quote:
>3. Format statements
>4. Other option?

I seldom use option 1 or 3. I often use option 2 because it clearly
separates what will be printed from how it will be printed. I usually
use " " around format strings and ' ' around strings to be printed,
to make it more obvious which is which. If a number in the format may
need to be changed, or if the format is complicated, especially if it
will be used several times, I often use option 4: making the format
a character variable, as in the following, which prints the same as the
original if niter < 10**4, and still copes if (as I hope is true for a
number of iterations:-) niter < 10**16 :

  character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring
  ...
  write(niterstring,"(I0)") niter
  write(fmt(12:13),"(I2)") min(4,len_trim(niterstring))
  print fmt,'The average value is ',av,' after ',niter,' iterations'

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington, New Zealand



Sat, 29 Nov 2008 06:29:04 GMT  
 The power that is C++


Quote:


> > > To a Fortran user this seems rather an involved way of formatting
numbers.

> > > http://www.devx.com/cplus/10MinuteSolution/31656/0/page/1

> > Another reason I never liked C++ much over C.
> Me too. I like the printf "text template"-like style more than "concat
> pieces" in C++ streams.
> Of course, the latter seems to be more suitable for stream, rather than
> record-based IO.

> To make the discussion more Fortranic, which Fortran formatting style
> do you find better, and why?
> 1. include text pieces in format string:
> print '("The average value is ",F9.3," after ",I4," iterations.")',
> av,niter
> 2. collect them outside
> print '(a,F9.3,a,I4,a)','The average value is ','after ',' iterations'
> 3. Format statements
> 4. Other option?

> I personally tend to use 1. (close to the printf style) as it usually
> best represents
> my "mental model" of the output line.
> But it gets messy if I need to include apostrophes or quotes in the
> string.
> This is messy anyway, whenever I deal with strings in any language I
> know.
> wouldn't it be more appropriate to have _paired_ string delimiters?
> Jaroslav Hajek

Preference is [1]. And Fortran now does do a version of what I want - I use
apostrophes (") around the format string and quotes (') within the string.


Sat, 29 Nov 2008 06:45:43 GMT  
 The power that is C++

(snip)

Quote:
>   character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring
>   ...
>   write(niterstring,"(I0)") niter
>   write(fmt(12:13),"(I2)") min(4,len_trim(niterstring))
>   print fmt,'The average value is ',av,' after ',niter,' iterations'

For C's printf() the widths are minimums, such that the field will
expand for larger numbers.  This causes funny results for large values
if one doesn't put explicit space between descriptors.  Also, I have
wondered about f format on Cray machines with 16 bit exponents in their
floating point data, including possible buffer overflow for sprintf()
(internal I/O C style, which doesn't check the size of the buffer).

-- glen



Sat, 29 Nov 2008 06:57:45 GMT  
 The power that is C++

Quote:



>>To make the discussion more Fortranic, which Fortran formatting style
>>do you find better, and why?
>>1. include text pieces in format string:
>>print '("The average value is ",F9.3," after ",I4," iterations.")',
>>av,niter
>>2. collect them outside
>>print '(a,F9.3,a,I4,a)','The average value is ','after ',' iterations'

> I presume you meant
> print '(a,F9.3,a,I4,a)','The average value is ',av,' after ',niter, &
>   ' iterations'

>>3. Format statements
>>4. Other option?

> I seldom use option 1 or 3. I often use option 2 because it clearly
> separates what will be printed from how it will be printed. I usually
> use " " around format strings and ' ' around strings to be printed,
> to make it more obvious which is which. If a number in the format may
> need to be changed, or if the format is complicated, especially if it
> will be used several times, I often use option 4: making the format
> a character variable, as in the following, which prints the same as the
> original if niter < 10**4, and still copes if (as I hope is true for a
> number of iterations:-) niter < 10**16 :

I think if we were inventing this from scratch, it would
be "obvious" that we would pick 2 for reasons that John states,
plus the fact that it reduces the nesting of " or '. Character
edit descriptors were needed before the character data type was
introduced in Fortran 77.

Quote:
>   character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring

And you can even make this a parameter if you don't plan
to change the format.

Quote:
>   ...
>   write(niterstring,"(I0)") niter
>   write(fmt(12:13),"(I2)") min(4,len_trim(niterstring))
>   print fmt,'The average value is ',av,' after ',niter,' iterations'

> -- John Harper, School of Mathematics, Statistics and Computer Science,
> Victoria University, PO Box 600, Wellington, New Zealand


--

The Fortran Company
6025 N. Wilmot Rd., Tucson, AZ 85750 USA


Sat, 29 Nov 2008 22:10:04 GMT  
 The power that is C++

Quote:




> >>To make the discussion more Fortranic, which Fortran formatting style
> >>do you find better, and why?
> >>1. include text pieces in format string:
> >>print '("The average value is ",F9.3," after ",I4," iterations.")',
> >>av,niter
> >>2. collect them outside
> >>print '(a,F9.3,a,I4,a)','The average value is ','after ',' iterations'

> > I presume you meant
> > print '(a,F9.3,a,I4,a)','The average value is ',av,' after ',niter, &
> >   ' iterations'

> >>3. Format statements
> >>4. Other option?

> > I seldom use option 1 or 3. I often use option 2 because it clearly
> > separates what will be printed from how it will be printed. I usually
> > use " " around format strings and ' ' around strings to be printed,
> > to make it more obvious which is which. If a number in the format may
> > need to be changed, or if the format is complicated, especially if it
> > will be used several times, I often use option 4: making the format
> > a character variable, as in the following, which prints the same as the
> > original if niter < 10**4, and still copes if (as I hope is true for a
> > number of iterations:-) niter < 10**16 :

> I think if we were inventing this from scratch, it would
> be "obvious" that we would pick 2 for reasons that John states,

I think we would have obsoleted the print statement as redundant with
the write statement.

- Show quoted text -

Quote:
> plus the fact that it reduces the nesting of " or '. Character
> edit descriptors were needed before the character data type was
> introduced in Fortran 77.

> >   character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring

> And you can even make this a parameter if you don't plan
> to change the format.

> >   ...
> >   write(niterstring,"(I0)") niter
> >   write(fmt(12:13),"(I2)") min(4,len_trim(niterstring))
> >   print fmt,'The average value is ',av,' after ',niter,' iterations'

> > -- John Harper, School of Mathematics, Statistics and Computer Science,
> > Victoria University, PO Box 600, Wellington, New Zealand

> --

> The Fortran Company
> 6025 N. Wilmot Rd., Tucson, AZ 85750 USA



Sat, 29 Nov 2008 22:27:41 GMT  
 The power that is C++
...

Quote:
> Preference is [1]. And Fortran now does do a version of what I want -
> I use apostrophes (") around the format string and quotes (') within
> the string.

???

A minor point, but the name of the character (') is apostrophe
and the name of the character (") is quote.  It's confusing enough
that C/UNIX people use the name "single quote" for the apostrophe
character and "double quote" for the quote character.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare



Sat, 29 Nov 2008 22:28:23 GMT  
 The power that is C++

Quote:

> I think we would have obsoleted the print statement as redundant with
> the write statement.

Since WRITE can do anything PRINT can, I use PRINT only for debugging
code, and that makes it easy to identify such code.


Sat, 29 Nov 2008 22:37:54 GMT  
 The power that is C++

Quote:


> > J...
> > I think if we were inventing this from scratch, it would
> > be "obvious" that we would pick 2 for reasons that John states,

> I think we would have obsoleted the print statement as redundant with
> the write statement.

Interestingly, although very early Fortrans had a PRINT statement,
it was not part of Fortran 66.  Then it reappeared in Fortran 77.
OTOH, PUNCH has not resurfaced.

Quote:
> > plus the fact that it reduces the nesting of " or '. Character
> > edit descriptors were needed before the character data type was
> > introduced in Fortran 77.

> > >   character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring

> > And you can even make this a parameter if you don't plan
> > to change the format.

Not to mention that it can reside at module scope.  A FORMAT statement
can not.

For the record, when not using list-directed, I generally use form #2.

Walt



Sun, 30 Nov 2008 00:15:25 GMT  
 The power that is C++


Quote:

>>   character(len=16) :: fmt = "(a,F9.3,a,I 4,a)", niterstring

>And you can even make this a parameter if you don't plan
>to change the format.

>>   ...
>>   write(niterstring,"(I0)") niter
>>   write(fmt(12:13),"(I2)") min(4,len_trim(niterstring))
>>   print fmt,'The average value is ',av,' after ',niter,' iterations'

I sent that off before realising that if the format may need to be
changed, my four statements can be replaced by one that uses option 2
(explicit character-string format in the print statement):

    print "(a,F9.3,a,I"//merge("4","0",niter<10**4)//"a)", &
      'The average value is ',av,' after ',niter,' iterations'

Fortran doesn't offer as many ways as English to express a given
thought but it does what it can:-)

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington, New Zealand



Sun, 30 Nov 2008 06:52:34 GMT  
 The power that is C++
On Tue, 13 Jun 2006 14:28:23 UTC, "James Giles"

Quote:


> ...
> > Preference is [1]. And Fortran now does do a version of what I want -
> > I use apostrophes (") around the format string and quotes (') within
> > the string.

> ???

> A minor point, but the name of the character (') is apostrophe
> and the name of the character (") is quote.  It's confusing enough
> that C/UNIX people use the name "single quote" for the apostrophe
> character and "double quote" for the quote character.

When giving one of my courses last year I was using a mixture of all
of these to try and make clear what I meant (I may have been
failing:-)) . One of the students reminded us that " is also a 'speech
mark'. Thereafter, that is the term I used.

--
Cheers - Dave.



Sun, 30 Nov 2008 14:03:48 GMT  
 The power that is C++

Quote:

> On Tue, 13 Jun 2006 14:28:23 UTC, "James Giles"


>>...

>>>Preference is [1]. And Fortran now does do a version of what I want -
>>>I use apostrophes (") around the format string and quotes (') within
>>>the string.

>>???

>>A minor point, but the name of the character (') is apostrophe
>>and the name of the character (") is quote.  It's confusing enough
>>that C/UNIX people use the name "single quote" for the apostrophe
>>character and "double quote" for the quote character.

> When giving one of my courses last year I was using a mixture of all
> of these to try and make clear what I meant (I may have been
> failing:-)) . One of the students reminded us that " is also a 'speech
> mark'. Thereafter, that is the term I used.

That doesn't really help much, though... publications in England (and
perhaps elsewhere across the pond) use single quote marks rather than
double quote marks to identify spoken bits. :-P


Sun, 30 Nov 2008 21:54:11 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Any Mac Developers, with C++( or smalltalk), Power Plant experience available

2. Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO can you help

3. Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO needed

4. Mac Developers., with C++( or smalltalk), Power Plant,Mac OS,OO experience help needed

5. Can a Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO needed

6. Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO required

7. Can a Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO needed

8. Can a Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO required

9. Can a Mac Developer, with C++( or smalltalk), Power Plant, Mac OS,OO help

10. Calling Mac Developers., with C++/smalltalk, Power Plant,Mac OS,OO experience

11. Mac Devpr., C++/Smalltalk,Power Plant,Mac OS,OO Proficient Needed In Sunny Phoenix

12. The Power Of C++ in REXX

 

 
Powered by phpBB® Forum Software