trigonometry

sin, cos, tan and rounding error

只愿长相守 提交于 2019-11-26 21:39:38
问题 I'm doing some trigonometry calculations in C/C++ and am running into problems with rounding errors. For example, on my Linux system: #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { printf("%e\n", sin(M_PI)); return 0; } This program gives the following output: 1.224647e-16 when the correct answer is of course 0. How much rounding error can I expect when using trig functions? How can I best handle that error? I'm familiar with the Units in Last Place technique for

Rotate a point around another point

纵然是瞬间 提交于 2019-11-26 20:13:20
问题 I have a task to draw a specific graphic. As part of this task I need to rotate some dot's on 45 degrees. I've spent already 2 days trying to calculate a formula, but just couldn't get it right. I've been searching all over the place including this particular website, I'm getting very close, but I'm still not there. Here it is: I need to draw 4 different points I have a specific formula to calculate there position, which is out of scope of the question, but here is what I'm getting as a

Finding Signed Angle Between Vectors

拈花ヽ惹草 提交于 2019-11-26 20:09:09
How would you find the signed angle theta from vector a to b? And yes, I know that theta = arccos((a.b)/(|a||b|)). However, this does not contain a sign (i.e. it doesn't distinguish between a clockwise or counterclockwise rotation). I need something that can tell me the minimum angle to rotate from a to b. A positive sign indicates a rotation from +x-axis towards +y-axis. Conversely, a negative sign indicates a rotation from +x-axis towards -y-axis. assert angle((1,0),(0,1)) == pi/2. assert angle((0,1),(1,0)) == -pi/2. If you have an atan2() function in your math library of choice: signed

Calculating the position of points in a circle

核能气质少年 提交于 2019-11-26 19:42:47
I'm having a bit of a mind blank on this at the moment. I've got a problem where I need to calculate the position of points around a central point, assuming they're all equidistant from the center and from each other. The number of points is variable so it's DrawCirclePoints(int x) I'm sure there's a simple solution, but for the life of me, I just can't see it :) Gareth McCaughan A point at angle theta on the circle whose centre is (x0,y0) and whose radius is r is (x0 + r cos theta, y0 + r sin theta) . Now choose theta values evenly spaced between 0 and 2pi. Brian Driscoll Given a radius

Sin and Cos give unexpected results for well-known angles

◇◆丶佛笑我妖孽 提交于 2019-11-26 19:12:46
I am sure this is a really stupid question, but when I pass an angle of 180 degrees into c/c++'s cos() and sin() functions I appear to receive an incorrect value. I know that it should be: sin of 0.0547 and cos of 0.99 but I get sin of 3.5897934739308216e-009 and cos of -1.00000 My code is: double radians = DegreesToRadians( angle ); double cosValue = cos( radians ); double sinValue = sin( radians ); DegreesToRadians() is: double DegreesToRadians( double degrees ) { return degrees * PI / 180; } Thank you :) C/C++ provides sin(a) , cos(a) , tan(a) , etc. functions that require a parameter with

get cosine similarity between two documents in lucene

女生的网名这么多〃 提交于 2019-11-26 18:43:25
i have built an index in Lucene. I want without specifying a query, just to get a score (cosine similarity or another distance?) between two documents in the index. For example i am getting from previously opened IndexReader ir the documents with ids 2 and 4. Document d1 = ir.document(2); Document d2 = ir.document(4); How can i get the cosine similarity between these two documents? Thank you When indexing, there's an option to store term frequency vectors. During runtime, look up the term frequency vectors for both documents using IndexReader.getTermFreqVector(), and look up document frequency

Java Math.cos(Math.toRadians(<angle>)) returns weird values

时光毁灭记忆、已成空白 提交于 2019-11-26 17:09:45
问题 I've got a little Problem with the Math.cos() method. I know, I have to convert the angle to Radians before using Math.cos() . But if I just do: System.out.println(Math.cos(Math.toRadians(90)); It outputs: 6.123233995736766E-17 Math.sin() is working well. 回答1: From trigonometry: sin x ~= x, for small values of x sin x = cos x+pi/2 Because pi/2 can't be represented exactly in IEEE-754 Floating point, it means, that it must be off by some value x, i.e it is represented by pi/2 +- x, where x <

Why is sin(180) not zero when using python and numpy?

一曲冷凌霜 提交于 2019-11-26 16:54:48
问题 Does anyone know why the below doesn't equal 0? import numpy as np np.sin(np.radians(180)) or: np.sin(np.pi) When I enter it into python it gives me 1.22e-16. 回答1: The number π cannot be represented exactly as a floating-point number. So, np.radians(180) doesn't give you π , it gives you 3.1415926535897931 . And sin(3.1415926535897931) is in fact something like 1.22e-16 . So, how do you deal with this? You have to work out, or at least guess at, appropriate absolute and/or relative error

Math.cos() gives wrong result

ⅰ亾dé卋堺 提交于 2019-11-26 16:48:34
问题 According to Wolfram Mathematica: cos(50) = 0.6427876096865394 ; But this code in Java: System.out.println(Math.cos(50)); gives 0.9649660284921133 . What is wrong with java.lang.Math ? 回答1: Math.cos() expects the parameter to be in radians. This will return the result you need: Math.cos(Math.toRadians(50)); 回答2: Math.cos() uses radians , so to get your expected result you need to do System.out.println(Math.cos(Math.toRadians(50))); 回答3: Most Java trigonometric functions expects parameters to

Convert lat/lon to pixel coordinate?

大憨熊 提交于 2019-11-26 15:26:34
I'm trying to convert a lat/lon pair to a pixel coordinate. I have found this mercator projection but I don't understand the code. What is the factor,x_adj, y_adj variable? When I run the code without those constants my lat/lon pair is not on my map and the x and y pixel coordinate is not what I want. function get_xy(lat, lng) { var mapWidth=2058; var mapHeight=1746; var factor=.404; var x_adj=-391; var y_adj=37; var x = (mapWidth*(180+lng)/360)%mapWidth+(mapWidth/2); var latRad = lat*Math.PI/180; var mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2))); var y = (mapHeight/2)-(mapWidth*mercN/(2