math

General floating-point maths query

那年仲夏 提交于 2020-01-14 13:17:26
问题 Okay so I get that some numbers can't be represented properly in binary just like 1/3 can't be fully represented in decimal. So how come when I console.log(0.3) it returns 0.3 but when I console.log(0.1 + 0.2) it returns the 0.30000000000000004 How come it is accounting for the error (if it even is) when simply outputting 0.3 but doesn't when the addition occurs? 回答1: Suppose we approximate 1/3 and 2/3 in decimal. 1/3 = 0.333 2/3 = 0.667 and we add 1/3+1/3: 1/3+1/3 = 0.333 + 0.333 = 0.666 We

General floating-point maths query

帅比萌擦擦* 提交于 2020-01-14 13:17:06
问题 Okay so I get that some numbers can't be represented properly in binary just like 1/3 can't be fully represented in decimal. So how come when I console.log(0.3) it returns 0.3 but when I console.log(0.1 + 0.2) it returns the 0.30000000000000004 How come it is accounting for the error (if it even is) when simply outputting 0.3 but doesn't when the addition occurs? 回答1: Suppose we approximate 1/3 and 2/3 in decimal. 1/3 = 0.333 2/3 = 0.667 and we add 1/3+1/3: 1/3+1/3 = 0.333 + 0.333 = 0.666 We

Iterate over lists with a particular sum

限于喜欢 提交于 2020-01-14 12:09:57
问题 I would like to iterate over all lists of length n whose elements sum to 2. How can you do this efficiently? Here is a very inefficient method for n = 10 . Ultimately I would like to do this for `n > 25'. n = 10 for L in itertools.product([-1,1], repeat = n): if (sum(L) == 2): print L #Do something with L 回答1: you only can have a solution of 2 if you have 2 more +1 than -1 so for n==24 a_solution = [-1,]*11 + [1,]*13 now you can just use itertools.permutations to get every permutation of this

Iterate over lists with a particular sum

99封情书 提交于 2020-01-14 12:07:26
问题 I would like to iterate over all lists of length n whose elements sum to 2. How can you do this efficiently? Here is a very inefficient method for n = 10 . Ultimately I would like to do this for `n > 25'. n = 10 for L in itertools.product([-1,1], repeat = n): if (sum(L) == 2): print L #Do something with L 回答1: you only can have a solution of 2 if you have 2 more +1 than -1 so for n==24 a_solution = [-1,]*11 + [1,]*13 now you can just use itertools.permutations to get every permutation of this

Method for calculating distance between all points in a dataframe containing a list of xy coordinates

最后都变了- 提交于 2020-01-14 10:33:24
问题 I'm sure this has been answered before, but I can't find the thread for the life of me! I am trying to use r to produce a list of all the distances between pairs of xy coordinates in a dataframe. The data is stored something like this: ID = c('1','2','3','4','5','6','7') x = c(1,2,4,5,1,3,1) y = c(3,5,6,3,1,5,1) df= data.frame(ID,x,y) At the moment I can calculate the distance between two points using: length = sqrt((x1 - x2)^2+(y1 - y2)^2). However, I am uncertain as to where to go next.

Method for calculating distance between all points in a dataframe containing a list of xy coordinates

会有一股神秘感。 提交于 2020-01-14 10:32:30
问题 I'm sure this has been answered before, but I can't find the thread for the life of me! I am trying to use r to produce a list of all the distances between pairs of xy coordinates in a dataframe. The data is stored something like this: ID = c('1','2','3','4','5','6','7') x = c(1,2,4,5,1,3,1) y = c(3,5,6,3,1,5,1) df= data.frame(ID,x,y) At the moment I can calculate the distance between two points using: length = sqrt((x1 - x2)^2+(y1 - y2)^2). However, I am uncertain as to where to go next.

Python - Vincenty's inverse formula not converging (Finding distance between points on Earth)

百般思念 提交于 2020-01-14 10:17:05
问题 I'm attempting to implement Vincenty's inverse problem as described on wiki HERE The problem is that lambda is simply not converging. The value stays the same if I try to iterate over the sequence of formulas, and I'm really not sure why. Perhaps I've just stared myself blind on an obvious problem. It should be noted that I'm new to Python and still learning the language, so I'm not sure if it's misuse of the language that might cause the problem, or if I do have some mistakes in some of the

Compacting mathematical graph

眉间皱痕 提交于 2020-01-14 09:33:38
问题 I want to draw a graph that will be something like this: alt text http://img25.imageshack.us/img25/9786/problemo.png You can see 3 pathes: a, b & c. How can I change position of elements (1,2,3...,9) to make long of the path as short as possible? I mean this lines should be as short as possible. Im very interested in it becouse I am drawing a graph with question, some kind of infographic like 'follow the lines to know the answer'. I know that its a bit about graph theory... so if its too hard

is <math.h> for C or C++?

喜夏-厌秋 提交于 2020-01-14 08:55:50
问题 Im needing the natural logarithm function for use in a .cpp (c++) source file. Now, of course I can do this with a quick google search and a simple library solution. But Im a bit confused... On the cplusplus dot com website under reference/cmath/log/ they have an example of how to use the log function, as follows /* log example */ #include <stdio.h> /* printf */ #include <math.h> /* log */ int main () { double param, result; param = 5.5; result = log (param); printf ("log(%f) = %f\n", param,

c++ convert a fractional part of a number into integer

微笑、不失礼 提交于 2020-01-14 08:51:51
问题 I needed to convert a fractional part of a number into integer without a comma, for example I have 3.35 I want to get just 35 part without zero or a comma, Because I used the modf() function to extract the the fractional part but it gives me a 0.35 if there is any way to do that or to filter the '0.' part I will be very grateful if you show me how with the smaller code possible, 回答1: A bit more efficient than converting to a string and back again: int fractional_part_as_int(double number, int