Questions tagged [double]

Double is a primitive data type used to store fractional numbers that holds a double-precision floating-point (often 64 bits).

In C and C++ double must be at least as large as float (another floating-point type), but its actual size can vary from system to system as it is implementation-defined. It is typically twice the size of float. Many systems store doubles in binary64, a format described in the IEEE 754 technical standard.

Java has a very strict definition for double, requiring it to be stored in binary64 (which is also called double-precision).

More information:

7762 questions
255
votes
8 answers

biggest integer that can be stored in a double

What is the biggest "no-floating" integer that can be stored in an IEEE 754 double type without losing precision ?
Franck Freiburger
  • 21,662
  • 20
  • 60
  • 90
249
votes
15 answers

How do I print a double value without scientific notation using Java?

I want to print a double value in Java without exponential form. double dexp = 12345678; System.out.println("dexp: "+dexp); It shows this E notation: 1.2345678E7. I want it to print it like this: 12345678 What is the best way to prevent this?
Despicable
  • 3,227
  • 3
  • 21
  • 40
249
votes
19 answers

How do I parse a string with a decimal point to a double?

I want to parse a string like "3.5" to a double. However, double.Parse("3.5") yields 35 and double.Parse("3.5", System.Globalization.NumberStyles.AllowDecimalPoint) throws a FormatException. Now my computer's locale is set to German, wherein a…
Legate
238
votes
3 answers

How do I convert a string to a double in Python?

I would like to know how to convert a string containing digits to a double.
user46646
  • 133,483
  • 43
  • 73
  • 82
234
votes
9 answers

Float and double datatype in Java

The float data type is a single-precision 32-bit IEEE 754 floating point and the double data type is a double-precision 64-bit IEEE 754 floating point. What does it mean? And when should I use float instead of double or vice-versa?
Leo
  • 4,649
  • 5
  • 24
  • 47
217
votes
11 answers

Compare double to zero using epsilon

Today, I was looking through some C++ code (written by somebody else) and found this section: double someValue = ... if (someValue < std::numeric_limits::epsilon() && someValue > -std::numeric_limits::epsilon()) { someValue =…
Sebastian Krysmanski
  • 7,124
  • 9
  • 43
  • 78
191
votes
3 answers

Why is a round-trip conversion via a string not safe for a double?

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s = d1.ToString("R"); double d2 = double.Parse(s); bool s1 = d1 == d2; // -> s1 is False But…
Philip Ding
  • 1,542
  • 1
  • 11
  • 11
184
votes
9 answers

How to round a Double to the nearest Int in swift?

I'm trying to make a calculator of growth rate (Double) that will round the result to the nearest Integer and recalculate from there, as such: let firstUsers = 10.0 let growth = 0.1 var users = firstUsers var week = 0 while users < 14 { …
duarte harris
  • 1,945
  • 2
  • 9
  • 8
179
votes
18 answers

How do I convert a double into a string in C++?

I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
172
votes
15 answers

C# Double - ToString() formatting with two decimal places but no rounding

How do I format a Double to a String in C# so as to have only two decimal places? If I use String.Format("{0:0.00}%", myDoubleValue) the number is then rounded and I want a simple truncate without any rounding. I also want the conversion to String…
kjv
  • 10,117
  • 33
  • 94
  • 138
163
votes
2 answers

Android - Round to 2 decimal places

Possible Duplicate: Round a double to 2 significant figures after decimal point I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a…
goodm
  • 7,127
  • 6
  • 28
  • 55
160
votes
22 answers

Retain precision with double in Java

public class doublePrecision { public static void main(String[] args) { double total = 0; total += 5.6; total += 5.8; System.out.println(total); } } The above code prints: 11.399999999999 How would I get…
Deinumite
  • 3,699
  • 3
  • 22
  • 22
155
votes
11 answers

How to implement infinity in Java?

Does Java have anything to represent infinity for every numerical data type? How is it implemented such that I can do mathematical operations with it? E.g. int myInf = infinity; //However it is done myInf + 5; //returns infinity myInf*(-1);…
user1753100
  • 1,681
  • 2
  • 13
  • 12
153
votes
3 answers

Why does adding 0.1 multiple times remain lossless?

I know the 0.1 decimal number cannot be represented exactly with a finite binary number (explanation), so double n = 0.1 will lose some precision and will not be exactly 0.1. On the other hand 0.5 can be represented exactly because it is 0.5 = 1/2 =…
icza
  • 289,344
  • 42
  • 658
  • 630
139
votes
3 answers

Why does this random value have a 25/75 distribution instead of 50/50?

Edit: So basically what I'm trying to write is a 1 bit hash for double. I want to map a double to true or false with a 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with…
gvlasov
  • 14,781
  • 17
  • 61
  • 99