World: r3wp
[Core] Discuss core issues
older newer | first last |
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. |
Graham 26-Jan-2010 [15685] | >> to-path "a/b/c" == a/b/c |
james_nak 26-Jan-2010 [15686] | Graham, here's the issue: I have some vid objects such as ua-fname: field. They follow a pattern in that the "ua" stands for "User Add" and the rest corresponds to the actual field name in the DB. The function I am writing is created in a way to be very generic and knows how to handle an insert into the DB based on a few parameters. What I am looking for is a way to "create" a name by joining "ua-" and field name which refers to the actual vid gadget. |
Graham 26-Jan-2010 [15687] | fldname: do join "ua-" db-field-name |
james_nak 27-Jan-2010 [15688] | Graham, you are the man. That works perfectly. Thanks for the info and saving me a ton of time. That's one of those "How in the world did you know that?" things to me. I guess that "do" says "What does this mean to me?" - the "me" being Rebol . I appreciate your help (once again). |
Graham 27-Jan-2010 [15689] | James it works because data is code |
Gregg 27-Jan-2010 [15690x3] | Has anyone extended the FOR* funcs to have special handling options for the first and last elements? Gab's power-mezz, and an idea from another template generator made me think of how I do that today. Here's the basic idea: |
forskip+: func [ "Like FORSKIP, but with local FIRST? and LAST? support." [throw catch] 'word [word!] {Word set to each position in series and changed as a result} skip-num [integer!] "Number of values to skip each time" body [block!] "Block to evaluate each time" /local orig result ][ if not positive? skip-num [throw make error! join [script invalid-arg] skip-num] if not any [ series? get word port? get word ] [ throw make error! {forskip/forall expected word argument to refer to a series or port!} ] orig: get word use [first? last?] [ first?: true last?: false body: bind/copy body 'first? while [any [not tail? get word (set word orig false)]] [ if tail? skip get word skip-num [last?: true] set/any 'result do body set word skip get word skip-num first?: false get/any 'result ] ] ] | |
b: [a b c d e] forskip+ b 1 [ if first? [print 'first] print b/1 if last? [print 'last] ] | |
BrianH 27-Jan-2010 [15693x2] | FIRST? seems like HEAD? with an assumed parameter, and LAST? seems like SINGLE? with an assumed parameter. |
SINGLE? is in R3 and will be in 2.7.8. | |
Gregg 27-Jan-2010 [15695x2] | The idea is that it could apply to all the interator funcs, so you don't have different types of checks for FOR/LOOP/REPEAT, FORSKIP, or FOREACH. |
And LAST? wouldn't be like SINGLE? in the context of FORSKIP with a bump value other than 1. | |
BrianH 27-Jan-2010 [15697] | Ah, good point. Too bad we can't write mezzanine loop functions in R3 yet :( |
older newer | first last |