Questions tagged [numbers]
70 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
53
votes
11 answers
Get 100 highest numbers from an infinite list
One of my friend was asked this interview question -
"There is a constant flow of numbers coming in from some infinite list
of numbers out of which you need to maintain a datastructure as to
return the top 100 highest numbers at any given point…

Sachin Shanbhag
- 543
- 1
- 4
- 7
52
votes
4 answers
Has Little Endian won?
When teaching recently about the Big vs. Little Endian battle, a student asked whether it had been settled, and I realized I didn't know. Looking at the Wikipedia article, it seems that the most popular current OS/architecture pairs use Little…

Ellen Spertus
- 728
- 1
- 6
- 13
50
votes
3 answers
Why is 2^16 a "special" number?
OK, I feel stupid asking this - but in Jeff's article: Getting the Interview Phone Screen Right and originally stated in the 5 essential phone screen questions:
They shouldn't stare blankly at you
when you ask with 2^16 is. It's a
special…

javamonkey79
- 875
- 2
- 10
- 13
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
47
votes
2 answers
Why do some languages round to the nearest EVEN integer?
Programming languages like Scheme (R5RS) and Python (see this Question) round towards the nearest even integer when value is exactly between the surrounding integers.
What is the reasoning behind this?
Is there a mathematical idea that makes…

Profpatsch
- 969
- 8
- 13
36
votes
1 answer
Where does paypal's 92233720368547800 number come from?
There has been a story in the news about a man whose Paypal account was accidentally credited with $92,233,720,368,547,800.
Where does this number come from? What sort of programming bug is likely to give rise to this number?

shamp00
- 2,759
- 3
- 18
- 14
30
votes
5 answers
What is the most efficient way to store a numeric range?
This question is about how many bits are required to store a range. Or put another way, for a given number of bits, what is the maximum range that can be stored and how?
Imagine we want to store a sub-range within the range 0-255.
So for example,…

rghome
- 668
- 5
- 12
28
votes
6 answers
How do random number generators work?
I was just pondering about php rand() function, and thinking about how I could remake it, and I came up completely stupified.
How do random number generators work?

Korvin Szanto
- 645
- 1
- 5
- 10
26
votes
6 answers
Why don't computers store decimal numbers as a second whole number?
Computers have trouble storing fractional numbers where the denominator is something other than a solution to 2^x. This is because the first digit after the decimal is worth 1/2, the second 1/4 (or 1/(2^1) and 1/(2^2)) etc.
Why deal with all sorts…

SomeKittens
- 4,220
- 6
- 31
- 38
22
votes
3 answers
How can I properly compare double values for equality in a unit test?
I recently designed a time series module where my time series is essentially a SortedDictionnary.
Now I would like to create unit tests to make sure that this module is always working and producing the expected result.
A common…

SRKX
- 1,939
- 1
- 15
- 24
20
votes
7 answers
Why does Java have primitives for different size numbers?
In Java there are primitive types for byte, short, int and long and the same thing for float and double. Why is it necessary to have a person set how many bytes should be used for a primitive value? Couldn't the size just be determined dynamically…

yitzih
- 983
- 4
- 15
17
votes
9 answers
Find a "hole" in a list of numbers
What is the fastest way to find the first (smallest) integer that doesn't exist in a given list of unsorted integers (and that is greater than the list's smallest value)?
My primitive approach is sorting them and stepping through the list, is there…

Fabian Zeindl
- 318
- 1
- 2
- 8
16
votes
4 answers
How to identify unstable floating point computations?
In numerics, it is very important to be able to identify unstable
schemes and to improve their stability. How to identify unstable
floating point computations?
I am working on a very complex simulation where many numerical schemes work together and…

user40989
- 2,860
- 18
- 35