0

how to calculate pow and round off with BigInteger data type

below is code but when we go with quick watch it's showing {0}.my concern is how to get the value and how to round off this " BigInteger_getpow" value

double a=1;
double b=2;
int k=50;

BigInteger BigInteger_x = (BigInteger)(a/b);
BigInteger BigInteger_getpow = BigInteger.Pow(BigInteger_x , k);
user3310138
  • 137
  • 5
  • 20
  • For your example, BigInteger_getpow is equal to 0.5^50 is this correct? and what do you want to do with this result exactly or what isn't working? Please explain a little more in detail... – ReeCube Mar 10 '14 at 11:28
  • yes.you are correct.i have some scenario where i need to do calculation – user3310138 Mar 10 '14 at 11:40
  • But your code isn't working like this or do you want to add some code? – ReeCube Mar 10 '14 at 11:41
  • could you please suggest what is the better approach for this – user3310138 Mar 10 '14 at 11:45
  • The reason why you get `0` is that `1/2` is not an integer number. So `BigInteger_x` is `0`. You can just calculate (1/2)^k=1/(2^k). But I doubt that this will achieve satisfying results. You could try to divide 1 by 2 for 50 times. This way you won't have to worry about conversion to `double`. I haven't checked if 1/2^50 is representable as a double. You should do that before you start. – Nico Schertler Mar 10 '14 at 11:47
  • in case we get exponent value.but i don't want exponent value that's why i am using Biginteger – user3310138 Mar 10 '14 at 11:52
  • What do you mean? Why is a `double` not sufficient? You won't loose precision when dividing by 2. – Nico Schertler Mar 10 '14 at 12:28
  • @user3310138: To reiterate the point of Nico Schertler, the type BigInteger holds, as the name tells, only integer numbers. 0.5 or powers thereof are not integers, and the closest integer is 0. (0.5 gets rounded to 0 in many if not most rounding modes.) You may try to use the type BigDecimal, if that existed in C#. See for instance http://stackoverflow.com/questions/2863388/what-is-the-equivalent-of-the-java-bigdecimal-class-in-c – Lutz Lehmann Mar 10 '14 at 13:20
  • Perhaps one should formulate a more realistic problem: Using a fictive class BigFloat with constructor `BigFloat(double value, long precision)`, compute Fibonacci numbers using the power method. `BigFloat w5=BigFloat.sqrt( BigFloat(5,1000) ); BigFloat x=(1+w5)/2; long int n=65000; BigInteger Fn = BigInteger( BigFloat.Floor( BigFloat.pow(x,n) / w5 ).toString() );` – Lutz Lehmann Mar 10 '14 at 13:32
  • BigFloat is not so fictive after all. There have been several uses of that class name, one here: http://www.extremeoptimization.com/Documentation/Reference/Extreme.Mathematics.BigFloat.aspx or http://stackoverflow.com/a/14608045/3088138 – Lutz Lehmann Mar 10 '14 at 13:38

0 Answers0