Hello, I am making use of a formula which involves long numbers in my starcraft map.
Everything goes fine until I have to get the square root of a number greater than the real number limit (524287), then the math just overflows (circled in red above). I am not very good when it comes to math and don't know much about the best practices for decreasing the scale of the numbers. If it was a linear equation, I would just divide everything by a number and then multiply it by that number afterward. However, the presence of logarithms and exponents makes it so I'm not sure how I would decrease the scale of this equation.
Please advice on how to alter equation or ways of getting the square root of a number > 524287 in the Starcraft Editor
One might have to write a custom floating point or multi word fixed point library to solve such an equation. Such libraries are often used for micro controllers and other cheap/simple processors where larger or more complex dedicated maths instructions are missing. I suggest using a search engine to look for such code, there appears to be tons on GitHub with heavy reference to Wikipedia which might be adaptable to Galaxy.
What is it for? Maybe there are other solutions that achieve similar results without requiring large intermediate values. For example an iterative algorithm that approximates towards it within reasonable time and complexity.
Hello. I have a shallow understanding of programming and even after some research, can't really understand what custom floating points / fixed point libraries are. I do most of my coding through the GUI.
I'm trying to make a supply/demand curve with a functioning market to determine resource prices, in this case, it is food.
Would it not be possible to scale down the formula? Currently it uses some pretty large numbers.
For example 440 * population * quantity supplied. So if one was to supply 1,000 people with 1,000 units you are looking at 440,000,000 which is well past the range of the SC2 fixed point. However if one were to define population in thousands of people and supply in thousands of units then one gets 440 * 1 * 1 which is 440, well within the range of the fixed point type. Obviously the constants would need to be changed depending how they were derived. If the constants end up growing then this is counter productive so not a viable solution.
One could try rounding parts of if into integers (range of -2,147,483,648 to 2,147,483,647). However I doubt the game comes with integer square root so one would have to implement one themselves. Wikipedia has a few articles explaining. https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Otherwise you will need some kind of custom number type with custom maths functions to process the intermediate value.
I looked into custom number types/functions and that seems beyond my ability. I altered the formula to work on a smaller scale. Even if the prices don't work exactly as well as the last one, I think its reasonable enough.
Looking at especially the (p * q) part of the equation, I realized that since the values will often be around the millions, I would have to pay close attention to that part of the formula and drastically decreased the scale. Surprisingly with the reduced scale, it was off my intended output by only 5% at its worst.
You still might want to check individual sub parts such as the q^2 in case of overflow. In such a case you could clamp them to max value instead of the output of the entire thing becoming garbage.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Hello, I am making use of a formula which involves long numbers in my starcraft map.
Everything goes fine until I have to get the square root of a number greater than the real number limit (524287), then the math just overflows (circled in red above). I am not very good when it comes to math and don't know much about the best practices for decreasing the scale of the numbers. If it was a linear equation, I would just divide everything by a number and then multiply it by that number afterward. However, the presence of logarithms and exponents makes it so I'm not sure how I would decrease the scale of this equation.
Please advice on how to alter equation or ways of getting the square root of a number > 524287 in the Starcraft Editor
One might have to write a custom floating point or multi word fixed point library to solve such an equation. Such libraries are often used for micro controllers and other cheap/simple processors where larger or more complex dedicated maths instructions are missing. I suggest using a search engine to look for such code, there appears to be tons on GitHub with heavy reference to Wikipedia which might be adaptable to Galaxy.
What is it for? Maybe there are other solutions that achieve similar results without requiring large intermediate values. For example an iterative algorithm that approximates towards it within reasonable time and complexity.
In reply to Forge_User_04585675:
Would it not be possible to scale down the formula? Currently it uses some pretty large numbers.
For example 440 * population * quantity supplied. So if one was to supply 1,000 people with 1,000 units you are looking at 440,000,000 which is well past the range of the SC2 fixed point. However if one were to define population in thousands of people and supply in thousands of units then one gets 440 * 1 * 1 which is 440, well within the range of the fixed point type. Obviously the constants would need to be changed depending how they were derived. If the constants end up growing then this is counter productive so not a viable solution.
One could try rounding parts of if into integers (range of -2,147,483,648 to 2,147,483,647). However I doubt the game comes with integer square root so one would have to implement one themselves. Wikipedia has a few articles explaining. https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Otherwise you will need some kind of custom number type with custom maths functions to process the intermediate value.
In reply to Forge_User_04585675:
You still might want to check individual sub parts such as the q^2 in case of overflow. In such a case you could clamp them to max value instead of the output of the entire thing becoming garbage.