0

I am doing a Project using c , and I encountered a Problem while doing some calculations.. actually I want to store a big number (2^52) in any data type

int helper = TwoOf(26);//52 over 2

unsigned long long help = helper * helper;
  • function Twoof(x) -> calculates x^2

I used long long to store 2^52 but the debugger shows it has a zero value.

can anyone help?

  • `unsigned long long` is not enough to store result of `2^52`. Instead use _int array of required size_ – Achal Apr 11 '18 at 05:39
  • 1
    Welcome to Stack Overflow. Could you provide a [mcve] ? This will probably help your question being well received and other person to help you. – Pac0 Apr 11 '18 at 05:39
  • `helper` is `int` so `helper * helper` is too. You assign it to an `unsigned long long` but this is too late. You need to start your calculations with long long. – n. m. could be an AI Apr 11 '18 at 05:44
  • 1
    @achal Please elaborate in view of the likely size of `unsigned long long` of 8 bytes, with 64 bits. Consider using extended space and formatting features of making an answer. – Yunnosch Apr 11 '18 at 05:44
  • 1
    Please explain the meaning of your "(2^52)" (which is usually interpreted as 2 to the power of 52 by most readers) in relation to "52 over 2", which might refer to a very different mathmatical construct/expression. E.g. "4 over 2" equals 6 (I hope, being a little rusty there....), while "2 to the power of 4" equals 16. http://mathworld.wolfram.com/BinomialCoefficient.html – Yunnosch Apr 11 '18 at 05:47
  • 1
    @Yunnosch it is `52/2`... `2^(52/2)` is the positive square root of `2^52`... – Antti Haapala -- Слава Україні Apr 11 '18 at 05:57
  • You just need to type your `helper` as `unsigned long long` or, *alternatively*, cast one operand to `unsigned long long` in the multiplication: `(unsigned long long)helper * helper`. – Antti Haapala -- Слава Україні Apr 11 '18 at 06:04

1 Answers1

1

If you do not require single digit accuracy and are happy with a number of significant figures, use a double

doron
  • 27,972
  • 12
  • 65
  • 103
  • Funnily enough, an IEEE754 `double` can store 2^52 exactly. (It starts to go awry after the 53rd power.) Occasionally this is a good idea especially if you compiler doesn't have a `long long` type and fast floating point; +1. – Bathsheba Apr 11 '18 at 07:13