Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Class for extremely big, or small, numbers  (Read 5339 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Class for extremely big, or small, numbers
« on: February 06, 2014, 09:52:36 pm »
Is there a class that allows for any number of digits before and after the decimal point for C++? Something that will allow me to have numbers that are bigger or smaller than what I can get with the standard data types.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Class for extremely big, or small, numbers
« Reply #1 on: February 06, 2014, 09:59:14 pm »
Have you already searched for "big integer c++" or "big decimal c++"?
GMP is a known library.

Out of curiosity, what do you need it for?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Class for extremely big, or small, numbers
« Reply #2 on: February 06, 2014, 09:59:26 pm »
Strings ? you can use them for arbitrarily big numbers (at least while you have memory..), you just need to implement the basic math operations on them and it should work.

It shouldn't be hard to make it work, but it will most likely have bad performance..

If you want something serious, I heard https://gmplib.org/ works fine.

AN71

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Class for extremely big, or small, numbers
« Reply #3 on: February 06, 2014, 10:02:14 pm »
Forgive me for asking, but out of curiosity, what could you possibly need more than the 19.26 decimal digits that long double gives you? Also, I don't think there is any way to store any number larger without losing precision.

edit: I guess you can ignore this post, other higher beings are more knowledgeable :)
« Last Edit: February 06, 2014, 10:08:37 pm by Aeroslizer »
Quote from: Friedrich Nietzsche
He who would learn to fly one day must first learn to stand and walk and run and climb and dance; one cannot fly into flying.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Class for extremely big, or small, numbers
« Reply #4 on: February 06, 2014, 10:27:20 pm »
Have you already searched for "big integer c++" or "big decimal c++"?
GMP is a known library.

Out of curiosity, what do you need it for?

I have searched for something similar and I found gmp. I was just trying to find more options.

I'm working on a particle accretion simulator for fun and working with really small numbers. I'm not getting enough precision with the numbers. 

Forgive me for asking, but out of curiosity, what could you possibly need more than the 19.26 decimal digits that long double gives you? Also, I don't think there is any way to store any number larger without losing precision.

edit: I guess you can ignore this post, other higher beings are more knowledgeable :)

I am using long double, but it is rounding numbers off. I calculate something with 13 decimals, and it is rounding off at 4 in some cases. I'm also getting read outs out equal numbers not being equal.

One such example is the gravitational constant,  0.0000000000667384. Already using up 11 decimals (I've had to truncate it to prevent getting 0 for results from rounding).

When calculating gravitational forces, you can easily get over the 19 decimals.

Fg = G * ( M1 + M2) / R ^ 2

If M1 + M2 = 1 and R ^ 2 (the squared distance between 1 and 2) is 2822400 (1680 * 1680, one side of my screen to the other).

Fg = G * 1 / 2,822,400 = 0.000000000000000023645975 ( 24 decimals )


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Class for extremely big, or small, numbers
« Reply #5 on: February 06, 2014, 10:57:16 pm »
Quote
0.000000000000000023645975 ( 24 decimals )
I think you misunderstand the concept of floating point; it's about significant digits, not zeros. See also this thread.

Most problems related to precision can be solved by using algorithms which are numerically stable, not by larger data types. The prime example here is the inversion of matrices. Before employing external libraries, make sure there's really no way to minimize rounding errors (it might require some reading about numerical methods).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: Class for extremely big, or small, numbers
« Reply #6 on: February 07, 2014, 08:13:59 am »
If all your numbers are that small, there's no need to add so many zeros. You can simple move the decimal point down and if you ever need to output something add a few zeros or use the scientific notation.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Class for extremely big, or small, numbers
« Reply #7 on: February 07, 2014, 02:54:41 pm »
Well, they are small now, but I plan to allow the particle to gather up and squeeze a group of smaller particles into bigger particles. Eventually there will be really big particles that will be making numbers that are much bigger.

AbelToy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • AbelToy.com
    • Email
Re: Class for extremely big, or small, numbers
« Reply #8 on: February 09, 2014, 12:30:07 pm »
Well, they are small now, but I plan to allow the particle to gather up and squeeze a group of smaller particles into bigger particles. Eventually there will be really big particles that will be making numbers that are much bigger.

And you still would be able to have bigger numbers without using larger data types.