Questions tagged [floating-point]

Floating point numbers are approximations of real numbers that can represent larger ranges than integers but use the same amount of memory, at the cost of lower precision. If your question is about small arithmetic errors (e.g. why does 0.1 + 0.2 equal 0.300000001?) or decimal conversion errors, please read the tag page before posting.

Many questions asked here about floating point math are about small inaccuracies in floating point arithmetic. To use the example from the excerpt, 0.1 + 0.2 might result in 0.300000001 instead of the expected 0.3. Errors like these are caused by the way floating point numbers are represented in computers' memory.

Integers are stored as exact values of the numbers they represent. Floating point numbers are stored as two values: a significand and an exponent. It is not possible to find a significand-exponent pair that matches every possible real number. As a result, some approximation and therefore inaccuracy is unavoidable.

Two commonly cited introductory-level resources about floating point math are What Every Computer Scientist Should Know About Floating-Point Arithmetic and the floating-point-gui.de.

FAQs:

Why 0.1 does not exist in floating point

Floating Point Math at https://0.30000000000000004.com/

Related tags:

Programming languages where all numbers are double-precision (64b) floats:

15006 questions
3861
votes
33 answers

Is floating point math broken?

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Why do these inaccuracies happen?
2709
votes
33 answers

How do I parse a string to a float or int?

How can I convert a str to float? "545.2222" → 545.2222 How can I convert a str to int? "31" → 31 For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it. Please instead use…
Tristan Havelick
  • 67,400
  • 20
  • 54
  • 64
2419
votes
18 answers

Difference between decimal, float and double in .NET?

What is the difference between decimal, float and double in .NET? When would someone use one of these?
Tom
2314
votes
36 answers

Limiting floats to two decimal points

I want a to be rounded to 13.95. I tried using round, but I get: >>> a 13.949999999999999 >>> round(a, 2) 13.949999999999999 For the analogous issue with the standard library Decimal class, see How can I format a decimal to always show 2 decimal…
kevin
2296
votes
12 answers

Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optimize the call pow(a,2) by compiling it into a*a, but the call pow(a,6) is not optimized and will actually call the library function pow,…
xis
  • 24,330
  • 9
  • 43
  • 59
1947
votes
41 answers

How do I check if a string represents a number (float or int)?

How do I check if a string represents a numeric value in Python? def is_number(s): try: float(s) return True except ValueError: return False The above works, but it seems clunky. If what you are testing comes from…
Daniel Goldberg
  • 19,908
  • 4
  • 21
  • 29
1665
votes
7 answers

Why does changing 0.1f to 0 slow down performance by 10x?

Why does this bit of code, const float x[16] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6}; const float z[16] = {1.123, 1.234, 1.345, 156.467, 1.578,…
GlassFish
  • 14,851
  • 3
  • 17
  • 22
1204
votes
37 answers

Format number to always show 2 decimal places

I would like to format my numbers to always display 2 decimal places, rounding where applicable. Examples: number display ------ ------- 1 1.00 1.341 1.34 1.345 1.35 I have been using this:…
Varada
  • 16,026
  • 13
  • 48
  • 69
1203
votes
16 answers

Why not use Double or Float to represent currency?

I've always been told never to represent money with double or float types, and this time I pose the question to you: why? I'm sure there is a very good reason, I simply do not know what it is.
Fran Fitzpatrick
  • 17,902
  • 15
  • 33
  • 34
1144
votes
13 answers

Is there a float input type in HTML5?

According to html5.org, the "number" input type's "value attribute, if specified and not empty, must have a value that is a valid floating point number." Yet it is simply (in the latest version of Chrome, anyway), an "updown" control with integers,…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
999
votes
34 answers

How do I use a decimal step value for range()?

How do I iterate between 0 and 1 by a step of 0.1? This says that the step argument cannot be zero: for i in range(0, 1, 0.1): print(i)
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
824
votes
47 answers

How to deal with floating point number precision in JavaScript?

I have the following dummy test script: function test() { var x = 0.1 * 0.2; document.write(x); } test(); This will print the result 0.020000000000000004 while it should just print 0.02 (if you use your calculator). As far as I…
Juri
  • 32,424
  • 20
  • 102
  • 136
800
votes
13 answers

How to convert Decimal to Double in C#?

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the app it gives the following error: Cannot implicitly convert type decimal to double
Eggs McLaren
  • 1,777
  • 3
  • 12
  • 13
783
votes
11 answers

How can I force division to be floating point? Division keeps rounding down to 0?

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a. How can I force c to be a floating point number in…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
775
votes
30 answers

How do you round UP a number?

How does one round a number UP in Python? I tried round(number) but it rounds the number down. Here is an example: round(2.3) = 2.0 and not 3, as I would like. Then I tried int(number + .5) but it round the number down again! Example: int(2.3 +…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
1
2 3
99 100