0

I have a number which is 0.010000000000000000000000000001. I want this number to be stored as it is and want to increment to it like 0.010000000000000000000000000002 0.010000000000000000000000000003 0.010000000000000000000000000004 in a loop of my limit.

The problem is a simple add method makes it 0.02 when i increment it and when i display it by making it a string. It return it to only 2 decimal places.

  • 1
    This can't be done with any built-in type; so you are in effect searching for a library, which is not allowed here. - Or you write your own, using strings. This may be hard or simple, depending on what functionality you want to use.. - Or you scale up the numbers and use `BigInteger` – TaW Jun 22 '17 at 10:14
  • Does BigRational help? More details are here: https://stackoverflow.com/questions/10359372/is-there-a-bigfloat-class-in-c – jason.kaisersmith Jun 22 '17 at 10:41
  • 2
    That's quite a strange requirement (as many as `30` meanful digits; when the highest accuracy we can find in science is `16` digits only - https://en.wikipedia.org/wiki/Atomic_clock). What is the *underlying problem*, please? – Dmitry Bychenko Jun 22 '17 at 10:47
  • To illustrate the basic problem, the closest double to the original number is 0.01000000000000000020816681711721685132943093776702880859375, which is also the closest double to 0.01. – Patricia Shanahan Jun 22 '17 at 12:08
  • These are actually keys for some files and i want to hash them all using SHA224 . The problem is i cant make my program loop these values. – Danyal Ahmed Jun 22 '17 at 12:14
  • I can store them in a string and hash them thats easy but then how will i increment them – Danyal Ahmed Jun 22 '17 at 12:15
  • Basic string processing, digit by digit..! – TaW Jun 22 '17 at 12:43
  • @Danyal: for the last digit: increment digit. If > `'9'`, there is a carry, so make it `'0'` and increment the next to last digit. Repeat the same (i.e. carry of it is > 9) for that digit, and so on, until there is no "carry" anymore. – Rudy Velthuis Jun 22 '17 at 18:42
  • @DmitryBychenko, Even if the _result_ of a calculation can have no more than N physically meaningful digits, that does not mean you only need N digits to calculate it: The result could be the output of an iterative process with millions or billions of steps. Even with very carefully thought-out algorithms, you may need to calculate with N+m digits in order to get an N-digit-accurate result. – Solomon Slow Jun 25 '17 at 22:58

0 Answers0