pow

finding cube root in C++?

心已入冬 提交于 2019-11-30 12:57:01
Strange things happen when i try to find the cube root of a number. The following code returns me undefined. In cmd : -1.#IND cout<<pow(( double )(20.0*(-3.2) + 30.0),( double )1/3) While this one works perfectly fine. In cmd : 4.93242414866094 cout<<pow(( double )(20.0*4.5 + 30.0),( double )1/3) From mathematical way it must work since we can have the cube root from a negative number. Pow is from Visual C++ 2010 math.h library. Any ideas? pow(x, y) from <cmath> does NOT work if x is negative and y is non-integral. This is a limitation of std::pow , as documented in the C standard and on

Power function using recursion

倖福魔咒の 提交于 2019-11-30 07:14:55
I have to write a power method in Java. It receives two ints and it doesn't matter if they are positive or negative numbers. It should have complexity of O(logN) . It also must use recursion. My current code gets two numbers but the result I keep outputting is zero, and I can't figure out why. import java.util.Scanner; public class Powers { public static void main(String[] args) { float a; float n; float res; Scanner in = new Scanner(System.in); System.out.print("Enter int a "); a = in.nextFloat(); System.out.print("Enter int n "); n = in.nextFloat(); res = powers.pow(a, n); System.out.print

How is pow() calculated in C?

孤街醉人 提交于 2019-11-30 04:08:38
问题 Our professor said that you can't calculate a b if a<0 using pow() because pow() uses natural logarithms to calculate it (a b =e b ln a ) and since it's undefined for negative numbers it can't be calculated. I tried it and it works as long as b is an integer. I have searched through math.h and further files, but was unable to find how the function is defined and what it uses to calculate. I also tried searching the internet, but without any success. There are similar questions on Stack

How to get the Power of some Integer in Swift language?

时间秒杀一切 提交于 2019-11-29 20:32:16
I'm learning swift recently, but I have a basic problem that can't find an answer I want to get something like var a:Int = 3 var b:Int = 3 println( pow(a,b) ) // 27 but the pow function can work with double number only, it doesn't work with integer, and I can't even cast the int to double by something like Double(a) or a.double()... Why it doesn't supply the power of integer? it will definitely return an integer without ambiguity ! and Why I can't cast a integer to a double? it just change 3 to 3.0 (or 3.00000... whatever) if I got two integer and I want to do the power operation, how can I do

calculate mod using pow function python

瘦欲@ 提交于 2019-11-29 19:08:35
问题 So, If i would like to calculate the value of 6^8 mod 5 using the pow function, what should I put in a line?? In the assumption that You don't need to import it first I know that pow is used like pow (x, y) = pow (6, 8) = 6^8 and My guess is mod.pow(6,8) Thank you! 回答1: It's simple: pow takes an optional 3rd argument for the modulus. From the docs: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z) . The

Ambiguous call to overloaded function 'pow'

血红的双手。 提交于 2019-11-29 13:53:42
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 = 0; for (int i = strlen(n) - 1; i >= 0; i--){ if (n[i] >= 'a') result += (n[i] - 'a' + 10)* pow(16,

GCC C++ pow accuracy

北城余情 提交于 2019-11-29 13:37:29
So i was in a computing contest and i noticed a weird bug. pow(26,2) would always return 675, and sometimes 674? even though correct answer is 676. These sort of errors also occur with pow(26,3), pow(26,4) etc After some debugging after the contest i believe the answer has to do with the fact int rounds down. Interestingly this kind of error has never occured to me before. The computer i had was running mingw on windows 8. GCC version was fairly new, like 2-3 months old i believe. But what i found was that if i turned the o1/o2/o3 optimization flag on these sort of error would miraculously

C# Math.Pow() is broken

会有一股神秘感。 提交于 2019-11-29 10:53:09
And no, this does not (to my understanding) involve integer division or floating-point rounding issues. My exact code is: static void Main(string[] args) { double power = (double)1.0 / (double)7.0; double expBase = -128.0; System.Console.WriteLine("sanity check: expected: -128 ^ 0.142857142857143 = -2. actual: " + expBase + " ^ " + power + " = " + Math.Pow(expBase, power)); System.Console.ReadLine(); } The output is: sanity check: expected: -128 ^ 0.142857142857143 = -2. actual: -128 ^ 0.14285 7142857143 = NaN The Target Framework for this code is (according to solution properties) .NET

pow for SSE types

走远了吗. 提交于 2019-11-29 07:11:18
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 code that otherwise is well vectorisable? Are there any libraries that provide something useful? (note

Does Java have an exponential operator?

本小妞迷上赌 提交于 2019-11-28 20:46:34
问题 Is there an exponential operator in Java? For example, if a user is prompted to enter two numbers and they enter 3 and 2 , the correct answer would be 9 . import java.util.Scanner; public class Exponentiation { public static double powerOf (double p) { double pCubed; pCubed = p*p; return (pCubed); } public static void main (String [] args) { Scanner in = new Scanner (System.in); double num = 2.0; double cube; System.out.print ("Please put two numbers: "); num = in.nextInt(); cube = powerOf