precision

Python floating point precision sum

谁都会走 提交于 2020-01-14 19:34:26
问题 I have the following array in python n = [565387674.45, 321772103.48,321772103.48, 214514735.66,214514735.65, 357524559.41] if I sum all these elements, I get this: sum(n) 1995485912.1300004 But, this sum should be: 1995485912.13 In this way, I know about floating point "error". I already used the isclose() function from numpy to check the corrected value, but how much is this limit? Is there any way to reduce this "error"? The main issue here is that the error propagates to other operations,

When is it better to use an NSDecimal, NSDecimalNumber instead of a double?

自古美人都是妖i 提交于 2020-01-14 10:27:32
问题 For simple uses, such as tracking weight values like 65.1kg, is there any benefit of going with NSDecimal/NSDecimalNumber over double? My understanding here is double (or even float) provides more than enough precision in such cases. Please correct me if I'm wrong. 回答1: First, read Josh Caswell's link. It it especially critical when working with money. In your case it may matter or may not, depending on your goal. If you put in 65.1 and you want to get exactly 65.1 back out, then you

How to write a code for link prediction precision assessment in python?

与世无争的帅哥 提交于 2020-01-14 06:42:09
问题 I am doing a link prediction problem using the adamic_adar index. The dataset is a grid network(edgelist with 1000 links). I randomly selected 80% (800) of the edges from the observed dataset. I need to select the highest 200 predicted links from preds as below and also calculate the precision ratio. I dont know what to do next. How would I do..help! import numpy as np import networkx as nx G = nx.read_edgelist('Grid.txt', create_using=nx.Graph(), nodetype=int) preds = nx.adamic_adar_index(G)

Formatting numbers when writing to files in Mathematica

泪湿孤枕 提交于 2020-01-13 09:54:13
问题 This is a continuation of this question regarding number formatting, and related to my earlier question about obtaining very specific Mathematica output to text files. I frequently have to use high precision in Mathematica for data generation but only need relatively low precision for visualization purposes. I also want to store the data for later use with all Symbol names and Array structures intact. For this I have been using Save[] , but there are two related problems. The high precision

How should I compare these doubles to get the desired result?

浪子不回头ぞ 提交于 2020-01-11 12:43:05
问题 I have a simple example application here where I am multiplying and adding double variables and then comparing them against an expected result. In both cases the result is equal to the expected result yet when I do the comparison it fails. static void Main(string[] args) { double a = 98.1; double b = 107.7; double c = 92.5; double d = 96.5; double expectedResult = 88.5; double result1 = (1*2*a) + (-1*1*b); double result2 = (1*2*c) + (-1*1*d); Console.WriteLine(String.Format("2x{0} - {1} = {2}

Floating Point Arithmetic error

。_饼干妹妹 提交于 2020-01-11 08:46:30
问题 I'm using the following function to approximate the derivative of a function at a point: def prime_x(f, x, h): if not f(x+h) == f(x) and not h == 0.0: return (f(x+h) - f(x)) / h else: raise PrecisionError As a test I'm passing f as fx and x as 3.0. Where fx is: def fx(x): import math return math.exp(x)*math.sin(x) Which has exp(x)*(sin(x)+cos(x)) as derivative. Now, according to Google and to my calculator exp(3)*(sin(3)+cos(3)) = -17.050059 . So far so good. But when I decided to test the

How to calculate the number of significant decimal digits of a c++ double?

与世无争的帅哥 提交于 2020-01-10 19:08:34
问题 When dealing with floating point values in Java, calling the toString() method gives a printed value that has the correct number of floating point significant figures. However, in C++, printing a float via stringstream will round the value after 5 or less digits. Is there a way to "pretty print" a float in C++ to the (assumed) correct number of significant figures? EDIT: I think I am being misunderstood. I want the output to be of dynamic length, not a fixed precision. I am familiar with

Order of operations to maximize precision

此生再无相见时 提交于 2020-01-09 11:57:09
问题 I'm using floats for these operations: Which of these two is more precise? (a * b) / c or (a / c) * b Does it matter at all or does it depend on the situation? If so, which should I choose in what cases? 回答1: Really, if you don't use double then you are misguided, and you don't care about precision. Otherwise, you get the best error bounds if the first result is slightly lower than the next higher power of two. For example, calculating (pi * e) / sqrt (2), you get the best error bounds by

Can C# store more precise data than doubles?

六眼飞鱼酱① 提交于 2020-01-09 11:35:51
问题 double in C# don't hold enough precision for my needs. I am writing a fractal program, and after zooming in a few times I run out of precision. I there a data type that can hold more precise floating-point information (i.e more decimal places) than a double? 回答1: Yes, decimal is designed for just that. However, do be aware that the range of the decimal type is smaller than a double. That is double can hold a larger value, but it does so by losing precision. Or, as stated on MSDN: The decimal

Assigning an exact representation to a floating point value

人走茶凉 提交于 2020-01-07 04:01:48
问题 In this question the OP tried to isolate an elusive bug in a big program. He observed some double values and then tried to hard code them as input to a smaller test program. The ideea is great for isolating such a bug. The method chosen however violates the String Aliasing rule, introducing UB, thus the test is completely unreliable: watch in debugger the expression: +------------------------+----------------------| | Watch Expression | Value | +------------------------+----------------------