math

Arithmetic error when adding two double values [duplicate]

こ雲淡風輕ζ 提交于 2020-01-15 11:13:53
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Floating point inaccuracy examples double arithmetic and equality in Java I caught this issue while trying to debug a sorting routine that checked if two values were equal. Getting the values was simply doing some addition on two double variables: 0.31 + 0.27. When the sort compared the sum of those two against the some of another objects, whose sum also equaled 0.58, it told me the comparison was not equal.

Computing Jacobian And Passing to Scipy Minimize

老子叫甜甜 提交于 2020-01-15 10:43:53
问题 I know the jacobian is the first derivative, but I don't know how to compute it for my simple function (I tried online derivative calculators) and pass it to my scipy minimize function. In code, here is the objective function (guess arrays can contain thousands of variables): def objective(current_guesses_array, first_guesses_array): return np.sum(np.divide(np.square(current_guesses_array - first_guesses_array), first_guesses_array)) I think the Jacobian is like this, but definitely may have

Computing Jacobian And Passing to Scipy Minimize

眉间皱痕 提交于 2020-01-15 10:39:27
问题 I know the jacobian is the first derivative, but I don't know how to compute it for my simple function (I tried online derivative calculators) and pass it to my scipy minimize function. In code, here is the objective function (guess arrays can contain thousands of variables): def objective(current_guesses_array, first_guesses_array): return np.sum(np.divide(np.square(current_guesses_array - first_guesses_array), first_guesses_array)) I think the Jacobian is like this, but definitely may have

How to randomize points on a sphere surface evenly?

匆匆过客 提交于 2020-01-15 10:37:30
问题 Im trying to make stars on the sky, but the stars distribution isnt even. This is what i tried: rx = rand(0.0f, PI*2.0f); ry = rand(0.0f, PI); x = sin(ry)*sin(rx)*range; y = sin(ry)*cos(rx)*range; z = cos(ry)*range; Which results to: img http://img716.imageshack.us/img716/3320/sphererandom.jpg And: rx = rand(-1.0f, 1.0f); ry = rand(-1.0f, 1.0f); rz = rand(-1.0f, 1.0f); x = rx*range; y = ry*range; z = rz*range; Which results to: img2 http://img710.imageshack.us/img710/5152/squarerandom.jpg

Output is zero when dividing?

情到浓时终转凉″ 提交于 2020-01-15 10:26:49
问题 Maybe I can't see obvious thing but: int x1 = 2; int y1 = 4; int x2 = 11; int y2 = 7; double res = (y2-y1)/(x2-x1); System.out.println(res); Output: 0.0 Why? 回答1: you need to initially define those variables as doubles and it should work. 回答2: The problem is you're doing integer arithmetic. You need a typecast in order to convert the numerator or denominator to floating point first (e.g.): int x1 = 2; int y1 = 4; int x2 = 11; int y2 = 7; double res = (double)(y2-y1)/(x2-x1); System.out

Sieve of Erathostenes ArrayIndexOutOfBounds

眉间皱痕 提交于 2020-01-15 09:42:28
问题 trying to implement a simple sieve of erathosthenes to solve this question on project euler : The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Link My code keeps returning this error however : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147479015 at Prime.main(Prime.java:28) Can anyone give me any hints as to why? Here is the code: import java.math.BigInteger; public class Prime { /* * Input: an integer n > 1 *

Cartesian power - via recursion

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 09:32:05
问题 The original question is here: Cartesian power (a special Cartesian product) -- choose elements from array, in repeatable style In the old question, there are already answers gave a solution via iteration. I am wondering is there a recursive solution, similar as the solution from following link, which print permutations with recursion: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ Currently I have wrote following program, which is not correct yet

Cartesian power - via recursion

旧巷老猫 提交于 2020-01-15 09:31:44
问题 The original question is here: Cartesian power (a special Cartesian product) -- choose elements from array, in repeatable style In the old question, there are already answers gave a solution via iteration. I am wondering is there a recursive solution, similar as the solution from following link, which print permutations with recursion: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ Currently I have wrote following program, which is not correct yet

Floor function to float and double values

大兔子大兔子 提交于 2020-01-15 07:34:25
问题 How to apply floor function to float or double values to get integer.I got double Value:4.4497083717E10 float Value:4.4497084E10 out of my function.I got floor values as Floor of double:4.4497083717E10 Floor of float:4.4497084416E10.Is there a possibility that floor results in a integer?? 回答1: .Is there a possibility that floor results in a integer?? From a type perspective - no. Math.floor is declared to return a double , and so will never return something with a static type of int . That

How to compute a^b^c mod p?

妖精的绣舞 提交于 2020-01-15 07:28:30
问题 I am trying to compute a^b^c mod p for some positive integers a,b,c,p. One possible (and obvious) way is to use fast modular exponentiation which will run in O(log(b^c))=clog(b) . While I don't mind the efficiency here, the obvious downfall of this method is that you need an explicit binary representation of b^c which in itself is already exponential. So the question for me is, if I can not represent b^c as a binary representation, is there a way I can compute a^b^c mod p from the binary