double-precision

Java BigDecimal precision problems

安稳与你 提交于 2019-11-26 13:52:31
问题 I know the following behavior is an old problem, but still I don't understand. System.out.println(0.1 + 0.1 + 0.1); Or even though I use BigDecimal System.out.println(new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue()); Why this result is: 0.30000000000000004 instead of: 0.3 ? How can I solve this? 回答1: What you actually want is new BigDecimal("0.1") .add(new BigDecimal("0.1")) .add(new BigDecimal("0.1")); The new BigDecimal(double)

Whats wrong with this simple 'double' calculation? [duplicate]

和自甴很熟 提交于 2019-11-26 11:18:27
问题 This question already has an answer here: How to resolve a Java Rounding Double issue [duplicate] 13 answers Whats wrong with this simple \'double\' calculation in java? I know some decimal numbers can not be represented in float / double binary formats properly, but with the variable d3, java is able to store and display 2.64 with no problems. double d1 = 4.64; double d2 = 2.0; double d3 = 2.64; double d4 = d1 - d2; System.out.println(\"d1 : \" + d1); System.out.println(\"d2 : \" + d2);

Does JavaScript have double floating point number precision?

社会主义新天地 提交于 2019-11-26 09:46:59
问题 I know it\'s an odd question, but does JavaScript have the capacity to work with double\'s as opposed to single floats? (64 bit floats vs. 32 bits.) 回答1: All numbers in JavaScript are 64-bit floating point numbers. Ref: http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference http://www.crockford.com/javascript/survey.html 回答2: According to the ECMA-262 specification (ECMAScript is the specification for Javascript), section 8.5: The Number type has exactly 18437736874454810627

How to correctly and standardly compare floats?

[亡魂溺海] 提交于 2019-11-26 08:56:07
问题 Every time I start a new project and when I need to compare some float or double variables I write the code like this one: if (fabs(prev.min[i] - cur->min[i]) < 0.000001 && fabs(prev.max[i] - cur->max[i]) < 0.000001) { continue; } Then I want to get rid of these magic variables 0.000001(and 0.00000000001 for double) and fabs, so I write an inline function and some defines: #define FLOAT_TOL 0.000001 So I wonder if there is any standard way of doing this? May be some standard header file? It

Floating point error in representation?

蹲街弑〆低调 提交于 2019-11-26 06:49:36
问题 when i make this multiplication 0.94 * 8700 the output is 8177.999999999999 but it should have been 8178 i\'m using java , but i don\'t think this error is related to a particular Programming language now my question is ... why this happened ?? and what other numbers (just as an example) cause the same error? 回答1: The specific reason in your case is that the real number 0.94 cannot be represented exactly in a double precision floating point. When you type 0.94 , the actual number stored is 0

strange output in comparison of float with float literal

喜你入骨 提交于 2019-11-25 22:16:49
问题 float f = 0.7; if( f == 0.7 ) printf(\"equal\"); else printf(\"not equal\"); Why is the output not equal ? Why does this happen? 回答1: This happens because in your statement if(f == 0.7) the 0.7 is treated as a double. Try 0.7f to ensure the value is treated as a float: if(f == 0.7f) But as Michael suggested in the comments below you should never test for exact equality of floating-point values. 回答2: This answer to complement the existing ones: note that 0.7 is not representable exactly either