pow

pow() returning wrong result under specific conditions (and an unexpected fix) - Why is all this?

主宰稳场 提交于 2019-12-23 13:00:40
问题 I have been creating a short program for implementing Sieve of Eratosthenes. In he program, I have used int startingPoint which held the value of the current prime number, which had it's multiplications being marked non-prime, starting from its square. I used the pow() function to calculate the square of the starting point in the current loop. While doing so, I came across a weird phenomenon. When startingPoint was equal to 5 , then after int i = pow(startingPoint,2) the value of i was 24,

How wrong do I use Math.Pow(a,b) function in this C# code?

吃可爱长大的小学妹 提交于 2019-12-23 03:50:42
问题 I can not find anything wrong with the following code, whence the MSVC# compiler stores NAN in "c": double c = Math.Pow(-8d, 1d / 3d); While I think this line should calculate -2 for "c", the compiler stores NAN in "c"? Am i wrong about anything? 回答1: The power function for floating point numbers is only defined for positive base or integral exponent. Try double c = - Math.Pow(8d, 1d / 3d); Actually, 1/3 can't be represented exactly as a floating point number, but needs to be rounded. An

Power function in php to calculate 17^2147482999

浪尽此生 提交于 2019-12-22 09:40:03
问题 I am trying to make a power function to calculate the power of 17^2147482999. I tried this code: function ipow($a, $b) { if ($b<0) { echo "B must be a positive integer"; } if ($b==0) return 1; if ($a==0) return 0; if ($b%2==0) { return ipow($a*$a, $b/2); } else if ($b%2==1) { return $a*ipow($a*$a,$b/2); } return 0; } The function call: echo ipow($a, $b); The error: Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\spoj\LASTDIG.php on line 23 Is there any

pow function in C

China☆狼群 提交于 2019-12-22 04:06:57
问题 I write a C code that have power function that is from math.h library. when I compiled my program, I received an error which is " undefined reference to 'pow' function ", I compile my program using gcc compiler (fedora 9). I insert -lm flag to gcc then, the error is omitted but the output of the pow function is 0. #include<math.h> main() { double a = 4, b = 2; b = pow(b,a); } Can anyone help me? Is there is a problem in my compiler?? Thanks. 回答1: Your program doesn't output anything. The 0

How to get the power of a number in J2ME [duplicate]

只愿长相守 提交于 2019-12-20 03:42:37
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: J2ME power(double, double) math function implementation I'm developing a simple j2me application. There I need to get the power of a number as like as in the pow(double num1, double num2) in java. But as I got to know, j2me doesn't support to this pow() method. Any helpful option is appreciated. 回答1: public double pow(double num1, double num2) { double result = 1; for (int i = 0; i < num2; i++) result *= num1;

C's pow function refuses to work with variable exponent

橙三吉。 提交于 2019-12-19 16:16:18
问题 Let's say I have the following code snippet: int i; double value; for(i = 0; i < CONSTANT; i++) { value = (double)pow(2, i); } Trying to compile this code yields an "undefined reference to `pow'" error. Including or excluding math.h makes no difference, since it ends up being included anyway. Raising 2.0 to a hardcoded power works okay, but everything fails if I substitute the exponent by an expression that contains i . What am I doing wrong? Thanks. 回答1: It's a very interesting behavior, and

C's pow function refuses to work with variable exponent

 ̄綄美尐妖づ 提交于 2019-12-19 16:16:13
问题 Let's say I have the following code snippet: int i; double value; for(i = 0; i < CONSTANT; i++) { value = (double)pow(2, i); } Trying to compile this code yields an "undefined reference to `pow'" error. Including or excluding math.h makes no difference, since it ends up being included anyway. Raising 2.0 to a hardcoded power works okay, but everything fails if I substitute the exponent by an expression that contains i . What am I doing wrong? Thanks. 回答1: It's a very interesting behavior, and

Power function returns 1 less result

天大地大妈咪最大 提交于 2019-12-19 11:35:27
问题 Whenever I input a number in this program the program return a value which is 1 less than the actual result ... What is the problem here?? #include<stdio.h> #include<math.h> int main(void) { int a,b,c,n; scanf("%d",&n); c=pow((5),(n)); printf("%d",c); } 回答1: pow() returns a double , the implicit conversion from double to int is "rounding towards zero". So it depends on the behavior of the pow() function. If it's perfect then no problem, the conversion is exact. If not: 1) the result is

Power function returns 1 less result

ぐ巨炮叔叔 提交于 2019-12-19 11:34:54
问题 Whenever I input a number in this program the program return a value which is 1 less than the actual result ... What is the problem here?? #include<stdio.h> #include<math.h> int main(void) { int a,b,c,n; scanf("%d",&n); c=pow((5),(n)); printf("%d",c); } 回答1: pow() returns a double , the implicit conversion from double to int is "rounding towards zero". So it depends on the behavior of the pow() function. If it's perfect then no problem, the conversion is exact. If not: 1) the result is

Why a+=b*pow(10,c-i-1) == 99 c++? [duplicate]

南笙酒味 提交于 2019-12-19 04:46:32
问题 This question already has answers here : Why pow(10,5) = 9,999 in C++ (8 answers) Closed 12 months ago . I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100? #include <iostream> #include<math.h> using namespace std; int main () { int skt = 0; int sk[3]; int nsk = 3; sk[0]=1; sk[1]=2; sk[2]=8; for (int i=0; i<nsk; i++) { skt = skt + (sk[i]*pow(10.0,nsk-i-1)); cout <<" "<<skt<<endl; } } the result of this code 99 119 127 ,but if I use library cmath it