pow

pow() function in C problems [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-02 09:32:10
This question already has an answer here: Strange behaviour of the pow function 5 answers I am having some problems with pow() function in C. When ever run this code, 153 as input, the sum evaluates to 152 . However if I dont use pow() function and instead use a for loop to get the value of N n , the sum evaluates to 153 . Can anyone help please explain me this difference? #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> int main(void) { unsigned int i, n, sum = 0, N, a = 1, j; char num[100], x[2] = { 0 }; printf("Determining an armstrong number\n\n" "Enter a number

Conversion from double to integer [duplicate]

北慕城南 提交于 2019-12-02 07:45:26
This question already has an answer here: C++: How to round a double to an int? [duplicate] 5 answers round() for float in C++ 21 answers I am stuck in problem where the double number is not getting properly converted to integer. In this case-> int x=1000; double cuberoot=pow(x,(1/(double)3)); int a=cuberoot; cout<<"cuberoot="<<cuberoot<<endl; cout<<"a="<<a<<endl; Output: cuberoot=10 a=9 Why here a=9 and not 10? Any solution to this problem?? Also I don't want to round the value..if a=3.67 then it should be converted to 3 only and not 4. Because the cuberoot is very close to 10 but not quite

Why am I getting unexpected output when using floor with pow?

為{幸葍}努か 提交于 2019-12-02 07:27:06
So, I ran this code on my code blocks: #include<iostream> #include<cmath> using namespace std; int main() { int a; a=pow(10,9); cout<<a<<endl; a=ceil(pow(10,9)); cout<<a<<endl; a=floor(pow(10,9)); cout<<a<<endl; return 0; } I got the output as: 999999999 100000000 100000000 1st output was not 10^9 due to truncation effect,which means that pow(10,9) was something like 999999999.99999.., but then how come floor of this thing is 1000000000 ?? Actually, the maximum value for int is 2,147,483,647, therefore there should be no overflow or truncation (it's an int ). And my output is exactly:

Why am I getting unexpected output when using floor with pow?

元气小坏坏 提交于 2019-12-02 07:20:34
问题 So, I ran this code on my code blocks: #include<iostream> #include<cmath> using namespace std; int main() { int a; a=pow(10,9); cout<<a<<endl; a=ceil(pow(10,9)); cout<<a<<endl; a=floor(pow(10,9)); cout<<a<<endl; return 0; } I got the output as: 999999999 100000000 100000000 1st output was not 10^9 due to truncation effect,which means that pow(10,9) was something like 999999999.99999.., but then how come floor of this thing is 1000000000 ?? 回答1: Actually, the maximum value for int is 2,147,483

How does the function pow work?

北慕城南 提交于 2019-12-02 05:30:50
问题 After compiling the following program I get the output "2346" but was expecting "2345". #include<math.h> #include<iostream.h> int nr_cif(int a) { int k=0; while(a!=0) { a = a/10; k++; } return k; } void Nr(int &a){ int k = nr_cif(a); a = a % (int)pow(10,k-1); } int main() { int a = 12345; Nr(a); cout<<a; } After debugging I noticed that it bugs out after it evaluates: a = a % (int)pow(10,k-1). Why does it break here? 回答1: It's not a very good idea to use pow for integer math. I would change

Power function returns 1 less result

喜你入骨 提交于 2019-12-02 04:52:28
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); } 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 slightly larger, then the conversion will round it down to the expected value. 2) if the result is slightly

Strange Result for Math.pow() in JavaScript across different Browsers

落爺英雄遲暮 提交于 2019-12-02 04:07:38
问题 Maybe this only happens in chrome latest version. Some weird behavior for the negative exponent value only in the Chrome browser . I already checked with different browsers & find it really weird. As FireFox & Chromium will show the exact same result while Chrome latest version will show the different result for some of the examples. And I don't know what's going on? Here are my findings for different browsers... FireFox Chromium Chrome Weird Thing!! For the Math.pow(10,-4) and Math.pow(10,-5

How does the function pow work?

蓝咒 提交于 2019-12-02 02:16:13
After compiling the following program I get the output "2346" but was expecting "2345". #include<math.h> #include<iostream.h> int nr_cif(int a) { int k=0; while(a!=0) { a = a/10; k++; } return k; } void Nr(int &a){ int k = nr_cif(a); a = a % (int)pow(10,k-1); } int main() { int a = 12345; Nr(a); cout<<a; } After debugging I noticed that it bugs out after it evaluates: a = a % (int)pow(10,k-1). Why does it break here? It's not a very good idea to use pow for integer math. I would change the code as follows: void Nr(int &a) { int ten_k = 1; while (ten_k < a) ten_k *= 10; a %= ten_k/10; // 10-to

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

假装没事ソ 提交于 2019-12-02 02:09:01
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. public double pow(double num1, double num2) { double result = 1; for (int i = 0; i < num2; i++) result *= num1; return result; } When developing applications for mobile devices using Java, you may require mathematical methods not available on your particular Java VM. You can use

Raising number to fractional power java [duplicate]

心不动则不痛 提交于 2019-12-01 17:34:11
问题 This question already has answers here : Division of integers in Java [duplicate] (7 answers) Closed 4 years ago . I used the following code: double pow = 3/7; double num = 85; System.out.println(Math.pow(num, pow)); Expected result: 6.71... The output is 1.0 Any idea why? 回答1: 3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0 . Change it to 3.0/7 in order to get a floating point result. 来源: https://stackoverflow.com