Re: BIG Numbers
Re: BIG Numbers
- Subject: Re: BIG Numbers
- From: Chris Nebel <email@hidden>
- Date: Mon, 13 Nov 2000 12:06:01 -0800
- Organization: Apple Computer, Inc.
Jeff Benjamin wrote:
>
How big can numbers get when using integers and math w/applescript?
>
>
I need to add up bytes in files until I have enough to burn to a DVD.
Integers go from +536870911 ... -536870911; real numbers go from
+/-1.7e+308 to +/-1.7e-307. (For the geeky types, that's a 30-bit
twos-complement integer and an IEEE double, respectively.)
That said, there are a few bugs to watch out for.
1. Integral results don't promote into reals when they go out of range;
they just roll over in the the integer space. That means you see things
like 536870911 + 1 --> -536870912, rather than 536870912.0 as you
should. This is fixed in AppleScript 1.5; in the meantime, you can work
around it by throwing in a few "as real"s. (E.g. "536870911 as real +
1" gives you the right result.)
2. "info for" returns file and folder sizes as 32-bit signed integers
with a similar rollover problem, so you'll get bizarre results for
anything bigger than 2GB. This is also fixed in AppleScript 1.5, but
there's no workaround other than avoiding "info for".
Also, because of how disk storage works, simply adding the byte sizes
together will give you incorrect results -- you won't be able to fit all
the files on that your algorithm says you ought to. The trick is that
file storage comes in fixed-size chunks (aka blocks), and only one file
can be in any given block. On HFS+, the block size is 4K. This is
where the "on disk" size comes from in Get Info windows -- if you check,
you'll find that it's always a multiple of the block size. (I don't
know for sure what it is on DVD, but 4K is a reasonable guess. To find
the real answer, look at the Get Info for a very small file on a DVD,
one less than 100 bytes or so. The size on disk will be the block
size.)
Anyway, when you get a file size, you should round it up to the nearest
4K and use that instead. If b is the size you get from "info for",
saying "(round (b / 4096.0) rounding up) * 4" will give you the size on
disk in K. Alternatively, you could ask the Finder for the "physical
size" of the item, and then divide by 1024 to get it in K. If you keep
the number in K rather than bytes, then you can do all your math using
integers, since a DVD is only about 2^22 Kbytes.
--Chris Nebel
AppleScript Engineering