/*
converts a double to decimal with no loss of accuracy.
*/
void accurate(char *out, double x)
{
char fmt[32];
sprintf(fmt, "%%.%dg", DBL_DIG);
sprintf(out, fmt, x);
}
Any way of getting rid of the first call to sprintf() ? 6 2834
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes: sprintf(fmt, "%%.%dg", DBL_DIG); sprintf(out, fmt, x);
Any way of getting rid of the first call to sprintf() ?
sprintf(out, "%.*g", (int) DBL_DIG, x);
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message sprintf(out, "%.*g", (int) DBL_DIG, x);
Thanks. I was barking up the wrong tree with the stringise (#) operator,
which of course gives me the string "DBL_DIG".
Malcolm wrote: /* converts a double to decimal with no loss of accuracy. */ void accurate(char *out, double x) { char fmt[32];
sprintf(fmt, "%%.%dg", DBL_DIG); sprintf(out, fmt, x); }
Any way of getting rid of the first call to sprintf() ?
#define ACCURATE(s,x) sprintf((s),"%.*g",DBL_DIG,(x))
--
Martin Ambuhl
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bv**********@newsg4.svr.pol.co.uk... "Ben Pfaff" <bl*@cs.stanford.edu> wrote in message sprintf(out, "%.*g", (int) DBL_DIG, x);
Thanks. I was barking up the wrong tree with the stringise (#) operator, which of course gives me the string "DBL_DIG".
That's easily fixed (See FAQ 11.17), but you're still in trouble.
AFAIK, there's nothing barring an implementation from doing any of these in
a header source...
#define DBL_DIG __DBL_DIG
#define DBL_DIG (10)
#define DBL_DIG (FLT_DIG*2)
So code like...
#define STR(x) #x
#define STRSTR(x) STR(x)
sprintf(out, "%." STRSTR(DBL_DIG) "g", x);
....is not as maximally portable as it might seem.
--
Peter
> > /* converts a double to decimal with no loss of accuracy. */ void accurate(char *out, double x) { char fmt[32];
sprintf(fmt, "%%.%dg", DBL_DIG); sprintf(out, fmt, x); }
#define ACCURATE(s,x) sprintf((s),"%.*g",DBL_DIG,(x))
#define ACCURATE(s,n,x) snprintf((s),(n),"%.*g",DBL_DIG,(x))
I don't mean to be a spelling nazi, but IMHO a buffer overflow
(eg. when the code is ported to a system that has a higher
value for DBL_DIG) is a more serious portability "error"
than casting malloc (which often does get pointed out on here).
Please tell me if I should refrain from future comments on this
issue :)
Also, you could go
#define ACCURATE_PF(x) "%.*g",DBL_DIG,(x)
and then
printf(ACCURATE_PF(x));
or
snprintf(s, n, ACCURATE_PF(x));
etc. ol*****@inspire.net.nz (Old Wolf) wrote in message news:<84**************************@posting.google. com>... /* converts a double to decimal with no loss of accuracy. */ void accurate(char *out, double x) { char fmt[32];
sprintf(fmt, "%%.%dg", DBL_DIG); sprintf(out, fmt, x); } #define ACCURATE(s,x) sprintf((s),"%.*g",DBL_DIG,(x))
#define ACCURATE(s,n,x) snprintf((s),(n),"%.*g",DBL_DIG,(x))
I don't mean to be a spelling nazi, but IMHO a buffer overflow (eg. when the code is ported to a system that has a higher value for DBL_DIG) is a more serious portability "error" than casting malloc (which often does get pointed out on here).
Only trouble is, snprintf is only available in C99. It's a common
extension for C90 implementations, but unfortunately, it's return
value is not consistent across such implementations.
Please tell me if I should refrain from future comments on this issue :)
Also, you could go #define ACCURATE_PF(x) "%.*g",DBL_DIG,(x) and then printf(ACCURATE_PF(x));
This is generally not considered good form as it obfuscates the printf
call.
or snprintf(s, n, ACCURATE_PF(x)); etc.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: C. Sengstock |
last post by:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
>unsigned char RAM;
Is there a C typical approach...
|
by: JNY |
last post by:
Hello,
I've searched for a solution, but can't find one.
When adding two numbers I'm not getting the expected result:
int testInt = 3056;
float testMant = 0.9001;
float testTotal;
...
|
by: Carramba |
last post by:
hi!
I am wondering if the is a way to get %f work intuitive, supose I
caclulate division of 2 double's and I wan't to get nice output, in case
it only one decimal I want to print only one.. and...
|
by: Junmin H. |
last post by:
Hello, I am trying to print the range of unsigned char and unsigned int.
The one for char is working good, gives me a correct output,
however the other one for int doesnt, why?? Thanks
#include...
|
by: spooler123 |
last post by:
Just a small little program. Can not figure out what am I doing wrong.
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
double max = FLT_MAX;
|
by: neha_chhatre |
last post by:
can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check
please help me as soon as possible
|
by: ratcharit |
last post by:
Currently using cosine function in math.h
Currently I get:
1 = cos(1e^-7)
Is there another way for cos to return value of high accuracy say:
0.999999 = cos(1e^-7)
|
by: santosh |
last post by:
Hello all,
In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without...
|
by: amarapreet |
last post by:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf
thanks
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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,...
| |