Jsoftware
High-Performance Development Platform

Format exponentials

View: New views
3 Messages — Rating Filter:   Alert me  

Format exponentials

by Zsbán Ambrus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear forum,

Is there a way to make 8!:0 use an exponential format for nonzero
numbers with absolute value below 1e_2 or make it use an exponential
format for all numbers?  I find the current default unusable because
there's no way to make it not lose precision:

   8!:0 ] 1.234567e_9
+-----------+
|0.000000001|
+-----------+
   '6' 8!:0 ] 1.234567e_9
+--------+
|0.000000|
+--------+

I could of course work this around by writing my own formatter function, such as

   '-'"_^:('_'&=)"0": 1.234567e_9
1.23457e-9

Or, more generally,

$ cat format.c
#include <stdio.h>
static char formatbuf[128];
char *
formatnumber(int digits, double val) {
        if (0 <= digits)
                snprintf(formatbuf, sizeof(formatbuf), "%.*e", digits, val);
        else
                snprintf(formatbuf, sizeof(formatbuf), "%.*g", -digits, val);
        return formatbuf;
}
$ gcc -fpic -shared -Wall -O2 -o format.so format.c
   $ jconsole
   formatnumber =: ([: < [: 15!:1 (0,_1),~
'/home/ambrus/local/lib/format.so formatnumber > x i d' 15!:0 [;])"0
   42^i:5
7.65162e_9 3.21368e_7 1.34975e_5 0.000566893 0.0238095 1 42 1764 74088
3.1117e6 1.30691e8
   ;,&' '&.> _6 formatnumber 42^i:5
         7.65162e-09 3.21368e-07 1.34975e-05 0.000566893 0.0238095 1
42 1764 74088 3.1117e+06 1.30691e+08
   (9!:14;9!:12)$0
+---------------------+-+
|j602/2008-03-03/16:45|5|
+---------------------+-+

Ambrus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Format exponentials

by Zsbán Ambrus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 12, 2009 at 12:12 AM, Zsbán Ambrus <ambrus@...> wrote:
> $ gcc -fpic -shared -Wall -O2 -o format.so format.c
>   $ jconsole
>   formatnumber =: ([: < [: 15!:1 (0,_1),~ '/home/ambrus/local/lib/format.so formatnumber > x i d' 15!:0 [;])"0

Uh, I obviously forgot a copy statement here, so try this instead:

   formatnumber =: ([: < [: 15!:1 (0,_1),~ './format.so formatnumber >
x i d' 15!:0 [;])"0

Ambrus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Format exponentials

by Roger Hui :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Consider using the dyad ": , e.g.:

   0j_5 ": 11^_10+i.4 5
3.85543e_11 4.24098e_10 4.66507e_9 5.13158e_8 5.64474e_7
6.20921e_6  6.83013e_5  7.51315e_4 8.26446e_3 9.09091e_2
1.00000e0   1.10000e1   1.21000e2  1.33100e3  1.46410e4
1.61051e5   1.77156e6   1.94872e7  2.14359e8  2.35795e9



----- Original Message -----
From: Zsbán Ambrus <ambrus@...>
Date: Wednesday, November 11, 2009 15:14
Subject: [Jprogramming] Format exponentials
To: programming@...

> Dear forum,
>
> Is there a way to make 8!:0 use an exponential format for nonzero
> numbers with absolute value below 1e_2 or make it use an exponential
> format for all numbers?  I find the current default
> unusable because
> there's no way to make it not lose precision:
>
>    8!:0 ] 1.234567e_9
> +-----------+
> |0.000000001|
> +-----------+
>    '6' 8!:0 ] 1.234567e_9
> +--------+
> |0.000000|
> +--------+
>
> I could of course work this around by writing my own formatter
> function, such as
>
>    '-'"_^:('_'&=)"0": 1.234567e_9
> 1.23457e-9
>
> Or, more generally,
>
> $ cat format.c
> #include <stdio.h>
> static char formatbuf[128];
> char *
> formatnumber(int digits, double val) {
> if (0 <= digits)
>          snprintf(formatbuf, sizeof(formatbuf), "%.*e", digits, val);
> else
>          snprintf(formatbuf, sizeof(formatbuf), "%.*g", -digits, val);
> return formatbuf;
> }
> $ gcc -fpic -shared -Wall -O2 -o format.so format.c
>    $ jconsole
>    formatnumber =: ([: < [: 15!:1 (0,_1),~
> '/home/ambrus/local/lib/format.so formatnumber > x i d' 15!:0 [;])"0
>    42^i:5
> 7.65162e_9 3.21368e_7 1.34975e_5 0.000566893 0.0238095 1 42 1764 74088
> 3.1117e6 1.30691e8
>    ;,&' '&.> _6 formatnumber 42^i:5
>          7.65162e-09
> 3.21368e-07 1.34975e-05 0.000566893 0.0238095 1
> 42 1764 74088 3.1117e+06 1.30691e+08
>    (9!:14;9!:12)$0
> +---------------------+-+
> |j602/2008-03-03/16:45|5|
> +---------------------+-+
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm