I just ran across this code (assume that buf is valid and large
enough to hold all characters printed to it):
const char *foo( double d, char *buf )
{
char *cp;
if( d == (int)d ) { /* 1 */
sprintf( buf, "%d", (int)d ); /* 2 */
}
else { /* 3 */
sprintf( buf, "%.3lf", d );
cp=buf+strlen( buf );
while( cp > buf && cp[-1] == '0' ) {
*(--cp)=0;
}
if( cp > buf && cp[-1] == '.' ) {
cp[-1]=0;
}
}
return buf;
}
1. Obviously this cast is unsafe - is it unsafe even if 0 < d < INT_MAX?
The code wants to verify that d has no decimal part, apparently.
Is floor(d) == ceil(d) an acceptable alternative? Will it work?
2. Presumably this line should be
printf( buf, "%.0lf", d );
Is that correct?
3. As near as I can make out, this attempts to ensure that there are
no trailing 0's printed, and that there is not a bare decimal
point either. Is there a better way to do that?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. 5 1106
Christopher Benson-Manica wrote: I just ran across this code (assume that buf is valid and large enough to hold all characters printed to it):
const char *foo( double d, char *buf ) { char *cp;
if( d == (int)d ) { /* 1 */ sprintf( buf, "%d", (int)d ); /* 2 */ } else { /* 3 */ sprintf( buf, "%.3lf", d ); cp=buf+strlen( buf ); while( cp > buf && cp[-1] == '0' ) { *(--cp)=0; } if( cp > buf && cp[-1] == '.' ) { cp[-1]=0; } } return buf; }
1. Obviously this cast is unsafe - is it unsafe even if 0 < d < INT_MAX? The code wants to verify that d has no decimal part, apparently. Is floor(d) == ceil(d) an acceptable alternative? Will it work?
The cast is safe if INT_MIN-1 < d < INT_MAX+1, and will
"work" as intended for that range. The floor/ceil test looks
all right, and maybe there's something you could do with modf().
2. Presumably this line should be
printf( buf, "%.0lf", d );
Is that correct?
Yes, perhaps, but probably not. The "corrected" version
assumes that `buf' already contains something like "%s%g" --
either that, or the wrong function is being called. Note, too,
that the "l" length modifier has no effect on the "%f" specifier.
3. As near as I can make out, this attempts to ensure that there are no trailing 0's printed, and that there is not a bare decimal point either. Is there a better way to do that?
Sure: you can get rid of the strlen() call by making use of
the value returned by sprintf(). But beyond that, I'm not so
sure that "123498487123123123198398734" is all that great an
improvement over "123498487123123123198398734.123" ... What's
the wider context? Making axis labels for a graph, perhaps?
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman <es*****@acm-dot-org.invalid> writes: Christopher Benson-Manica wrote: I just ran across this code (assume that buf is valid and large enough to hold all characters printed to it): const char *foo( double d, char *buf ) { char *cp; if( d == (int)d ) { /* 1 */ sprintf( buf, "%d", (int)d ); /* 2 */ }
[snip] 2. Presumably this line should be printf( buf, "%.0lf", d ); Is that correct?
Yes, perhaps, but probably not. The "corrected" version assumes that `buf' already contains something like "%s%g" -- either that, or the wrong function is being called. Note, too, that the "l" length modifier has no effect on the "%f" specifier.
Actually, I think it merely assumes that "sprintf" is spelled without
a leading 's'.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus: const char *foo( double d, char *buf ) { char *cp;
if( d == (int)d ) { /* 1 */ sprintf( buf, "%d", (int)d ); /* 2 */ } else { /* 3 */ sprintf( buf, "%.3lf", d ); cp=buf+strlen( buf ); while( cp > buf && cp[-1] == '0' ) { *(--cp)=0; } if( cp > buf && cp[-1] == '.' ) { cp[-1]=0; } } return buf; }
Is the following code a legitemate re-implementation of the function?
const char *foo( double d, char *buf )
{
unsigned int real_d, pos;
modf( d*1000, &d );
real_d=(unsigned int)d; /* assume d is in range */
pos=sprintf( buf, "%u", real_d/1000 );
if( real_d%1000 ) {
sprintf( buf+pos, ".%u", real_acc%1000 );
}
}
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
On Fri, 8 Apr 2005 14:50:53 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote: Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
const char *foo( double d, char *buf ) { char *cp;
if( d == (int)d ) { /* 1 */ sprintf( buf, "%d", (int)d ); /* 2 */ } else { /* 3 */ sprintf( buf, "%.3lf", d ); cp=buf+strlen( buf ); while( cp > buf && cp[-1] == '0' ) { *(--cp)=0; } if( cp > buf && cp[-1] == '.' ) { cp[-1]=0; } } return buf; }
Is the following code a legitemate re-implementation of the function?
const char *foo( double d, char *buf ) { unsigned int real_d, pos; modf( d*1000, &d ); real_d=(unsigned int)d; /* assume d is in range */ pos=sprintf( buf, "%u", real_d/1000 ); if( real_d%1000 ) { sprintf( buf+pos, ".%u", real_acc%1000 ); } }
What is real_acc??
<<Remove the del for email>>
Barry Schwarz <sc******@deloz.net> spoke thus: What is real_acc??
A mistake (it should be real_d).
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: chandra.somesh |
last post by:
Hi
I recently had to write a small code in a competition ,but my code was
rejected cause it failed in 1 of test cases.
The problm was .....we are given vector of strings....each string...
|
by: maadhuu |
last post by:
hello ,
i would like to know as to why double is more efficient than float .
thanking you,
ranjan.
|
by: Jesper, DK. |
last post by:
A little preamble
A year ago I had a very odd bug in my program, in an if clause it picked and executed the else statement even though the if clause should evaluate to true. Normally you know that...
|
by: Sanjeev Azad |
last post by:
I'm porting my application from VC++ 6.0 to VS .Net
and experiencing a difference in the double precisions.
I have the following code in my application
double val = 16e-6;
When I debug it,...
|
by: cj |
last post by:
VB2003. I need a large positive integer. Which is larger int64 or double?
I see int64 also apparently is known as long and will hold
-9,223,372,036,854,775,808 through...
|
by: Erick-> |
last post by:
hi all...
I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??
thanks
Erick
|
by: richard_lavoie |
last post by:
Hi,
I have something like this:
vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
I always become a
error: syntax error before `>'...
|
by: Shirsoft |
last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding
error in the 8th digit. Unfortunately because of the algorithm we use,
the errors percolate into higher digits.
C++ code is...
|
by: siryuhan |
last post by:
I am trying to apply bitwise operations on float, double, and long
double values. I do not believe this is possible natively, so I
created a wrapper class to construct a double value given two...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |