2000-04-27 22:03:57 +02:00
|
|
|
/**********
|
|
|
|
|
Copyright 1990 Regents of the University of California. All rights reserved.
|
|
|
|
|
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
|
2001-12-02 11:03:11 +01:00
|
|
|
Modified: 2001 Paolo Nenzi
|
2000-04-27 22:03:57 +02:00
|
|
|
**********/
|
|
|
|
|
|
2001-12-02 11:03:11 +01:00
|
|
|
/* Paolo Nenzi 2001: printnum does not returns static data anymore.
|
|
|
|
|
* It is up to the caller to allocate space for strings.
|
2000-04-27 22:03:57 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-12-06 22:04:45 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2011-12-11 19:05:00 +01:00
|
|
|
#include "ngspice/ngspice.h"
|
2000-04-27 22:03:57 +02:00
|
|
|
#include "printnum.h"
|
|
|
|
|
|
|
|
|
|
int cp_numdgt = -1;
|
|
|
|
|
|
2019-12-06 22:04:45 +01:00
|
|
|
|
|
|
|
|
static inline int get_num_width(double num)
|
2000-04-27 22:03:57 +02:00
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
|
2019-12-06 22:04:45 +01:00
|
|
|
if (cp_numdgt > 1) {
|
2000-04-27 22:03:57 +02:00
|
|
|
n = cp_numdgt;
|
2019-12-06 22:04:45 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2000-04-27 22:03:57 +02:00
|
|
|
n = 6;
|
2019-12-06 22:04:45 +01:00
|
|
|
}
|
|
|
|
|
if (num < 0.0 && n > 1) {
|
2000-04-27 22:03:57 +02:00
|
|
|
n--;
|
2019-12-06 22:04:45 +01:00
|
|
|
}
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2019-12-06 22:04:45 +01:00
|
|
|
return n;
|
|
|
|
|
} /* end of function get_num_width */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This funtion writes num to buf. It can cause buffer overruns. The size of
|
|
|
|
|
* buf is unknown, so cp_numdgt can be large enough to cause sprintf()
|
|
|
|
|
* to write past the end of the array. */
|
|
|
|
|
void printnum(char *buf, double num)
|
|
|
|
|
{
|
|
|
|
|
int n = get_num_width(num);
|
2000-07-03 17:32:29 +02:00
|
|
|
(void) sprintf(buf, "%.*e", n, num);
|
2019-12-06 22:04:45 +01:00
|
|
|
} /* end of function printnum */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int printnum_ds(DSTRING *p_ds, double num)
|
|
|
|
|
{
|
|
|
|
|
const int n = get_num_width(num);
|
|
|
|
|
return ds_cat_printf(p_ds, "%.*e", n, num);
|
|
|
|
|
} /* end of function printnum_ds */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|