Questions tagged [precision]
For questions related to numerical precision in programming. For classification precision use the tag [precision-recall].
4,914
questions
2091votes
32answers
4.5mviews
Limiting floats to two decimal points
I want a to be rounded to 13.95.
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
The round function does not work the way I expected.
961votes
7answers
506kviews
decimal vs double! - Which one should I use and when? [duplicate]
I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision.
My question is when should a use a double and when should I use a decimal type?
Which type is ...
499votes
14answers
1.1mviews
What is the difference between float and double?
I've read about the difference between double precision and single precision. However, in most cases, float and double seem to be interchangeable, i.e. using one or the other does not seem to affect ...
418votes
13answers
478kviews
JavaScript displaying a float to 2 decimal places
I wanted to display a number to 2 decimal places.
I thought I could use toPrecision(2) in JavaScript .
However, if the number is 0.05, I get 0.0500. I'd rather it stay the same.
See it on JSbin.
...
407votes
16answers
583kviews
How do I print a double value with full precision using cout?
In my earlier question I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a double using full precision?
276votes
4answers
332kviews
How do I interpret precision and scale of a number in a database?
I have the following column specified in a database: decimal(5,2)
How does one interpret this?
According to the properties on the column as viewed in SQL Server Management studio I can see that it ...
265votes
12answers
471kviews
Get DateTime.Now with milliseconds precision
How can I exactly construct a time stamp of actual time with milliseconds precision?
I need something like 16.4.2013 9:48:00:123. Is this possible? I have an application, where I sample values 10 ...
232votes
5answers
59kviews
Why are floating point numbers inaccurate?
Why do some numbers lose accuracy when stored as floating point numbers?
For example, the decimal number 9.2 can be expressed exactly as a ratio of two decimal integers (92/10), both of which can be ...
205votes
26answers
195kviews
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
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....
193votes
36answers
28kviews
Unexpected results when working with very big integers on interpreted languages
I am trying to get the sum of 1 + 2 + ... + 1000000000, but I'm getting funny results in PHP and Node.js.
PHP
$sum = 0;
for($i = 0; $i <= 1000000000 ; $i++) {
$sum += $i;
}
printf("%s", ...
191votes
3answers
10kviews
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(...
186votes
11answers
323kviews
What's the difference between a single precision and double precision floating point operation?
What is the difference between a single precision floating point operation and double precision floating operation?
I'm especially interested in practical terms in relation to video game consoles. ...
173votes
23answers
235kviews
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 ...
159votes
10answers
16kviews
Is floating-point math consistent in C#? Can it be?
No, this is not another "Why is (1/3.0)*3 != 1" question.
I've been reading about floating-points a lot lately; specifically, how the same calculation might give different results on different ...
153votes
3answers
17kviews
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 ...
148votes
9answers
64kviews
Difference between toFixed() and toPrecision()?
I'm new to JavaScript and just discovered toFixed() and toPrecision() to round numbers. However, I can't figure out what the difference between the two is.
What is the difference between number....
139votes
8answers
318kviews
Printf width specifier to maintain precision of floating-point value
Is there a printf width specifier which can be applied to a floating point specifier that would automatically format the output to the necessary number of significant digits such that when scanning ...
137votes
4answers
276kviews
Controlling number of decimal digits in print output in R
There is an option in R to get control over digit display. For example:
options(digits=10)
is supposed to give the calculation results in 10 digits till the end of R session. In the help file of R, ...
124votes
11answers
42kviews
PHP7.1 json_encode() Float Issue
This isn't a question as it is more of a be aware. I updated an application that uses json_encode() to PHP7.1.1 and I was seeing an issue with floats being changed to sometimes extend out 17 digits. ...
110votes
11answers
18kviews
In which order should floats be added to get the most precise result?
This was a question I was asked at my recent interview and I want to know (I don't actually remember the theory of the numerical analysis, so please help me :)
If we have some function, which ...
106votes
9answers
79kviews
Is it safe to check floating point values for equality to 0?
I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case.
While I can understand imprecisions between 0.00000000000001 and 0....
104votes
7answers
58kviews
C# DateTime.Now precision
I just ran into some unexpected behavior with DateTime.UtcNow while doing some unit tests. It appears that when you call DateTime.Now/UtcNow in rapid succession, it seems to give you back the same ...
102votes
5answers
365kviews
Double precision floating values in Python?
Are there data types with better precision than float?
100votes
19answers
82kviews
How to properly round-up half float numbers?
I am facing a strange behavior of the round() function:
for i in range(1, 15, 2):
n = i / 2
print(n, "=>", round(n))
This code prints:
0.5 => 0
1.5 => 2
2.5 => 2
3.5 => 4
4.5 ...
99votes
11answers
104kviews
Should I use double or float?
What are the advantages and disadvantages of using one instead of the other in C++?
97votes
6answers
11kviews
How many double numbers are there between 0.0 and 1.0?
This is something that's been on my mind for years, but I never took the time to ask before.
Many (pseudo) random number generators generate a random number between 0.0 and 1.0. Mathematically there ...
93votes
5answers
103kviews
BigDecimal, precision and scale
I'm using BigDecimal for my numbers in my application, for example, with JPA. I did a bit of researching about the terms 'precision' and 'scale' but I don't understand what are they exactly.
Can ...
92votes
8answers
53kviews
PHP - Floating Number Precision [duplicate]
$a = '35';
$b = '-34.99';
echo ($a + $b);
Results in 0.009999999999998
What is up with that? I wondered why my program kept reporting odd results.
Why doesn't PHP return the expected 0.01?
91votes
12answers
10kviews
Endless sine generation in C
I am working on a project which incorporates computing a sine wave as input for a control loop.
The sine wave has a frequency of 280 Hz, and the control loop runs every 30 µs and everything is written ...
83votes
1answer
225kviews
Dividing two integers to produce a float result [duplicate]
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. ...
83votes
2answers
6kviews
Why is 199.96 - 0 = 200 in SQL?
I have some clients getting weird bills. I was able to isolate the core problem:
SELECT 199.96 - (0.0 * FLOOR(CAST(1.0 AS DECIMAL(19, 4)) * CAST(199.96 AS DECIMAL(19, 4)))) -- 200 what the?
SELECT ...
82votes
7answers
88kviews
What range of numbers can be represented in a 16-, 32- and 64-bit IEEE-754 systems?
I know a little bit about how floating-point numbers are represented, but not enough, I'm afraid.
The general question is:
For a given precision (for my purposes, the number of accurate decimal ...
77votes
4answers
58kviews
What's the C++ suffix for long double literals?
In C++ (and C), a floating point literal without suffix defaults to double, while the suffix f implies a float. But what is the suffix to get a long double?
Without knowing, I would define, say,
...
73votes
2answers
28kviews
In MATLAB, are variables REALLY double-precision by default?
This question arose out of something strange that I noticed after investigating this question further...
I always understood MATLAB variables to be double-precision by default. So, if I were to do ...
69votes
6answers
24kviews
Why is 24.0000 not equal to 24.0000 in MATLAB?
I am writing a program where I need to delete duplicate points stored in a matrix. The problem is that when it comes to check whether those points are in the matrix, MATLAB can't recognize them in the ...
69votes
7answers
64kviews
Adjusting decimal precision, .net
These lines in C#
decimal a = 2m;
decimal b = 2.0m;
decimal c = 2.00000000m;
decimal d = 2.000000000000000000000000000m;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console....
68votes
7answers
63kviews
Is there any way to get current time in nanoseconds using JavaScript?
So, I know I can get current time in milliseconds using JavaScript. But, is it possible to get the current time in nanoseconds instead?
66votes
5answers
16kviews
Are all integer values perfectly represented as doubles? [duplicate]
My question is whether all integer values are guaranteed to have a perfect double representation.
Consider the following code sample that prints "Same":
// Example program
#include <iostream>
...
63votes
8answers
13kviews
Emulate "double" using 2 "float"s
I am writing a program for an embedded hardware that only supports 32-bit single-precision floating-point arithmetic. The algorithm I am implementing, however, requires a 64-bit double-precision ...
63votes
2answers
6kviews
Why does double in C print fewer decimal digits than C++?
I have this code in C where I've declared 0.1 as double.
#include <stdio.h>
int main() {
double a = 0.1;
printf("a is %0.56f\n", a);
return 0;
}
This is what it prints, a is 0....
62votes
4answers
118kviews
Python: Find index of minimum item in list of floats [duplicate]
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 ...
60votes
5answers
79kviews
Set the display precision of a float in Ruby
Is it possible to set the display precision of a float in Ruby?
Something like:
z = 1/3
z.to_s #=> 0.33333333333333
z.to_s(3) #=> 0.333
z.to_s(5) #=> 0.33333
Or do I have to override ...
57votes
4answers
106kviews
Convert Java Number to BigDecimal : best way
I am looking for the best way to convert a Number to a BigDecimal.
Is this good enough?
Number number;
BigDecimal big = new BigDecimal(number.toString());
Can we lose precision with the toString() ...
56votes
4answers
116kviews
Set specific precision of a BigDecimal
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. ...
55votes
10answers
17kviews
Are doubles faster than floats in C#?
I'm writing an application which reads large arrays of floats and performs some simple operations with them. I'm using floats, because I thought it'd be faster than doubles, but after doing some ...
53votes
5answers
62kviews
How to calculate precision and recall in Keras
I am building a multi-class classifier with Keras 2.02 (with Tensorflow backend),and I do not know how to calculate precision and recall in Keras. Please help me.
49votes
7answers
127kviews
Double precision - decimal places
From what I have read, a value of data type double has an approximate precision of 15 decimal places. However, when I use a number whose decimal representation repeats, such as 1.0/7.0, I find that ...
47votes
14answers
18kviews
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
double r = 11.631;
double theta = 21.4;
In the debugger, these are shown as 11.631000000000000 and 21.399999618530273.
How can I avoid this?
47votes
3answers
111kviews
Changing precision of numeric column in Oracle
Currently I have a column that is declared as a NUMBER. I want to change the precision of the column to NUMBER(14,2).
SO, I ran the command
alter table EVAPP_FEES modify AMOUNT NUMBER(14,2)'
for ...