Questions tagged [floating-point]

81 questions
228
votes
8 answers

When do you use float and when do you use double

Frequently, in my programming experience, I need to make a decision whether I should use float or double for my real numbers. Sometimes I go for float, sometimes I go for double, but really this feels more subjective. If I would be confronted to…
Jakub Zaverka
  • 2,429
  • 2
  • 14
  • 8
86
votes
6 answers

Why are floats still part of the Java language when doubles are mostly recommended instead?

In every place I've looked, it says that double is superior to float in almost every way. float has been made obsolete by double in Java, so why is it still used? I program a lot with Libgdx, and they force you to use float (deltaTime, etc.), but it…
Eames
  • 885
  • 1
  • 6
  • 9
85
votes
6 answers

Why is negative zero important?

I'm confused about why we care about different representations for positive and negative zero. I vaguely recall reading claims that having a negative zero representation is extremely important in programming that involves complex numbers. I've never…
jpmc26
  • 5,389
  • 4
  • 25
  • 37
68
votes
7 answers

What causes floating point rounding errors?

I am aware that floating point arithmetic has precision problems. I usually overcome them by switching to a fixed decimal representation of the number, or simply by neglecting the error. However, I do not know what are the causes of this inaccuracy.…
nmat
  • 813
  • 1
  • 7
  • 6
48
votes
4 answers

Are there numbers that are not representable in base 10 but can be represented in base 2?

C# has the decimal type which is used for numbers that needs exact representation in base 10. For instance, 0.1 cannot be represented in base 2 (e.g. float and double) and will always be an approximation when stored in variables that are of these…
Max
  • 599
  • 4
  • 6
42
votes
12 answers

Is there an alternative to bits?

Is there an alternative to bits as the smallest unit of data? Something that won't be only 0 or 1, but actually hold many possible states in between? Wouldn't it be more natural to store floats like that?
Dokkat
  • 557
  • 4
  • 6
35
votes
9 answers

Why are floating point numbers used often in Science/Engineering?

While investigating the accuracy of floating point numbers, I've seen in a few places a statement similar to "float and double are (designed for/used often in) engineering and scientific calculation" From my understanding, the strength of floats…
DoubleDouble
  • 603
  • 1
  • 5
  • 11
35
votes
11 answers

Why do we still use floats?

I understand why floats served a purpose in the past. And I think I can see why they're useful in some simulation examples today. But I think those example are more exceptional than common. So I don't understand why floats are more prevalent in…
anon
34
votes
2 answers

Can anyone explain representation of float in memory?

Can anyone help me in understanding how float values are stored in the memory. My doubt is here float values contain '.' (for example 3.45) how the '.' will be represented in the memory? Can anyone please clarify me with a diagram?
user2720323
  • 991
  • 3
  • 12
  • 11
32
votes
12 answers

Why do you need float/double?

I was watching http://www.joelonsoftware.com/items/2011/06/27.html and laughed at Jon Skeet joke about 0.3 not being 0.3. I personally never had problems with floats/decimals/doubles but then I remember I learned 6502 very early and never needed…
user2528
31
votes
9 answers

Does comparing equality of float numbers mislead junior developers even if no rounding error occurs in my case?

For example, I want to show a list of buttons from 0,0.5,... 5, which jumps for each 0.5. I use a for loop to do that, and have different color at button STANDARD_LINE: var MAX=5.0; var DIFF=0.5 var STANDARD_LINE=1.5; for(var i=0;i<=MAX;i=i+DIFF){ …
ocomfd
  • 5,652
  • 8
  • 29
  • 37
27
votes
5 answers

When conversion from Integer to Single may lose precision

I was reading an article from Microsoft regarding Widening Conversions and Option Strict On when I got to the part The following conversions may lose precision: Integer to Single Long to Single or Double Decimal to Single or Double However,…
Vinicius V
  • 387
  • 3
  • 3
23
votes
5 answers

Solutions for floating point rounding errors

In building an application that deals with a lot of mathematical calculations, I have encountered the problem that certain numbers cause rounding errors. While I understand that floating point is not exact, the problem is how do I deal with exact…
JNL
  • 904
  • 2
  • 8
  • 18
16
votes
6 answers

How to implement float hashing with approximate equality

Let's say we have the following Python class (the problem exists in Java just the same with equals and hashCode) class Temperature: def __init__(self, degrees): self.degrees = degrees where degrees is the temperature in Kelvin as a…
Marten
  • 269
  • 2
  • 6
12
votes
6 answers

Do i still need to use fixed point to guarantee computers get the same result for math operations anymore?

I was told most modern computers follow the same floating point standard, does this mean they will all get the same float answer for a given math operation if the inputs are the same? I ask because i am researching into making an RTS game on a…
WDUK
  • 2,032
  • 3
  • 13
  • 23
1
2 3 4 5 6