128 bit integer

Hello everyone !
I’m building a game in which i have to do physical simulation.
In this game , i’ll have to manipulate huge value (from 10^-15 to 10^15), and i want to use integers.
I cannot afford to use double values, because small errors cumulation could ruin my game.

Thus, I’m considering using a 128 bit integer to handle all the calculations in my physic engine.

Do someone knows the best efficient way (CPU and RAM-wise) to have a “128 integer” in java ?
I just want to do the fallowing operations on this:

  • Addition/substraction (often)
  • Distance (often)
  • operator “greater or equal than”
  • operator equal
  • Conversion into long / int when no overflow occurs
  • Scalar multiplication (with a int or long) (rare)
  • Multiplication (rare)

I’m not wanting to use BigInteger because it is not efficient.

Bests Regards,

AM.

If all you need is up to around 10^15, then just use longs, they can go to 2^63 or so, which is almost 10^19.

Maybe you should describe what you want to do and why you think you need a large dynamic range.

128 bits is roughly enough resolution for every square angstrom on earth. I suspect you don’t actually need that.

yeah use long.
You dont want to use BigInteger because its inefficient ?
Is it really ?
I’m not sure what CERN uses, but I do know the use Java, so i would guess BigInteger, but one could look up whats appropriate - java is used in many scientific calculations and stuff, there should be viable solutions already available

if longs 64bit is not enough you could make your own class of long128, which contains 2 longs one after the other. Its possible shrug

I’m usually not the type to question if a person really needs what they are asking for… but if you need a lot of fast 128bit integers… for a game / simulation - there is something wrong. Compare it to other games/simulations and then real super computer stuff - which they do not have to be super fast I guess, pretty sure they use BigInteger

But if you really wanna go there, check for alternative bignum kinda implementations in java, like: GMP : https://bitbucket.org/dfdeshom/gmp-java/overview

Yeah. There’s a reason I posted this: wiki page. Snippet from:

“The observable universe has an estimated radius of 93 billion lightyears which is ~2119 nanometers.”

Actually, no :smiley:

Since its from 10^-15 to 10^15, its like if i want from 1 to 10^30. (Hence, it wont fit into a long).

To summerize, I want to do a “multiple scale game” where players could be HUGE or really small, in the same universe, yet interracting with each others.
And yes, i aimed at the proton/galaxy sizes ratio and roughly approximated it to -15/15.
A simple scale factor would work for the sizes of the people, but not for the “absolute coords” of them. (Unless i have multiple origins … which would be complicated …)

I guess i’ll have to use my own class with 2 longs then.
Haaa! i will have to do arithmetics again :smiley:
I hate <<'s and >>'s and &0xFFFF :smiley:

Do you mean -10^15? You can’t represent 10^-15 with ints. If you need 10^-15, you’ll have to use floats.

Nope ! I do mean 10^-15.
In memory, i’ll store from 1 to 10^30… and when i will have to display to the user, i’ll divide by 10^15 after.

I’m sure if you can work out the interaction of protons with galaxies, you’ve got a Nobel Prize in it for you just for starters. They built this big metal donut under Switzerland just to figure out how that stuff worked for just the first nanosecond or so of the universe’s existence.

When it comes to physics engines for games, you’re lucky to pull off modeling interactions of objects within two orders of magnitude of size of each other. Anyway, you may as well use BigInteger then, since the performance implications of it are going to be the very least of your problems.

That’s not a particularly huge value.

10^15 = 1 000 000 000 000 000 000
10^-15 = 0.000 000 000 000 000 001

int’s can’t hold floating point numbers.

Seems to me like all your problems would be solved by a ‘long’ but you’re just too silly to realize it. Then again I can’t make sense of what you’re trying to do. I could be wrong.

When you are getting the range from 10^-15 -> 10^15, you are essentially doing this…
i + (10^15)[/i]

not this…
i * (10^15)[/i]

I have to agree that this can be done in a long value.

A signed long is what 2^63 - 1… that is at least 10^19. (Much bigger than 10^15). All you have to do is convert the negative numbers to 1/(value) and you should be fine with just using a Java long.

Local coordinate frames. I could model points scattered through-out the observable universe in singles with reasonable local precision.

There’s a very good series on implementing a real-time procedurally generated universe by Sean O’Neil on gamasutra: http://www.gamasutra.com/view/authors/322755/Sean_O’Neil.php
Especially the third part might be an interesting read for you as it deals with problems and solutions of proper scaling.

Are you sure you don’t want 128-bit floating point? If you’re trying to deal with effects on the order of 1 ULP in fixpoint then you’re going to have rounding problems.

Sounds like he just wants to use 64 bit fixed precision to me. Unfortunately the intermediary products of several operations will therefore require 128 bits.

Cas :slight_smile:

Use doubles. That is what we use for serious science work. You get “rounding errors” with any finite representation. Strict math in java mean you get exactly the same round off so you can replicate results to the last bit easily. Some RTS games do use lock step simulation with physics using doubles. Mine does and its a lot easier than what you seem to describe.

I have also used 64bit fixed precision before as well. It is a bit of pain to be honest. It was easier to just use doubles. Of course using floats could have worked as well. But these days there is not a lot of reason to not use doubles which i do in all my scientific calculations.

Seriously you will lose far more accuracy with the way you simulate than from the number representation.

Thanks you all !

Actually, my fear using doubles is due to the epsilon value.
I remember we used to set epsilon = 10^-8 in my previous work.
I guess in theory, you can reach -12 but with sampling noise, -8 sounds fine.

well, i guess i will have to use this plus some tricks.

10^-15 is fine… but it depends on the actual math. ie matrices can be singular or close to when values are quite large really.

I will finish my answer properly. Round off error in physics simulation is typically a very small problem. The bigger problems are to do with the way the equations are solved. They are solved approximately (ie Euler, RK, leap frog) and this is where the errors come from. Not round off or any other finite representation. It also turns out that “more accurate” are not always better. But methods that say conserve energy or momentum may work better for many problems. However mostly stability is what you want. Again round off has nothing to do with it. Implicit methods tend to be stable, or in other words the simulation doesn’t just blow up, even with large step size.

Note that all of these problems exist even if you could use perfect real numbers in a computer.