enclose a negative number in columns

P

puzzlecracker

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
 
P

puzzlecracker

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)

?


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
 
L

LR

puzzlecracker said:
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)

?


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
 
P

Peter Remmers

puzzlecracker said:
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.
 
P

Peter Remmers

Peter said:
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.
 
S

Stefan Ram

Peter Remmers said:
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.
 
J

James Kanze

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.
 
P

Pascal J. Bourguignon

puzzlecracker said:
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),negative(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!
 
J

James Kanze

James said:
On Dec 21, 2:52 am, (e-mail address removed) (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top