r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Graham
24-Jan-2010
[15635x2]
decimal! and integer!

where the latter specifies the number of decimal places
The task is to convert from scientific notation to numbers only
Steeve
24-Jan-2010
[15637x2]
I just have the feeling that it should be done with only few lines
*could be
Henrik
24-Jan-2010
[15639x2]
Graham, this is a replacement for Gabriele's version. This one is 
more complete.
The primary issue is overcoming REBOL's varying usage of scientific 
notation. That's one reason it's so big (but still smaller than Gabriele's).
Maxim
24-Jan-2010
[15641]
I've done my own, and its similar, in size and functionality... not 
much to do... the scientific notation is a pain to manage.
Graham
24-Jan-2010
[15642x2]
I suggest we fix core so that it triggers scientific notation at 
7 decimal places as it does in R3 instead of at 2 decimal places 
as it does no.
w.
Steeve
24-Jan-2010
[15644]
well, using a "mask" approach, allows more capabilities then just 
providing the number of decimals.

Like I've done here for R3: http://www.rebol.net/cgi-bin/r3blog.r?view=0302#comments
(see fnum)


It's true, it doesn't handle scientific notation as entry currently, 
but hey !, it should not take more than 2 or 3 more lines.
Graham
24-Jan-2010
[15645]
I'd rather we didn't have the problem in the first place!
Henrik
24-Jan-2010
[15646]
Yes, agree. I'm helping building a rather large app, where this is 
important and when things like this aren't trivial to solve... But 
what ever happens, I think we need a function like this in R3 and 
R2-Forward.
Gregg
24-Jan-2010
[15647]
It should be part of a general FORMAT func IMO.
BrianH
24-Jan-2010
[15648]
The disadvantage to that is that it makes the FORMAT function more 
complex, and thus slow.
Gregg
24-Jan-2010
[15649]
It's just chocies. Format, as it stands, isn't something I'll use. 
And if someone shows me a case where the overhead has a noticable 
and visible impact on their code, I will refactor a custom version 
for them. :-) 


I'm open to discussion, scenarios, and the backs of envelopes. Where 
is FORMAT likely to be used, how often will it be called, and how 
slow is too slow?
BrianH
24-Jan-2010
[15650]
Numeric formatting should be fast enough to get called in a tight 
loop for grid output. Thousands of times, really quickly.
Graham
24-Jan-2010
[15651x2]
well, if you format you change the type
in rebgui at least
BrianH
24-Jan-2010
[15653]
Sounds like RebGUI needs grid formatting.
Gregg
24-Jan-2010
[15654]
Brian, great example. It also highlights that we may want it to accept 
sets of values as well as single values.
BrianH
24-Jan-2010
[15655]
Single value formatting is exactly the kind of thing that could use 
the /into option, for buffer reuse.
Gregg
24-Jan-2010
[15656]
I have a large, non-optimzed FORMAT function, but couldn't remember 
profiling it. I just did a few quick tests.

>> time-it/count [format d "yyyy-mmm-dd"] 1000
== 0:00:00.094
>> time-it/count [format d 'rel-time] 1000
== 0:00:00.078
>> time-it/count [format 1000.01 'general] 1000
== 0:00:00.047
>> time-it/count [format 1000.01 'reb-general] 1000
== 0:00:00.031
>> time-it/count [format "Gregg" [10 right]] 1000
== 0:00:00.031
>> time-it/count [format "Gregg" [10 right #"."]] 1000
== 0:00:00.016
>> time-it/count [format true 'on-off] 1000
== 0:00

Are those results too slow?
BrianH
24-Jan-2010
[15657]
I have no idea. It's easier to judge by DP than DT (the equivalent 
of time-it).
Gregg
24-Jan-2010
[15658]
How so? I haven't used DP.
BrianH
25-Jan-2010
[15659]
It will tell you evaluations and series created, which is a bit more 
cross-platform reliable than time. Don't know how fast your CPU is.
Steeve
25-Jan-2010
[15660x3]
to compare our CPUs we can use SPEED?
==2100 on my Celeron
(with R3)
SPEED? sould be ehnanced to output system informations (like the 
CPU, frequency and OS)
Henrik
25-Jan-2010
[15663]
seems there is a bug in it:

form-decimal -100 1
== "-.100,0"

Anyone with a fix?
Pekr
25-Jan-2010
[15664]
Some time ago, I did form-decimal function too. But I am really a 
coding lamer, so dunno, if it cover at least half the cases other 
versions do. Here it is:

form-decimal: func [num /local tmp main rest sign base][

     either found? find tmp: to-string num "E" [

              parse tmp [
                 [copy main to "." 
                  skip
                  copy rest to "E"
                  |
                  copy rest to "E"
                  (main: copy "")  
                  ]
             
                 skip
                 mark: (sign: copy/part mark 1)
                 skip
                 copy base to end
               ]


        either sign = "-" [

                tmp: copy "0."

                loop ((to-integer base) - 1) [insert tail tmp "0"]
                insert tail tmp rest
        ][
                tmp: copy ""

                insert tail tmp join main rest

                loop ((to-integer base) - (length? rest)) [insert tail tmp "0"]  
                    
                           
        ] 
     
      tmp                 

     ][num] 

]
Henrik
25-Jan-2010
[15665]
Graham, wouldn't it be more appropriate to simply never output scientific 
numbering and then create a scientific formatting function?
Graham
25-Jan-2010
[15666x2]
Here's Gabriele's original version
form-decimal: func [   num     [number!]
        cifre   [integer!]

        /local sign str poscifre int frac pow
    ][   sign: either negative? num [#"-"] [""]
        str: make string! 16
        either zero? num
        [   insert str #"0"
            if cifre > 0
            [   insert/dup
                    insert
                        tail str
                        #","
                    #"0"
                    cifre
        ]   ]
        [   num: abs num
            num: form
                add
                    multiply
                        power 10 cifre
                        to-decimal num
                    0,5

                        ; mainly WINE bug workaround - might also work for larger numbers 
                        on Windows
                        if find num "E"

                        [       parse num [copy int to "." skip copy frac 15 to "E" skip 
                        copy pow to end]
                                pow: to integer! pow
                                either pow >= 0
                                [       insert/dup
                                                insert/part

                                                        insert clear num int
                                                        frac

                                                        skip frac pow
                                                #"0"

                                                pow - length? frac
                                ]
                                [       num: "0"]
                        ]
            clear any [find num "." ""]
            poscifre: skip  tail num  negate cifre
            insert/part insert str sign num
                num: skip num
                    1 + remainder
                        subtract
                            index? poscifre
                            2
                        3
            while [(index? poscifre) > (index? num)]
            [   insert/part
                    insert
                        tail str
                        #"'"
                    num
                    num: skip num 3
            ]
            if empty? str [insert str #"0"]
            if not tail? poscifre
            [   insert
                    insert/dup
                        insert
                            tail str
                            #","
                        #"0"
                        cifre - length? poscifre
                    poscifre
            ]
        ]
        str
    ]
Pekr
25-Jan-2010
[15668]
quite long :-)
Janko
25-Jan-2010
[15669]
is form-decimal for scientific format for really large/small numbers 
like 1.25e+100 ? or is it for stuff like money 12.300,00 ? I have 
a function for the money :) (formating) , it's quite short if I remember 
correct.
Graham
25-Jan-2010
[15670]
That's a good point, if you only need a couple of decimal places, 
the form decimal could be much shorter
Gregg
25-Jan-2010
[15671]
SPEED? == 2'450 here.


I very much like the idea of combining heuristics with profiling. 
But if we're just going for rough estimates, we only need to know 
if a machine is extraordinarily fast or slow. Then we say "is 20K 
calls/sec acceptable in terms or order of magnitude?"
amacleod
25-Jan-2010
[15672x3]
form-decimal 4.17E-3
== "0.0017"
form-decimal 4.17E+3
== 4170.0

positive version seems to work.
using Henriks function
first convertion does not work...
Pekr
26-Jan-2010
[15675]
negative version is correct too, no? Mine form-decimal returns the 
same ...
amacleod
26-Jan-2010
[15676x2]
sorry, Pekr's version...
should it not be 0.00417?
Pekr
26-Jan-2010
[15678x2]
ah, yes, it is buggy. "4" somehow disappeared :-)
amacleod - the fix is easy - just replace "insert tail tmp rest" 
by "insert tail tmp join main rest"
amacleod
26-Jan-2010
[15680x2]
Thanks Pekr...I did not have time to look at it but I''m working 
on an app that need this function. Thanks.
What's the easiest way to convert from money! back to decimal or 
integer?
Sunanda
26-Jan-2010
[15682]
In r2:
>> second $12.33
== 12.33
amacleod
26-Jan-2010
[15683]
Ok, I see...its a combo of string and decimal..
james_nak
26-Jan-2010
[15684]
I think I asked this before or figured it out but now the mind has 
lost the answer. I build strings (via a join) that reference existing 
objects.  Obviously the joining part is easy but of course I end 
up with a string.