floating-accuracy

Different results when adding same doubles in different order

五迷三道 提交于 2021-02-18 20:09:48
问题 Why is the output different when adding same numbers? public class Test { public static void main(String a[]) { double[] x = new double[]{3.9, 4.3, 3.6, 1.3, 2.6}; System.out.println(">>>>>>> " + sum(x)); } public static double sum(double[] d) { double sum = 0; for (int i = 0; i < d.length; i++) { sum += d[i]; } return sum; } } Output is : 15.7 and if I interchange values double[] x = new double[] {2.6, 3.9, 4.3, 3.6, 1.3}; I am getting Output as : 15.700000000000001 How do I get the same

Why two float variables with PHP_INT_MAX values are same unless one of them is added with value greater than 1025

我们两清 提交于 2021-02-08 15:53:59
问题 <?php $x=PHP_INT_MAX; echo ((float)($x+1026)==(float)($x))?'EQUAL':'Not Equal'; I know floating point arithmetic is not exact and $x and $x+1 are so close together that they are rounded to the same floating point value and it shows the output as EQUAL if you use any number between 1 and 1025 but its only after you use value beyond 1025 it will start giving output as 'Not Equal'. I want to know why? What's the reason behind it? Why only after 1025? 回答1: With float, your assumption $x == $x + 1

C# accuracy when checking float in List<float> with Contains method

点点圈 提交于 2021-02-08 13:46:14
问题 I have a list of float s and want to check if it already contains a particular value with the List.Contains() method. I know that for float equality tests you often can't use == but something like myFloat - value < 0.001 . My question is, does the Contains method account for this or I do I need to use a method that accounts for float precision errors for testing if the float is in the list? 回答1: From the docs for List(T).Contains: This method determines equality by using the default equality

Why compile-time floating point calculations might not have the same results as run-time calculations?

安稳与你 提交于 2021-02-07 06:45:06
问题 In constexpr: Introduction, the speaker mentioned "Compile-time floating point calculations might not have the same results as runtime calculations": And the reason is related to "cross-compiling". Honestly, I can't get the idea clearly. IMHO, different platforms may also have different implementation of integers. Why does it only affect floating points? Or I miss something? 回答1: You're absolutely right that, at some level, the problem of calculating floating-point values at compile time is

Set all BigDecimal operations to a certain precision?

回眸只為那壹抹淺笑 提交于 2021-02-07 05:20:20
问题 My Java program is centered around high precision calculations, which need to be accurate to at least 120 decimal places. Consequentially, all non-integer numbers will be represented by BigDecimals in the program. Obviously I need to specify the accuracy of the rounding for the BigDecimals, to avoid infinite decimal expressions etc. Currently, I find it a massive nuisance to have to specify the accuracy at every instantiation or mathematical operation of a BigDecimal. Is there a way to set a

Set all BigDecimal operations to a certain precision?

别说谁变了你拦得住时间么 提交于 2021-02-07 05:16:46
问题 My Java program is centered around high precision calculations, which need to be accurate to at least 120 decimal places. Consequentially, all non-integer numbers will be represented by BigDecimals in the program. Obviously I need to specify the accuracy of the rounding for the BigDecimals, to avoid infinite decimal expressions etc. Currently, I find it a massive nuisance to have to specify the accuracy at every instantiation or mathematical operation of a BigDecimal. Is there a way to set a

Is Google Sheets less or more accurate than Microsoft Excel?

眉间皱痕 提交于 2021-02-05 11:14:29
问题 I am aware of the general problem of numerical inaccuracies with floating point numbers, but I would expect Excel and Google Sheets to behave the same. Unfortunately, they don't - see the following example: A1: 15.525 our reference A2: =3*5.175 should equal 15.525 A3: =A2=A1 shows TRUE, as expected, in both A4: =A2-A1 but A2 is actually smaller in Google Sheets, by -1.78E-15 [Excel shows 0.00E+00] B1: =ROUND(A1,2) shows 15.53, correct, in both B2: =ROUND(A2,2) shows 15.53, correct, in both -

float arithmetic and x86 and x64 context

Deadly 提交于 2021-02-05 06:40:28
问题 We are running some code in both VisualStudio process context (x86 context) and out of VisualStudio context (x64 context). I notice the following code provides a different result in both context (100000000000 in x86 and 99999997952 in x64) float val = 1000f; val = val * val; return (ulong)(val * 100000.0f); We need to obtain a ulong value from a float value in a reliable way, no matter the context and no matter the ulong value, it is just for hashing purpose. I tested this code in both x64

Floating Point comparison specific

故事扮演 提交于 2021-02-04 16:36:47
问题 I have a specific question about floating point comparisons. I know that it's not recommended to use the == comparison due to precision issues, but in this specific case, I am wondering if, in all cases / compilers, this statement will hold true? float a = 1.02f; float b = 1.02f; if(a == b) { print(true); } else { print(false); } In other words, if I assign floating point numbers exactly, with no addition, subtraction, demotion, or promotion, will this always hold true? 回答1: Yes, the compiler

Floating Point comparison specific

白昼怎懂夜的黑 提交于 2021-02-04 16:36:07
问题 I have a specific question about floating point comparisons. I know that it's not recommended to use the == comparison due to precision issues, but in this specific case, I am wondering if, in all cases / compilers, this statement will hold true? float a = 1.02f; float b = 1.02f; if(a == b) { print(true); } else { print(false); } In other words, if I assign floating point numbers exactly, with no addition, subtraction, demotion, or promotion, will this always hold true? 回答1: Yes, the compiler