double-precision

Why does adding double.epsilon to a value result in the same value, perfectly equal?

ε祈祈猫儿з 提交于 2019-11-27 23:05:08
问题 I have a unit test, testing boundaries: [TestMethod] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void CreateExtent_InvalidTop_ShouldThrowArgumentOutOfRangeException() { var invalidTop = 90.0 + Double.Epsilon; new Extent(invalidTop, 0.0, 0.0, 0.0); } public static readonly double MAX_LAT = 90.0; public Extent(double top, double right, double bottom, double left) { if (top > GeoConstants.MAX_LAT) throw new ArgumentOutOfRangeException("top"); // not hit } I thought I'd just

Simulate tearing a double in C#

对着背影说爱祢 提交于 2019-11-27 15:50:39
问题 I'm running on a 32-bit machine and I'm able to confirm that long values can tear using the following code snippet which hits very quickly. static void TestTearingLong() { System.Threading.Thread A = new System.Threading.Thread(ThreadA); A.Start(); System.Threading.Thread B = new System.Threading.Thread(ThreadB); B.Start(); } static ulong s_x; static void ThreadA() { int i = 0; while (true) { s_x = (i & 1) == 0 ? 0x0L : 0xaaaabbbbccccddddL; i++; } } static void ThreadB() { while (true) {

Java Doubles are not good at math [closed]

末鹿安然 提交于 2019-11-27 07:39:58
问题 I am currently writing a calculator program in java. It is my first java program, I am used to c++. I have noticed that doubles in java are not at all like doubles in c++. try this in java and c++ 4.1*3 that/.1 it should be 12.3 then 123, and c++ gives this result but java gives 12.299999999999999 and 122.99999999999999 How can I do math like in c++ with doubles, I understand that anything you would use 12.299999999999999 in a program at all would make no difference compared to 12.3, but when

C++ double precision and rounding off

送分小仙女□ 提交于 2019-11-27 07:24:54
问题 I have the following problem: double a = 6.005; double b = 5.995; I want to set precision of doubles 2 digits after point, for example double c = a+b;// I would like to get 11.99 not 12.00 . How can I do this? 回答1: Precision is one thing; rounded for display is quite another. I think this is wrong headed. You should want all the precision you can get and worry about rounding for display when the results are complete. UPDATE: You should not be representing currency using doubles. Last time I

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

♀尐吖头ヾ 提交于 2019-11-27 04:40:18
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); System.out.println("d3 : " + d3); System.out.println("d4 : " + d4); System.out.println("d1 - d2 : " + (d1 - d2));

Does JavaScript have double floating point number precision?

£可爱£侵袭症+ 提交于 2019-11-27 01:48:53
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.) Moin Zaman 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 According to the ECMA-262 specification (ECMAScript is the specification for Javascript), section 8.5: The Number type has exactly 18437736874454810627 (that is, 2 64 −2 53 +3) values, representing the double-precision 64-bit format IEEE 754 values as

How do you round a double in Dart to a given degree of precision AFTER the decimal point?

◇◆丶佛笑我妖孽 提交于 2019-11-27 01:44:12
问题 Given a double, I want to round it to a given number of points of precision after the decimal point , similar to PHP's round() function. The closest thing I can find in the Dart docs is double.toStringAsPrecision(), but this is not quite what I need because it includes the digits before the decimal point in the total points of precision. For example, using toStringAsPrecision(3): 0.123456789 rounds to 0.123 9.123456789 rounds to 9.12 98.123456789 rounds to 98.1 987.123456789 rounds to 987

how to fix double precision issue in java [duplicate]

∥☆過路亽.° 提交于 2019-11-26 22:09:58
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Java floats and doubles, how to avoid that 0.0 + 0.1 + … + 0.1 == 0.9000001? How can I overcome the precision issue with double multiplication in java android??Please note that I am converting a string value into double value. eg: when I multiply two double value: double d1 = Double.valueOf("0.3").doubleValue() * Double.valueOf("3").doubleValue(); System.out.println("Result of multiplication : "+d1); I am

Comparing double values for equality in Java.

孤者浪人 提交于 2019-11-26 21:23:45
问题 I would like some advice from people who have more experience working with primitive double equality in Java. Using d1 == d2 for two doubles d1 and d2 is not sufficient due to possible rounding errors. My questions are: Is Java's Double.compare(d1,d2) == 0 handling rounding errors to some degree? As explained in the 1.7 documentation it returns value 0 if d1 is numerically equal to d2 . Is anyone certain what exactly they mean by numerically equal? Using relative error calculation against

Should I use double or float?

不想你离开。 提交于 2019-11-26 14:09:50
What are the advantages and disadvantages of using one instead of the other in C++? J-16 SDiZ If you want to know the true answer, you should read What Every Computer Scientist Should Know About Floating-Point Arithmetic . In short, although double allows for higher precision in its representation, for certain calculations it would produce larger errors . The "right" choice is: use as much precision as you need but not more and choose the right algorithm . Many compilers do extended floating point math in "non-strict" mode anyway (i.e. use a wider floating point type available in hardware, e.g