Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 128 bit integer  (Read 4428 times) 0 Members and 1 Guest are viewing this topic.
ArsMagna

Junior Newbie

 « Posted 2013-02-15 15:53:48 »

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:
* 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.
BurntPizza

« JGO Bitwise Duke »

Medals: 356
Exp: 6 years

 « Reply #1 - Posted 2013-02-15 15:59:38 »

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.
Roquen
 « Reply #2 - Posted 2013-02-15 16:02:01 »

Maybe you should describe what you want to do and why you think you need a large dynamic range.
 Games published by our own members! Check 'em out!
sproingie

JGO Kernel

Medals: 202

 « Reply #3 - Posted 2013-02-15 16:11:09 »

128 bits is roughly enough resolution for every square angstrom on earth.  I suspect you don't actually need that.
Cero
 « Reply #4 - Posted 2013-02-15 16:35:34 »

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

Roquen
 « Reply #5 - Posted 2013-02-15 16:39:56 »

128 bits is roughly enough resolution for every square angstrom on earth.  I suspect you don't actually need that.
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."
ArsMagna

Junior Newbie

 « Reply #6 - Posted 2013-02-15 17:03:38 »

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.

Actually, no

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
I hate <<'s and >>'s and &0xFFFF

cheatsguy

Junior Devvie

Medals: 3

Gamer turned Pixel Artist turned Programmer

 « Reply #7 - Posted 2013-02-15 17:08:38 »

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

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
ArsMagna

Junior Newbie

 « Reply #8 - Posted 2013-02-15 17:13:28 »

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.

sproingie

JGO Kernel

Medals: 202

 « Reply #9 - Posted 2013-02-15 17:28:15 »

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.

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.

 Games published by our own members! Check 'em out!
jonjava

« In padded room »

 « Reply #10 - Posted 2013-02-15 19:01:31 »

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.
ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

 « Reply #11 - Posted 2013-02-15 19:21:14 »

Actually, no

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).

When you are getting the range from 10^-15 -> 10^15, you are essentially doing this...
(10^15) + (10^15)

not this...
(10^15) * (10^15)

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.

Roquen
 « Reply #12 - Posted 2013-02-15 19:28:07 »

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

Senior Newbie

Medals: 1

 « Reply #13 - Posted 2013-02-15 19:42:02 »

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%27Neil.php
Especially the third part might be an interesting read for you as it deals with problems and solutions of proper scaling.
pjt33

« JGO Spiffy Duke »

Medals: 39
Projects: 4
Exp: 7 years

 « Reply #14 - Posted 2013-02-15 21:34:36 »

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.
princec

« JGO Spiffy Duke »

Medals: 493
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

 « Reply #15 - Posted 2013-02-15 22:20:44 »

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

delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #16 - Posted 2013-02-16 10:15:50 »

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.

I have no special talents. I am only passionately curious.--Albert Einstein
ArsMagna

Junior Newbie

 « Reply #17 - Posted 2013-02-18 08:23:23 »

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.
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #18 - Posted 2013-02-18 08:57:02 »

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 have no special talents. I am only passionately curious.--Albert Einstein
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #19 - Posted 2013-02-18 11:32:09 »

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.

I have no special talents. I am only passionately curious.--Albert Einstein
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #20 - Posted 2013-02-18 13:23:01 »

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.

Actually, no

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
I hate <<'s and >>'s and &0xFFFF

Maybe you want the scales to be that big, but why would you need such an high resolution (if you are making an game)?
It would never be visible to an human player, so you could easely use steps of 1000, 1000000 or more.
This way you have an big scale without the need of *** long ints
delt0r

JGO Knight

Medals: 33
Exp: 18 years

Computers can do that?

 « Reply #21 - Posted 2013-02-18 13:45:04 »

Seriously, 10^-15m is the size of a proton, and 10^15 is about 1000 light hours (~50x bigger than the solar system). Turns out the pixels in your screen are quite  a bit bigger than that. Even adding zoom doesn't work. There is a reason why in a lot of games units are *not* to scale cus it doesn't really work. Even in games where they claim to be to scale (Supreme commander) they cheat. Look at the size of a tank compared to a aircraft carrier. Hell the range of weapons is massively reduced as well because mixing scales like this leads to boring game play or just plain no gameplay.

Space the scales are even worse. If the sun was a basket ball, the earth is pea about 100m away and the nearest star is about 30,000km away IIRC. Aka its all just tiny dots on the screen until you hit it. That is why space games cheat and make planets much larger, or orbits much smaller than true scale. So things are interesting.

I have no special talents. I am only passionately curious.--Albert Einstein
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #22 - Posted 2013-02-18 16:06:15 »

Yeah that was my point, but much better explained
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ags1 (21 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (47 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (32 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (61 views) 2015-03-10 11:36:42 ClaasJG (43 views) 2015-03-10 11:33:01
 BurntPizza 21x LiquidNitrogen 21x basil_ 19x KevinWorkman 18x EgonOlsen 17x Roquen 17x theagentd 16x Varkas 12x wessles 11x 65K 11x Riven 11x Rayvolution 9x phu004 8x princec 8x SHC 8x Ashedragon 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org