ID:1909158
 
(See the best response by DaGilbert.)
While trying to calculate, using high values (e.g 1,000,000,000,000), dream maker keeps returning wrong results..
Anyone has any idea as for how to solve it? By either reducing the answer's accuracy, creating a new system of my own to handle calculations, or any known way to get through it?
Thanks in advance for any suggested solution / help :)
Best response
Not an expert here, and ive been using DM code for like a week but.

I believe DM uses 32 bit integers which are limited to, 2.1 bil

Not sure if you can state 64 bit integers. or how.

But that is probably what your issue is.

You might need to tone down the numbers a little. (A lot)
Here is an algorithm that will calculate the result of two positive integers multiplied by each other, represented as lists. Note, it returns the correct result in reverse.

proc/multiply(list/n, list/m)

var/length1 = length(n)
var/length2 = length(m)

var/list/result[length1+length2]

for (var/i = length1; i > 0; i --)
var/pos1 = length1 - i
for (var/k = length2; k > 0; k --)
var/pos2 = length2 - k

var/mult = n[i] * m[k]
result[pos1+pos2+1] += mult

var/r = result[pos1+pos2+1] % 10
var/d = (result[pos1+pos2+1] - r) / 10
result[pos1+pos2+1] = r
result[pos1+pos2+1+1] += d

return result

mob/verb/test()
var/list/n1 = list(1,9,9,9,4,2,3)
var/list/n2 = list(1,5,3,1,3,4,5,9,9,9)
var/list/result = multiply(n1,n2)

for (var/i = length(result); i > 0; i --)
world << "[result[i]]\..."

world << "\n"

There are nicer ways to do this, especially working in base 2.
That is incredibly inefficient, it'd probably be better to figure out what terrible situation in your game would require the use of such high number values.
In response to Nadrew
Nadrew wrote:
That is incredibly inefficient, it'd probably be better to figure out what terrible situation in your game would require the use of such high number values.
Haha. I think this is good advice for this sort of application.

Anyhow, numbers are stored as 32-bit floats. Their precision starts to become inaccurate once you get to
-16,777,216 or 16,777,216 (224). When using large numbers like that, expect some inaccuracies. It's better to do calculations with large numbers if the number worked on is also large. This is because floats will store the most significant digits (the left-most) in order to retain the highest accuracy possible.

You can get away with a bit more accurate results if you respect the 16 million it gives you, but adding 1 to some trillion will probably do nothing whatsoever.

32-bit floats in a nutshell (I think)
1 bit for sign (+ or -)
7 bits for exponent (10 to the power of -63 to 64, I am guessing)
24 bits for mantissa (the binary representation of the number -16,777,216 to 16,777,216).
It's not really that inefficient. Just O(n^2) where n is the number of digits.

#define DEBUG
mob/verb/test_performance()
var/list/n1 = list(9,9,9,9,9,9,9,9,9,9)
var/list/n2 = list(1,5,3,1,3,4,5,9,9,9)
. = 10000
while(.-- > 0) multiply(n1,n2)

10000 multiplications of 10-digit integers took 3.5 seconds on my end. If OP wants to use this to update powerlevels or something he will be more than ok.

For even larger-digit integers there are O(nlog(n)^O(1)) algorithms for multiplication.
Yes, but compared to using numbers that make sense, it's still inefficient :)
Honestly, lack of operator overloading in DM is more of a barrier to using this consistently in your game than efficiency (fuck typing BigInt.Add(BigInt3.Multiply(BigInt2)) every time you want to do something ...).
I'm not saying it's not a good thing to have when you need it, I'm saying the situations you actually need to use numbers that large are pretty rare and generally mean you're scaling values in your game vastly too high for no reason.

Outside of over-the-top math-related stuff, it's not common to need numbers above the 32-bit float range.
While I agree with you, without knowing what OP wants to use this for we are in no position to tell him/her to change the game design. This warning should be seen as a footnote to an actual solution rather than the primary answer to OP's question.
I looked at his favorites, and his hub listing, it's mostly fangames. I made an educated guess that he was getting into that 'stats so retardedly high they make no sense' part of his game creation process.
If you need to work with big values, you'll definitely need a library. But honestly, stat inflation that high is extremely silly; you can balance nothing that way. If you value game balance at all, try for much lower stats.
Alright, I'll take into consideration all of your suggestions.. Thanks (:
If you value game balance at all, try for much lower stats.

QFE