optimising recursion?
[1/3] from: petr:krenzelok:trz:cz at: 20-Mar-2002 11:15
Hi,
is my function conceptually wrong? It fails on very large network
directory ....
REBOL []
dir-size?: func [path [file!] /local dir-list total tmp][
total: 0
if not dir? path [return none] ; wrong param to func ...
dir-list: read path
if empty? dir-list [return 0] ;take care of empty dir
foreach item dir-list [
either (last to-string item) = #"/" [
tmp: dir-size? append copy path item
total: total + tmp
][
s: size? append copy path item
if not none? s [total: total + s] ; take care
of possibly locked file ....
]
]
total
]
->> ble: dir-size? %/g/control/
** Math Error: Math or number overflow
** Where: dir-size?
** Near: total: total + tmp
I haven't found dir-utils in the library, so I quickly hacked my own
solution, but maybe I am missing something ....
Thanks a lot,
-pekr-
[2/3] from: joel::neely::fedex::com at: 20-Mar-2002 6:24
Hi, Petr,
You must have a 2Gb or larger disk... ;-)
Try changing the inital value of TOTAL to floating point
and see if it helps.
Petr Krenzelok wrote:
> Hi,
>
> is my function conceptually wrong? It fails on very large network
> directory ....
>
> REBOL []
>
> dir-size?: func [path [file!] /local dir-list total tmp][
>
> total: 0
total: 0.0
> if not dir? path [return none] ; wrong param to func ...
>
> dir-list: read path
> if empty? dir-list [return 0] ;take care of empty dir
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Why? Won't it will simply
fall thru the following loop
without changing TOTAL from
its initial value?
> foreach item dir-list [
> either (last to-string item) = #"/" [
<<quoted lines omitted: 12>>
> ** Where: dir-size?
> ** Near: total: total + tmp
I can trigger that error with a simple integer calculation which
overflows integer range, but it disappears when the calculation
is done with non-integer numbers.
>> recur-test: func [n [number!]] [
[ either zero? n [1] [2 * (recur-test n - 1) + 1]]
>> recur-test 5 == 63
>> recur-test 30 == 2147483647
>> recur-test 31
** Math Error: Math or number overflow
** Where: recur-test
** Near: 2 * (recur-test n - 1) + 1
>> recur-test 40
** Math Error: Math or number overflow
** Where: recur-test
** Near: 2 * (recur-test n - 1) + 1
>> recur-test: func [n [number!]] [
[ either zero? n [1.0] [2 * (recur-test n - 1) + 1]]
>> recur-test 30 == 2147483647
>> recur-test 31 == 4294967295
>> recur-test 40 == 2199023255551
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[3/3] from: g:santilli:tiscalinet:it at: 21-Mar-2002 22:50
Hi Petr,
On Wednesday, March 20, 2002, 11:15:24 AM, you wrote:
->>> ble: dir-size? %/g/control/
PK> ** Math Error: Math or number overflow
PK> ** Where: dir-size?
PK> ** Near: total: total + tmp
The problem is:
>> a: to-integer 2 ** 31 - 1
== 2147483647
>> a + 1
** Math Error: Math or number overflow
** Where: halt-view
** Near: a + 1
but if you use a decimal! instead of integer! you can grow
further.
Try changing:
total: 0
to:
total: to-decimal 0
HTH,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted