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.2 + 0.1 equal 0.300000001?) or decimal conversion errors, please read the "info" page linked below 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.1 + 0.1 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:

13427 questions
3313
votes
30 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?
Cato Johnston
  • 38,149
  • 10
  • 35
  • 42
2458
votes
28 answers

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

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31? I just want to know how to parse a float str to a float, and (separately) an int str to an int.
Tristan Havelick
  • 58,645
  • 19
  • 52
  • 64
2194
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
  • 22,592
  • 8
  • 39
  • 55
2192
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
1909
votes
29 answers

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.
kevin
1751
votes
35 answers

How do I check if a string is a number (float)?

What is the best possible way to check if a string can be represented as a number in Python? The function I currently have right now is: def is_number(s): try: float(s) return True except ValueError: return…
Daniel Goldberg
  • 18,355
  • 4
  • 19
  • 28
1584
votes
5 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,051
  • 3
  • 15
  • 22
1033
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
  • 16,032
  • 15
  • 31
  • 33
956
votes
12 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
  • 1,055
  • 124
  • 399
  • 759
916
votes
31 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
  • 12,891
  • 13
  • 45
  • 67
852
votes
33 answers

How to use a decimal range() step value?

Is there a way to step between 0 and 1 by 0.1? I thought I could do it like the following, but it failed: for i in range(0, 1, 0.1): print i Instead, it says that the step argument cannot be zero, which I did not expect.
Evan Fosmark
  • 89,147
  • 33
  • 99
  • 116
745
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
  • 108,984
  • 95
  • 246
  • 308
718
votes
14 answers

How to convert a Decimal to a Double in C#?

I want to use a Track-Bar to change a Form's opacity. This is my code: decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the application, it gives the following error: Cannot implicitly convert type decimal to double I…
Eggs McLaren
  • 1,787
  • 3
  • 12
  • 13
680
votes
44 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
  • 30,146
  • 17
  • 97
  • 132
582
votes
26 answers

How do you round UP a number in Python?

This problem is killing me. How does one roundup a number UP in Python? I tried round(number) but it round the number down. Example: round(2.3) = 2.0 and not 3, what I would like The I tried int(number + .5) but it round the number down again!…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
1
2 3
99 100