Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > enclose a negative number in columns

Reply
Thread Tools

enclose a negative number in columns

 
 
puzzlecracker
Guest
Posts: n/a
 
      12-19-2008
Say I have,

char buffer[50];
double d=-205;
that contains a number (negative or positive), and I know what number
it is, Now I need to enclose the number in the buffer with columns if
it negative.

sprintf(buffer,"%s", d);

Here is the twist, the format varies, it can be "%s", can also be
something else. So, after I constructed that buffer with number, want
to enclose it with columns.

Any suggestion how to enclose the end buffer with them?

Thanks
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      12-19-2008
On Dec 19, 11:38*am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> puzzlecracker ha scritto:
>
> > Say I have,

>
> > * char buffer[50];
> > *double d=-205;
> > that contains a number (negative or positive), and I know what number
> > it is, Now I need to enclose the number in the buffer with columns if
> > it negative.

>
> > sprintf(buffer,"%s", d);

>
> > Here is the twist, *the format varies, it can be "%s", can also be
> > something else. So, after I constructed that buffer with number, want
> > to enclose it with columns.

>
> I fear I do not understand the problem. Why don't you simply use
>
> (if d < 0)
> * *sprintf(buffer, "%s", d)
> else
> * *sprintf(buffer, "|%s|", d)
>
> ?
>
> --
> Christian Hackl
> ha...@sbox.tugraz.a



well, I am given a final string with which contains a number. It's
constructed based on various criteria, and it would be painful to
modify every format (around 100 in total) that a number is
constructed with.

So I just want to take the final product and enclose it with columns
 
Reply With Quote
 
 
 
 
LR
Guest
Posts: n/a
 
      12-19-2008
puzzlecracker wrote:
> On Dec 19, 11:38 am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
>> puzzlecracker ha scritto:
>>
>>> Say I have,
>>> char buffer[50];
>>> double d=-205;
>>> that contains a number (negative or positive), and I know what number
>>> it is, Now I need to enclose the number in the buffer with columns if
>>> it negative.
>>> sprintf(buffer,"%s", d);
>>> Here is the twist, the format varies, it can be "%s", can also be
>>> something else. So, after I constructed that buffer with number, want
>>> to enclose it with columns.

>> I fear I do not understand the problem. Why don't you simply use
>>
>> (if d < 0)
>> sprintf(buffer, "%s", d)
>> else
>> sprintf(buffer, "|%s|", d)
>>
>> ?
>>
>> --
>> Christian Hackl
>> ha...@sbox.tugraz.a

>
>
> well, I am given a final string with which contains a number. It's
> constructed based on various criteria, and it would be painful to
> modify every format (around 100 in total) that a number is
> constructed with.
>
> So I just want to take the final product and enclose it with columns


I'm completely confused by this problem.

1) What kind of number is in buffer after sprintf(buffer,"%s",d); ? This
seems unwise to me.

2) Is this any different than putting some other characters around the
contents of whatever is in buffer if what's in buffer is not a number?

3) What if buffer were empty, ie buffer[0] == 0

4) Can you use std::string make the thing you want and copy it back?
Before you start you'll have to test the length of whatever is in buffer
to make sure that strlen(buffer) < sizeof(buffer)-3

5) Or alternatively, append whatever character you want to the end of
buffer, move all the characters in buffer to the "right", make buffer[0]
= whatever character you want. Same tests for length apply.

I feel like I must be missing some fundamental point about your question.

LR



 
Reply With Quote
 
Peter Remmers
Guest
Posts: n/a
 
      12-20-2008
puzzlecracker schrieb:
> Say I have,
>
> char buffer[50];
> double d=-205;
> that contains a number (negative or positive), and I know what number
> it is, Now I need to enclose the number in the buffer with columns if
> it negative.
>
> sprintf(buffer,"%s", d);
>
> Here is the twist, the format varies, it can be "%s", can also be
> something else. So, after I constructed that buffer with number, want
> to enclose it with columns.
>
> Any suggestion how to enclose the end buffer with them?
>
> Thanks


If I understand your problem correctly, how about:

char buffer[50];
double d=-205;

char *p = buffer;

// opening bracket
if (d < 0)
*p++ = '(';

// format your number
p += sprintf(p, "%s", d); // format mismatch?

// closing bracket
if (d < 0)
*p++ = ')';


Of course, as this is a C++ newsgroup, some std::string solution would
be more appropriate, and here you might have to worry about the buffer
length.
 
Reply With Quote
 
Peter Remmers
Guest
Posts: n/a
 
      12-20-2008
Peter Remmers schrieb:

> If I understand your problem correctly, how about:
>
> char buffer[50];
> double d=-205;
>
> char *p = buffer;
>
> // opening bracket
> if (d < 0)
> *p++ = '(';
>
> // format your number
> p += sprintf(p, "%s", d); // format mismatch?
>
> // closing bracket
> if (d < 0)
> *p++ = ')';
>
>
> Of course, as this is a C++ newsgroup, some std::string solution would
> be more appropriate, and here you might have to worry about the buffer
> length.


I was too quick. *p++ only works for the opening bracket. Would have to
use sprintf here, too. Otherwise the terminating zero would be messed up.

if (d < 0)
p += sprintf(p, "|");

p += sprintf(p, "%s", d);

if (d < 0)
p += sprintf(p, "|");

But the basic idea is the same.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-20-2008
Peter Remmers <> writes (shortened):
>if (d < 0)
> *p++ = '(';
>p += sprintf(p, "%s", d);
>if (d < 0)
> *p++ = ')';


Or,

p += ::std::sprintf( p, d < 0 ? "(%s)" : "%s", d );

But when using »sprintf« one must always be aware of
the possibility of a buffer overflow. I am not sure
whether the OP can proove that the size of p will suffice.

In C, one can use a special form of sprintf to obtain
the size needed and then allocate this, like:

char * salloc( char * const f, ... )
{ va_list a; char * b = 0;
va_start(a,f); int const s = vsnprintf( 0, 0, f, a ); va_end(a);
if( s >= 0 ){ size_t const k = 1 + s; if( b = malloc( k ))
{ va_start(a,f); vsprintf( b, f, a ); va_end(a); }}
return b; }

In C++, using ::std::string, as you wrote, will take
care of this.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-21-2008
On Dec 21, 2:52 am, blargg....@gishpuppy.com (blargg) wrote:
> Peter Remmers wrote:


> [...]
> > If I understand your problem correctly, how about:


> > char buffer[50];
> > double d=-205;


> > char *p = buffer;


> > // opening bracket
> > if (d < 0)
> > *p++ = '(';


> > // format your number
> > p += sprintf(p, "%s", d); // format mismatch?


> > // closing bracket
> > if (d < 0)
> > *p++ = ')';


> Will <0 catch negative zero?


Of course not.

> I'd use <=-0.0 above.


Which is the same as <= 0.0.

> Remember, FP is usually not two's complement, so it has
> negative zero.


Which is irrelevant in most cases. Positive and negative 0 are
required to compare equal, not less than or greater than.
They're just artifacts of the representation: a positive 0 isn't
strictly positive, and a negative 0 isn't strictly
negative---both are zero.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      12-22-2008
puzzlecracker <> writes:

> On Dec 19, 11:38*am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
>> puzzlecracker ha scritto:
>>
>> > Say I have,

>>
>> > * char buffer[50];
>> > *double d=-205;
>> > that contains a number (negative or positive), and I know what number
>> > it is, Now I need to enclose the number in the buffer with columns if
>> > it negative.

>>
>> > sprintf(buffer,"%s", d);

>>
>> > Here is the twist, *the format varies, it can be "%s", can also be
>> > something else. So, after I constructed that buffer with number, want
>> > to enclose it with columns.

>>
>> I fear I do not understand the problem. Why don't you simply use
>>
>> (if d < 0)
>> * *sprintf(buffer, "%s", d)
>> else
>> * *sprintf(buffer, "|%s|", d)
>>
>> ?


sprintf(buffer,((d<0)?"|%s|":"%s"),d);

> well, I am given a final string with which contains a number. It's
> constructed based on various criteria, and it would be painful to
> modify every format (around 100 in total) that a number is
> constructed with.
>
> So I just want to take the final product and enclose it with columns


Then why do you lie to us?
You do not have a number, you have a string!

And it looks like you want to concatenate this string with some other
when it starts with a '-' character.

#include <ciso646>
std::string f(std::string number){
return(((number.size()>0) and (number[0]=='-')) ? "|"+number+"|" : number);
}


Or perhaps the string may be left-padded:

std::string f(std::string number){
return((number.position('-')!=std::string::npos) ? "|"+number+"|" : number);
}


Or perhaps your string may be anything, and you will still have to
modify all the places where the number is formated:

class SignedString {
private:
std::string formatedNumber;
bool negative;
public:
SignedString(std::string aFormatedNumber,bool aNegativeFlag):formatedNumber(aFormatedNumber),neg ative(aNegativeFlag){}
inline std::string getFormatedNumber(){return(formatedNumber);}
inline bool getNegative(){return(negative);};
};

in places where you format your numbers:

sprintf(buffer,"...",d);
return SignedString(buffer,d<0);


then:

std::string f(SignedString number){
return(number.getNegative() ? "|"+number.getFormatedNumber()+"|" : number.getFormatedNumber());
}


But in any case, do not lie, when you have a string don't say you have a number!

--
__Pascal Bourguignon__
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-22-2008
On Dec 22, 2:20 am, blargg....@gishpuppy.com (blargg) wrote:
> James Kanze wrote:
> > On Dec 21, 2:52 am, blargg....@gishpuppy.com (blargg) wrote:

> [...]
> > > Will <0 catch negative zero?


> > Of course not.


> > > I'd use <= -0.0 above.


> > Which is the same as <= 0.0.


> > > Remember, FP is usually not two's complement, so it has
> > > negative zero.


> > Which is irrelevant in most cases. Positive and negative 0
> > are required to compare equal, not less than or greater
> > than. They're just artifacts of the representation: a
> > positive 0 isn't strictly positive, and a negative 0 isn't
> > strictly negative---both are zero.


> So it seems. Maybe it's just my implementation, but printf
> does put a negative sign before a negative zero (cout
> doesn't).


I would consider that a very serious bug. It seems to be
universal, however:

int main()
{
double d = 0.0 ;
d *= -1.0 ;
printf( "%f\n", d ) ;
std::cout << std::fixed << d << std::endl ;
return 0 ;
}

Outputs -0.000000 twice with all compilers/libraries I have
access too. That's three different librarys for printf, and
four for std::cout. And it leads to the interesting point that
two values can compare equal, but not result in the same output.

(Note that if the 0.0 is a result of rounding, there might be
some justification. If, for example, I was outputting something
like -1E20 with "%f" or std::fixed. Even then, however, I doubt
it.)

[...]
> Assuming the goal is to parenthesize values with a negative
> sign, how would one do the conditional in print()? As you
> said, negative and positive zero both compare <= -0.0.


Portably, of course, you can't, since there's no guarantee that
you have a negative zero. But the C99 standard provides the
macro "signbit()", with a footnote specifying that "The signbit
macro reports the sign of all values, including infinities,
zeros, and NaNs. If zero is unsigned, it is treated as
positive." So if your implementation supports C99 (and this
will be in C++0x), you can use that.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Martin Eisenberg
Guest
Posts: n/a
 
      12-22-2008
James Kanze wrote:
> On Dec 22, 2:20 am, blargg....@gishpuppy.com (blargg) wrote:
>> James Kanze wrote:


>> > They're just artifacts of the representation: a
>> > positive 0 isn't strictly positive, and a negative 0 isn't
>> > strictly negative---both are zero.

>
>> So it seems. Maybe it's just my implementation, but printf
>> does put a negative sign before a negative zero (cout
>> doesn't).

>
> I would consider that a very serious bug. It seems to be
> universal, however:


Signed zero is there for a reason. See
http://en.wikipedia.org/wiki/-0_(number)


Martin

--
Quidquid latine scriptum est, altum videtur.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Adding a positive number and a negative number MRAB Python 2 01-31-2009 02:41 AM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
Regexp to enclose text with P-tag Andreas N Perl 1 05-18-2004 01:43 PM
[Namespace] Could I enclose different namespaces ? Cram TeXeD XML 0 04-06-2004 06:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57