Double to const char conversion

Dear all,
I am using C and ROOT for programming. And I need to incorporate following in my code.

 char *fps=NULL;
  int dec=0,sign=0;
  float mean = h1->GetMean(1);   //0.001298
  fps= fcvt(mean,6 , &dec, &sign);

I need to provide this mean as const char to some other function to get displayed on my final plot.

Alas, this is working. But not compltely.
For the mean value = 0.001298. It is only displaying on the plot as 1298 (depeneding on the no of char I asked for).

What I want is to display mean as 0.001298.

Please help.

Thanks a lot,
emily

Man Page for fcvt (opensolaris Section 3c) - The UNIX and Linux Forums says The high-order digit is non-zero, unless the value is 0. The
low-order digit is rounded. The position of the radix character rela-
tive to the beginning of the string is stored in the integer pointed to
by decpt (negative means to the left of the returned digits).

Maybe you want to convert with one of the options of sprintf: OPENSOLARIS Man Pages and OPENSOLARIS Commands at the UNIX and Linux Forums

$ printf '%8.6f\n' .00123456
0.001235
$

Or use decpt to figure how much of "0.0*" to tack onto the front.

sprintf( fps2, "%.*s%s", 10 + dec, "0.00000000", fps );
1 Like

thanks, :slight_smile: it worked..

PS: I prefer a no-printf() solution on inner loops for speed, and just memcpy() the bytes of "0.0000" I need to the buffer, or often even better, fwrite() or write() first the "0.0000*" bytes out and then the fps, so there is no excess copying. The idea of parsing the "%.s%s" over and over bothers me. Maybe the optimizers chew it up? Output is, after all, the point of the exercise. Work back from output to the minimum variables you need. Write stuff in pieces if pieces is how it comes. However, write() is a system call where you can lose the CPU, so the FILE buffering of fwrite() is a good thing for little pieces. And putc() is a macro, so it is always inline optimizable.

Given that FILE * is opaque, how does it macroize it?

I suspect that a FILE* points to a struct with a buffer pointer, a characters-in-buffer-count or next-character-pointer, a buffer size and a file descriptor, and putc() is a macro that directly puts one more character in the buffer, then if full writes it, returns EOF if EOF or error, else sets the characters-in-buffer-count to zero or next-character-pointer to the buffer pointer and returns the character written. There is no function call stack overhead as with not-inlined fwrite(), fputs() or fprintf(). Man Page for putc (opensolaris Section 3) - The UNIX and Linux Forums

This also seems to apply to GNU GCC and libc, too. Neat.

I am sure it warms the heartstrings of those guys who design CPUs with multiple parallel integer units. Calls and branches can slow things. Ditto getc().

PS: Back burner of brain bubble: FILE* also needs to have an EOF/error save variables for feof() and ferror(), but might rely on real EOF and errno. Let's look! I **never** looked before, I just inferred (so much cheaper!) HP-UX 11.00 stdio.h, excuse my line numbers:

63    _NAMESPACE_STD_START
64       typedef struct {
65        int         __cnt;
66        unsigned char    *__ptr;
67        unsigned char    *__base;
68        unsigned short     __flag;
69        unsigned char      __fileL;        /* low byte of file desc */
70        unsigned char      __fileH;        /* high byte of file desc */
71       } FILE;
72    _NAMESPACE_STD_END

It is very important that programmers realize all the FILE* stuff is inside the process, and it does all its work with the embedded fd. I was considering writing a new super_FILE* that, when you might block on FILE* I/O, services all FILE*, emptying write buffers and filling read buffers, maybe even with its own threads to take the system call hits.