pow

unusual output from pow

别来无恙 提交于 2019-11-28 13:57:06
The following C code int main(){ int n=10; int t1=pow(10,2); int t2=pow(n,2); int t3=2*pow(n,2); printf("%d\n",t1); printf("%d\n",t2); printf("%d\n",t3); return (0); } gives the following output 100 99 199 I am using a devcpp compiler. It does not make any sense, right? Any ideas? (That pow(10,2) is maybe something like 99.9999 does not explain the first output. Moreover, I got the same output even if I include math.h) You are using a poor-quality math library. A good math library returns exact results for values that are exactly representable. Generally, math library routines must be

Using Recursion to raise a base to its exponent - C++

a 夏天 提交于 2019-11-28 11:55:20
问题 I simply want to write some code that makes use of recursion of functions to raise a base to its power. I know that recursion is not the most right way to do things in C++, but I just want to explore the concept a bit. The program asks the user for a base and an exponent and then console outs the answer. Here's the program I've written: #include <iostream> #include <math.h> using namespace std; int raisingTo(int, int); int main() { int base, exponent; cout << "Enter base value: "; cin >> base

Ambiguous call to overloaded function 'pow'

ⅰ亾dé卋堺 提交于 2019-11-28 08:11:54
问题 I'm having some problems runnning the following code. I got this: error C2668: 'pow' : ambiguous call to overloaded function. I've tried to manually cast the arguments to the appropiate type using static_cast, however I think I get some pointer errors?! The program should convert a number from base 16 to base 10. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <math.h> //base 16 to base 10 int convert(char *n){ int result

Math.Pow vs multiply operator (performance)

倖福魔咒の 提交于 2019-11-28 06:17:54
Anyone knows if multiply operator is faster than using the Math.Pow method? Like: n * n * n vs Math.Pow ( n, 3 ) Basically, you should benchmark to see. Educated Guesswork (unreliable): In case it's not optimized to the same thing by some compiler... It's very likely that x * x * x is faster than Math.Pow(x, 3) as Math.Pow has to deal with the problem in its general case, dealing with fractional powers and other issues, while x * x * x would just take a couple multiply instructions, so it's very likely to be faster. ggf31416 I just reinstalled windows so visual studio is not installed and the

Find Cube root of a number Using System.Math.Pow() method in C#

巧了我就是萌 提交于 2019-11-28 00:45:59
问题 While writing a program I came across finding the cube root of a number in one of my functions. when I used the below code, I was getting an incorrect value for the cube root ( 1 was getting printed for n = 64 ). public static void cubicPairs(double n) { double root = (System.Math.Pow(n, (1/3))); Console.WriteLine(root); } Now after I changed the code slightly to this, public static void cubicPairs(double n) { double root = (System.Math.Pow(n, (1.0/3.0))); //Changed how second parameter is

pow for SSE types

北城余情 提交于 2019-11-28 00:28:58
问题 I do some explicitly vectorised computations using SSE types, such as __m128 (defined in xmmintrin.h etc), but now I need to raise all elements of the vector to some (same) power, i.e. ideally I would want something like __m128 _mm_pow_ps(__m128, float) , which unfortunately doesn't exist. What is the best way around this? I could store the vector, call std::pow on each element, and then reload it. Is this the best I can do? How do compilers implement a call to std::pow when auto-vectorising

Does pow() work for int data type in C? [duplicate]

泄露秘密 提交于 2019-11-27 22:59:05
This question already has an answer here: Strange behaviour of the pow function 5 answers I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5. My code is: #include <stdio.h> #include <math.h> int main(void) { int a,b; printf("Enter the number."); scanf("\n%d",&a); b=pow(a,2); printf("\n%d",b); } The output is something like this: "Enter the number. 2 4 "Enter the number. 5 24 "Enter the number. 4 16 "Enter the number. 10 99 Can't we use pow() function for int data type??

pow() cast to integer, unexpected result

戏子无情 提交于 2019-11-27 16:06:07
I have some problems using an integer cast for the pow() function in the C programming language. The compiler I'm using is the Tiny C Compiler (tcc version 0.9.24) for the Windows platform. When executing the following code, it outputs the unexpected result 100, 99 : #include <stdio.h> #include <math.h> int main(void) { printf("%d, ", (int) pow(10, 2)); printf("%d", (int) pow(10, 2)); return 0; } However, at this online compiler the output is as expected: 100, 100 . I don't know what is causing this behavior. Any thoughts? Programming error from me, compiler bug? You found a bug in tcc. Thanks

Why is pow(int, int) so slow?

别说谁变了你拦得住时间么 提交于 2019-11-27 11:04:31
问题 I've been working on a few project Euler exercises to improve my knowledge of C++. I've written the following function: int a = 0,b = 0,c = 0; for (a = 1; a <= SUMTOTAL; a++) { for (b = a+1; b <= SUMTOTAL-a; b++) { c = SUMTOTAL-(a+b); if (c == sqrt(pow(a,2)+pow(b,2)) && b < c) { std::cout << "a: " << a << " b: " << b << " c: "<< c << std::endl; std::cout << a * b * c << std::endl; } } } This computes in 17 milliseconds. However, if I change the line if (c == sqrt(pow(a,2)+pow(b,2)) && b < c)

Wrong result by Java Math.pow

≯℡__Kan透↙ 提交于 2019-11-27 09:40:53
If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't expect the least significant digit(s) of your result to actually be meaningful/precise. If you need arbitrarily