unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Suggested fix in numbers.c
@ 2002-12-28 23:25 Roland Orre
  2003-01-16  1:43 ` Thien-Thi Nguyen
  0 siblings, 1 reply; 3+ messages in thread
From: Roland Orre @ 2002-12-28 23:25 UTC (permalink / raw)


Below is my standard fix to numbers.c since I started using guile 95.
It allows for instance, which is my most common use, to let the user set
the float format to the C format string "%g" etc, by setting the routine
scm_user_idbl2str to a user specifed one. This to avoid excess in
decimals, for instance when looking at single float vectors, where it's
annoying to inspect a value and see e.g.
 #s(0.00519724749028683 0.00519724749028683 0.0 0.0 0.0 ...
Of course, today, general float outputs can be nicely handled by
specific formats strings in the output generation, which is also thread
safe as different threads can have different desires, but for general
purpose interaction and prototyping I find this fix to be very useful.

	Best regards
	Roland Orre


in numbers.c
#ifdef USER_FLOAT_FORMAT
  size_t
  scm_idbl2str (double f, char *a);

  size_t (*scm_user_idbl2str)(double f,char *a);

  #define idbl2str(f,a) scm_user_idbl2str(f,a)

  size_t
  scm_idbl2str (double f, char *a)
#else
static size_t
idbl2str (double f, char *a)
#endif

and in the init routine to make the fix transparent when no user
modules are loaded.

void
scm_init_numbers ()
{
#ifdef USER_FLOAT_FORMAT
  scm_user_idbl2str=scm_idbl2str;
#endif




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Suggested fix in numbers.c
  2002-12-28 23:25 Suggested fix in numbers.c Roland Orre
@ 2003-01-16  1:43 ` Thien-Thi Nguyen
  2003-01-24 22:13   ` Roland Orre
  0 siblings, 1 reply; 3+ messages in thread
From: Thien-Thi Nguyen @ 2003-01-16  1:43 UTC (permalink / raw)
  Cc: guile-user

   From: Roland Orre <orre@nada.kth.se>
   Date: 29 Dec 2002 00:25:58 +0100

   Below is my standard fix to numbers.c [...]

thanks for this; i've installed it in 1.4.1.x cvs.  the changes were
small enough that no papers are required.  i would like to mention this
feature in the documentation; could you provide a suitable example?

thi


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Suggested fix in numbers.c
  2003-01-16  1:43 ` Thien-Thi Nguyen
@ 2003-01-24 22:13   ` Roland Orre
  0 siblings, 0 replies; 3+ messages in thread
From: Roland Orre @ 2003-01-24 22:13 UTC (permalink / raw)
  Cc: guile-user

On tor, 2003-01-16 at 02:43, Thien-Thi Nguyen wrote:
>    From: Roland Orre <orre@nada.kth.se>
>    Date: 29 Dec 2002 00:25:58 +0100
> 
>    Below is my standard fix to numbers.c [...]
> 
> thanks for this; i've installed it in 1.4.1.x cvs.  the changes were
> small enough that no papers are required.  i would like to mention this
> feature in the documentation; could you provide a suitable example?

I include here example code using a C format string for float values.

This is useful when for instance dealing with float
vectors. Assume that we make the following input:
> (define foo #s(1.1 1.3 1.7))
The foo vector will be by default be displayed as:
> foo
#s(1.10000002384186 1.29999995231628 1.70000004768372)
Now e.g. define float format to be the C-string "%g"
> (float-format "%g")
> foo
#s(1.1 1.3 1.7)

The user code below is an example of an implementation of
USER_FLOAT_FORMAT. In your setup do #define USER_FLOAT_FORMAT 1
then the following code will allow you to set float format to a C
format string:

size_t scm_idbl2str(double f, char *a);
extern size_t (*scm_user_idbl2str)(double f,char *a);
static char float_format_string[40];
static size_t user_idbl2str(double f,char *a)
{ 
  return (size_t)sprintf(a,float_format_string,f);
} /* user_idbl2str */

SCM_PROC(s_float_format,"float-format",0,1,0,float_format);
/* returns #f if default format is used otherwise returns format string.
   Optional format may be #f or format string. */
SCM float_format(SCM format)
{
  SCM res;
  if SCM_UNBNDP(format) {
    if (scm_user_idbl2str==scm_idbl2str) /* default float conversion */
      return SCM_BOOL_F;
    else {
      res=scm_makstr(strlen(float_format_string),0);
      strcpy(SCM_CHARS(res),float_format_string);
      return res;
    }
  }
  if (SCM_FALSEP(format)) {
    scm_user_idbl2str=scm_idbl2str;	 /* default float conversion */
    return SCM_BOOL_F;
  }
 
SCM_ASSERT(SCM_NIMP(format)&&SCM_STRINGP(format),format,SCM_ARG1,s_float_format);
  strcpy(float_format_string,SCM_CHARS(format));
  scm_user_idbl2str=user_idbl2str;	/* user float conversion */
  return SCM_BOOL_T;
} /* float_format */

On tor, 2003-01-16 at 02:43, Thien-Thi Nguyen wrote:
>    From: Roland Orre <orre@nada.kth.se>
>    Date: 29 Dec 2002 00:25:58 +0100
> 
>    Below is my standard fix to numbers.c [...]
> 
> thanks for this; i've installed it in 1.4.1.x cvs.  the changes were
> small enough that no papers are required.  i would like to mention this
> feature in the documentation; could you provide a suitable example?
> 
> thi



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-01-24 22:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-28 23:25 Suggested fix in numbers.c Roland Orre
2003-01-16  1:43 ` Thien-Thi Nguyen
2003-01-24 22:13   ` Roland Orre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).