exponentiation

JavaScript exponentiation unary operator design decision

一笑奈何 提交于 2019-12-31 00:40:44
问题 So I was fooling around with the new exponentiation operator and I discovered you cannot put a unary operator immediately before the base number. let result = -2 ** 2; // syntax error let result = -(2 ** 2); // -4 let x = 3; let result = --x ** 2; // 4 From the documentation on MDN: In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator ( + / - / ~ / ! / delete / void / typeof ) immediately before the base number. In most

Rule to calculate power of a number when the exponent is Negative in Prolog?

我们两清 提交于 2019-12-30 12:36:24
问题 I have a power function pow that attempts to calculate the value of B to the power of E . So far I handle the cases- 1. exponent is 0 2. exponent is non-zero pow(B,0,1). pow(B,E,Result):- E2 is E - 1, pow(B,E2,Result2), Result is B*Result2. How can I add another case where the power function can handle negative exponents? 回答1: First, one should consider how to define 0 0 . Formally speaking it is indeterminate . It could be zero or it could be 1. As Wolfram's Mathworld says in its article on

Modular Exponentiation

北战南征 提交于 2019-12-29 09:08:37
问题 In C/C++ how can I calculate (a^b)%m where b does not fit into 64 bits? In other words, is there a way of calculating the above value using b%m instead of b ? And is there any algorithm that can compute the above result in O(log(b)) time or O(log(b%m)) time? 回答1: According to Euler's theorem, if a and m are coprime: a b mod m = a b mod phi(m) mod m so if b is large, you can use the value b % phi(m) instead of b . phi(m) is Euler's totient function, which can be easily calculated if you know

Exponentiation with negative base

老子叫甜甜 提交于 2019-12-29 07:52:20
问题 So, the R expression and its output is as follows: > (4-7)^1.3 [1] NaN Any ideas how to solve this in R? 回答1: The answer is a complex number, so you need to give it a complex argument: > (4-7+0i)^1.3 [1] -2.451751-3.374545i but remember this is only one root... 回答2: I quote from Wikipedia, especially the bold text (http://en.wikipedia.org/wiki/Exponentiation): The IEEE 754-2008 floating point standard is used in the design of most > floating point libraries. It recommends a number of

Parse error: syntax error, unexpected '*' [duplicate]

若如初见. 提交于 2019-12-25 19:06:44
问题 This question already has answers here : PHP parse/syntax errors; and how to solve them (17 answers) Closed 4 years ago . My code: <?php function ci($principle, $rate, $time) { $ci = ($principle * (( (1 + $rate / 100) ** $time) - 1)); echo $ci; } ?> <?php echo ci(10,10,10); ?> And when I am running it, it gives the following error Parse error: syntax error, unexpected '*' in D:\Xampp\htdocs\php\functions.php on line 4 Please tell me what's the error in line 4 ( $ci = ($principle * (((1+$rate

Hat ^ operator vs Math.Pow()

你说的曾经没有我的故事 提交于 2019-12-23 17:12:51
问题 Having perused the MSDN documentation for both the ^ (hat) operator and the Math.Pow() function, I see no explicit difference. Is there one? There obviously is the difference that one is a function while the other is considered an operator, e.g. this will not work: Public Const x As Double = 3 Public Const y As Double = Math.Pow(2, x) ' Fails because of const-ness But this will: Public Const x As Double = 3 Public Const y As Double = 2^x But is there a difference in how they produce the end

Fast exponentiation when only first k digits are required?

橙三吉。 提交于 2019-12-20 09:44:52
问题 This is actually for a programming contest, but I've tried really hard and haven't got even the faintest clue how to do this. Find the first and last k digits of n m where n and m can be very large ~ 10^9. For the last k digits I implemented modular exponentiation. For the first k I thought of using the binomial theorem upto certain powers but that involves quite a lot of computation for factorials and I'm not sure how to find an optimal point at which n^m can be expanded as (x+y) m . So is

Vectorized exponentiation

徘徊边缘 提交于 2019-12-20 04:47:39
问题 I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = x n for each x in X and n in N . For example, the following input: X = [2 3 4]' N = [1 2 3] should produce: ans = [2 4 8; 3 9 27; 4 16 64] Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)? 回答1: Use bsxfun: bsxfun(@power, X, N) This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the

Vectorized exponentiation

廉价感情. 提交于 2019-12-20 04:47:01
问题 I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = x n for each x in X and n in N . For example, the following input: X = [2 3 4]' N = [1 2 3] should produce: ans = [2 4 8; 3 9 27; 4 16 64] Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)? 回答1: Use bsxfun: bsxfun(@power, X, N) This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the

Exponentiation in Haskell

本小妞迷上赌 提交于 2019-12-17 21:42:59
问题 Can someone tell me why the Haskell Prelude defines two separate functions for exponentiation (i.e. ^ and ** )? I thought the type system was supposed to eliminate this kind of duplication. Prelude> 2^2 4 Prelude> 4**0.5 2.0 回答1: There are actually three exponentiation operators: (^) , (^^) and (**) . ^ is non-negative integral exponentiation, ^^ is integer exponentiation, and ** is floating-point exponentiation: (^) :: (Num a, Integral b) => a -> b -> a (^^) :: (Fractional a, Integral b) =>