Re: BIG Numbers (Richard 23)
Re: BIG Numbers (Richard 23)
- Subject: Re: BIG Numbers (Richard 23)
- From: Cem Karan <email@hidden>
- Date: Mon, 13 Nov 2000 14:09:38 -0500
>
Re: BIG Numbers (Richard 23)
>
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.
The rest has been deleted.
The limit for integers is (2^32) - 1. The problem is that this is for unsigned integers (I think that applescript was written in C or C++ internally, if you know C, you'll understand what I'm talking about) However, if the internal representation is all signed numbers, then you'll have problems at (2^31) - 1 instead. The workaround is fairly simple, but it looks like a hack. Use two counters. Have one count up to some power of two, and then start over. When it flips back, increment the other counter by one. This won't tell you every byte, but it will tell you multiples of bytes. So, in C pseudocode it would look like this (forgive me for using C rather than applescript, but I know C very well, and am an absolute newbie to applescript)
unsigned integer littleCounter = 0;
unsigned integer bigCounter = 0;
while (there is more stuff to count up) do
{
littleCounter = littleCounter + 1;
if (littleCounter < 16) // Remember that you start counting from 0, not 1
{
bigCounter = bigCounter + 1;
littleCounter = 0;
}
}
This littleCounter works over the range [0,15] inclusive, which means that if you take the contents of bigCounter and multiply by 16 and add what ever is in littleCounter at the end, that is the number of bytes you have. I don't know how much data you have to check for your DVDs but by adjusting 15 up or down, you adjust how much bigCounter and littleCounter hold. If you want to max things out, then you can use the code below:
unsigned integer littleCounter = 0;
unsigned integer bigCounter = 0;
while (there is more stuff to count up) do
{
littleCounter = littleCounter + 1;
if (littleCounter == 0)
{
bigCounter = bigCounter + 1;
}
}
bigCounter now needs to be multiplied by 2^32 and have littleCounter added to it to get the total amount. The maximum that this can handle is 18446744073709551615 bytes, as long as integers don't change in the size of number that they can hold. But if they do, then the technique from above can be used to add another huge amount of storage capacity on.
The only problem with this is that if Apple ever decides to change how much integers can hold, then this code will suddenly seem to break; your assumptions on the amount of data that you are counting won't hold up any more.
Also, don't use floating point numbers to find out how many bytes of data you have; Because of how they store information, they are inherently 'lossy' You'll have a pretty good approximation of how much data you have, but the technique above will give you EXACTLY how much data you have.
Lots of luck, if you need more help, email me directly, I'll see what I can do.