Sign in to follow this  
Supernovae

Astronomical numbers problem

Recommended Posts

Supernovae    111
Greetings

I have a problem with numbers being too gigantical (large).

When i try to run my program, the compiler starts to complain " warning: integer constant is too large for its type. ".

The number i need to define is: 2688920000000000000000000000000000000

I had it first in "float", but after this i decided to try "double". Neither work, my program wount work if i cannot use these numbers.

So could someone let me know what to do with this.

I have it like this:

/*

float Cloud_Mass;
Cloud_Mass = 2688920000000000000000000000000000000;

*/

I use c++


Thanks for reading.

Share this post


Link to post
Share on other sites
haegarr    7372
Use [url="http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic"]Arbitrary-precision Arithmetic[/url] (some libraries are listed in the linked Wikipedia article).

Share this post


Link to post
Share on other sites
Phobon    247
Or, if this is possible, just use better units. (e.g. solar masses instead of kilograms etc).
I've worked a little in computational astronomy and have yet to see a problem where arbitrary-precision arithmetics were really neccesary (given the required precision)
So if you give us some more details about your problem, maybe we can help you better.

Share this post


Link to post
Share on other sites
Supernovae    111
[quote name='Phobon' timestamp='1307457860' post='4820532']
Or, if this is possible, just use better units. (e.g. solar masses instead of kilograms etc).
I've worked a little in computational astronomy and have yet to see a problem where arbitrary-precision arithmetics were really neccesary (given the required precision)
So if you give us some more details about your problem, maybe we can help you better.
[/quote]

The scope of the project demands precission, so this is the only way. I got it to work with BigInteger library, so the problem is now solve.

Now i can make solar mass unit this way:

string S_Solar_Mass("1988920000000000000000000000000");
BigInteger Solar_Mass = stringToBigInteger(S_Solar_Mass);

Thanks alot for help people.

Share this post


Link to post
Share on other sites
Supernovae    111
[quote name='Waterlimon' timestamp='1307467347' post='4820592']
try 10472057190.0 or try adding some suffix...
[/quote]

Good point, it does not work with decimals. Hmm... gotta try something else.

Share this post


Link to post
Share on other sites
iMalc    2466
[quote name='Supernovae' timestamp='1307464655' post='4820572']
The scope of the project demands precission, so this is the only way.[/quote]It's a little hard to believe that you need more precision that a double, since 1988920000000000000000000000000 is already rounded to 6 significant figures (out of 31) which is less than a double.

Share this post


Link to post
Share on other sites
Storyyeller    215
Yeah, there's no reason to use big ints here. You need to create double literals by adding a .0 at the end.

For example, try this.

float Cloud_Mass;
Cloud_Mass = 2688920000000000000000000000000000000.0f;

Share this post


Link to post
Share on other sites
If you are writing a very large integer literal, make sure you put a L (for 'long long', i.e. a 64bit number) at the end of your number, like this:
[b]345234234234234432423[color="#ff0000"]L[/color][/b]

If it's unsigned, use UL. (for unsigned long long, also a 64bit number).
[b]435435345345345345345435[color="#ff0000"]UL[/color][/b]

If it's larger than 64 bits (larger than [b]18,446,744,073,709,551,616[/b]), you can't make it a literal. Seeing that the number is so large, you can't possibly hold it in any native C++ variable types, therefore you must already be using some sort of BigNum library, so see whether they have a macro or something as part of that library, for literals.

Share this post


Link to post
Share on other sites
Storyyeller    215
Why was my post rated down? It answers the original question in the simplest possible manner.

The OP mentioned trying to use floats but failed due to not knowing how to declare floating point constants. All you need to do is add the appropriate suffix. All this talk about bigints is unnecessary and distracting. Unless you really need more than 53 bits of precision, but even then you need an arbitrary precision floating point type, not an integer.


Incidentally, the closest value which can be represented by a double is exactly 2688919999999999979070951376811982848.0
If for some reason you want to represent the original value exactly, you'll need a floating point type with at least 88 bits of precision.

Share this post


Link to post
Share on other sites
Supernovae    111
FINALLY IT WORKS !!!!!

I decided to go for BigInteger library. Tho, still it's bit tricky, since if i want huge number i need to use several variables then * them and get my end result variable. I tested it many many times even with functions etc.. works flawlessly. I even multiplied earth mass in grams, with sun mass in grams, lol. Works nicely. I just wanted to thank everyone helping. Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this