floating-point-precision

Python: Find index of minimum item in list of floats [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-28 19:37:44
问题 This question already has answers here : Getting the index of the returned max or min item using max()/min() on a list (22 answers) Closed 4 years ago . How can I find the index of the minimum item in a Python list of floats? If they were integers, I would simply do: minIndex = myList.index(min(myList)) However, with a list of floats I get the following error, I assume because float equality comparison is rather iffy. ValueError: 0.13417985135 is not in list Now, I know that I could simply

Confusion with floating point numbers

南楼画角 提交于 2019-11-28 13:58:15
int main() { float x=3.4e2; printf("%f",x); return 0; } Output: 340.000000 // It's ok. But if write x=3.1234e2 the output is 312.339996 and if x=3.12345678e2 the output is 312.345673 . Why are the outputs like these? I think if I write x=3.1234e2 the output should be 312.340000 , but the actual output is 312.339996 using GCC compiler. Not all fractional numbers have an exact binary equivalent so it is rounded to the nearest value. Simplified example, if you have 3 bits for the fraction, you can have: 0 0.125 0.25 0.375 ... 0.5 has an exact representation, but 0.1 will be shown as 0.125. Of

Converting Int to Float loses precision for large numbers in Swift

时间秒杀一切 提交于 2019-11-28 12:16:38
XCode 6.3.1 Swift 1.2 let value: Int = 220904525 let intmax = Int.max let float = Float(value) // Here is an error probably let intFromFloat = Int(float) let double = Double(value) println("intmax=\(intmax) value=\(value) float=\(float) intFromFloat=\(intFromFloat) double=\(double)") // intmax=9223372036854775807 value=220904525 float=2.20905e+08 intFromFloat=220904528 double=220904525.0 The initial value is 220904525. But when I convert it to float it becomes 220904528. Why? This is due to the way the floating-point format works. A Float is a 32-bit floating-point number, stored in the IEEE

epsilon for various float values

别等时光非礼了梦想. 提交于 2019-11-28 11:40:35
问题 There is FLT_MIN constant that is nearest to zero. How to get nearest to some number value? As an example: float nearest_to_1000 = 1000.0f + epsilon; // epsilon must be the smallest value satisfying condition: // nearest_to_1000 > 1000.0f I would prefer numeric formula without using special functions. 回答1: Caution: Bugs were found in this code while working on another answer. I hope to update this later. In the meantime, it fails for some values involving subnormals. C provides a function for

Set specific precision of a BigDecimal

a 夏天 提交于 2019-11-28 08:52:20
I have an XSD that requires me to use a BigDecimal for a lat/lon. I currently have the lat/lon as doubles, and convert them to BigDecimal, but I am only required to use about 12 places of precision. I have not been able to figure out how to set that. Can anyone help me with this? You can use setScale() e.g. double d = ... BigDecimal db = new BigDecimal(d).setScale(12, BigDecimal.ROUND_HALF_UP); The title of the question asks about precision. BigDecimal distinguishes between scale and precision. Scale is the number of decimal places. You can think of precision as the number of significant

Why does 0.06 + 0.01 = 0.07 in ColdFusion?

守給你的承諾、 提交于 2019-11-28 06:56:40
问题 Why don't math operations in ColdFusion seem to be affected by floating point math issues? Take the code: result = 0.06 + 0.01; writedump(result); writedump(result.getClass().getName()); Which outputs 0.07 java.lang.Double However the equivlant Java code produces what I"d expect when adding two doubles: public static void main(String[] args) { double a = 0.01d; double b = 0.06d; System.out.println(a + b); //0.06999999999999999 } This is what I'd expect to see from ColdFusion because of the

numpy matrix inversion rounding errors

自古美人都是妖i 提交于 2019-11-28 02:23:56
I am getting a very strange value for my (1,1) entry for my BinvA matrix I am just trying to invert B matrix and do a (B^-1)A multiplication. I understand that when I do the calculation by hand my (1,1) is supposed to be 0 but instead I get 1.11022302e-16. How can I fix it? I know floating point numbers can't be represented to full accuracy but why is this giving me such an inaccurate response and not rounding to 0 is there any way I can make it more accurate? Her is my code: import numpy as np A = np.array([[2,2],[4,-1]],np.int) A = A.transpose() B = np.array([[1,3],[-1,-1]],np.int) B = B

PHP - Getting a float variable internal value

a 夏天 提交于 2019-11-28 01:54:24
问题 I am trying to establish the delta I need when doing float comparison in PHP. I want to take a closer look at my variables to see the difference. I have 2 computed variables, $a, $b. $a = some_function(); $b = some_other_function(); How can I see the exact number which PHP uses? I want to compare them with this formula, where I need to specify the delta: $delta = 0.00001; if (abs($a-$b) < $delta) { echo "identical"; } var_dump($a, $b) returns 1.6215; 1.6215. but I know that they are not

Get next smallest Double number

ぐ巨炮叔叔 提交于 2019-11-28 01:47:37
As part of a unit test, I need to test some boundary conditions. One method accepts a System.Double argument. Is there a way to get the next-smallest double value? (i.e. decrement the mantissa by 1 unit-value)? I considered using Double.Epsilon but this is unreliable as it's only the smallest delta from zero, and so doesn't work for larger values (i.e. 9999999999 - Double.Epsilon == 9999999999 ). So what is the algorithm or code needed such that: NextSmallest(Double d) < d ...is always true. If your numbers are finite, you can use a couple of convenient methods in the BitConverter class: long

Why does for loop using a double fail to terminate

梦想的初衷 提交于 2019-11-28 01:47:23
I'm looking through old exam questions (currently first year of uni.) and I'm wondering if someone could explain a bit more thoroughly why the following for loop does not end when it is supposed to. Why does this happen? I understand that it skips 100.0 because of a rounding-error or something, but why? for(double i = 0.0; i != 100; i = i +0.1){ System.out.println(i); } The number 0.1 cannot be exactly represented in binary, much like 1/3 cannot be exactly represented in decimal, as such you cannot guarantee that: 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1==1 This is because in binary : 0.1=