Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Double precision

Author
Message
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 26th Jun 2019 00:59
Does anyone have any code for implementing double precision (I basically want to represent values into the trillions)?
I know there is a plugin but i would like to use it on mobile devices as well so i need it implemented in basic code
SFSW
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location:
Posted: 26th Jun 2019 22:51 Edited at: 27th Jun 2019 00:56
Sounds like you are after just integers? There are a couple of ways to go about this with smaller (4-byte) integers. If the value isn't important for handling continuously in a loop, you can break it down into its eight 8-bit values into a memblock or array. From there, your math operations apply across those values. However, that can be pretty tedious and difficult to manage, including reading into and out of. So I'll go over what I think is the easier and better method below.

Another way is to use two values, one to hold the high count and one to hold the low count. Since 32-bit values range from −2,147,483,648 through 2,147,483,647, you'd cap the low value to something like 1-billion and then handle the billions with the high value. So you'd have something like:

LowInt = -1,000,000,000 to 1,000,000,000
HighInt = -1,000,000,000 (for -1,000,000,000 X 1,000,000,000) to 1,000,000,000 (for 1,000,000,000 X 1,000,000,000)

So your total capacity is then a billion billions or 1,000,000,000,000,000,000 for a full Sextillion range.

Most of your math can/should take place just with the low value and then any time the result is above or below the next billion, you increase or decrease the HighInt value accordingly. So it would be important to keep your math operations within the billion range, otherwise, you'll need to break those down also into smaller calculations so your HighInt can be adjusted at a billion per equation properly. An example of keeping track of this just in the positive range might look like:



You'd either keep that in your loop or as a separate function/gosub any time you perform a calculation involving these values. Then for displaying, you port the two values over to a string as needed. Something like:



Now 's$' contains the total value that you can display on screen if needed. If you want commas, here is a quick function to add those to the string:



So now the output 'o$' will contain the value with commas for better legibility. This only real limitation you need to contend with is the scope of your math equations relative to the size of the 'HighInt' you set up. Otherwise, this approach can easily handle values well into the trillions and beyond.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 27th Jun 2019 00:02 Edited at: 27th Jun 2019 00:03
That is very very nice! Thanks!
How would i say, calculate a percentage of the value or apply/use it in an equation?
SFSW
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location:
Posted: 27th Jun 2019 00:44 Edited at: 27th Jun 2019 00:59
blink0k wrote: "That is very very nice! Thanks!
How would i say, calculate a percentage of the value or apply/use it in an equation?"


It depends. I haven't run/checked this code, so consider it pseudo code for now, but it should work something like the examples below. For equations, I'd just do what I recommended and keep them contained to the LowInt value where possible. So for an addition example:

LowInt = LowInt + 5843028

Then with the bounds check I posted earlier, if it passes the next billion tier, values are transferred and offset as needed automatically. If the value going in is over 1-billion, then you just need to add the number of billions to the HighInt value first, then add what remains to the LowInt value. I'll illustrate this in code below with a percentage increase example.

For percentages with something over a billion, you can transfer the billion state over to a float, so something like this to calcuate a 40% percentage for a starting value of 1,250,000,000:



This will equal 0.4 in the float value, so equivalent to 400,000,000 stored in the TempHighInt value. You can single line the calculation if you want to, but I've spread it out for clarity/simplicity. Then be sure to apply the same percentage reduction to your LowInt value (which is 250,000,000) also:



This will equal 100,000,000. So now all you need to do is add the two together with the TempHighInt value representing its (reduced) value in billions ratio plus the LowInt value at its 1:1 ratio. So:



And your target value is 500,000,000 for a direct 1,250,000,000 * 0.4 calculation, so it'll match. It works in the other direction also, you just need to account for the expanding billions value. So for 280% (we'll go over a billion added for this to show how it can work, calcs spread out to help illustrate steps):



And if you are running the bounds check routine I posted earlier, this will automatically correct the additional billion that the HighInt value needs to increase by while keeping everything within the limits of the values you need to manage. So in this example, HighInt increases to '2' for the 2 billion, and LowInt increases to 700,000,000 initially, then another 800,000,000 after including the left over value from the HighInt increase. The bounds check will add another 1 to the HighInt for the 1.5-billion LowInt increases to, then LowInt will be cropped to 500-million. So the total result will be 3-billion (HighInt = 3) and 500-million (LowInt = 500000000). And that is the same result you'd get with a direct 1,250,000,000 * 2.8 calculation.

Login to post a reply

Server time is: 2024-11-22 21:45:38
Your offset time is: 2024-11-22 21:45:38