Skip to main content

Questions tagged [numbers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
231 votes
8 answers
300k views

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 ...
Jakub Zaverka's user avatar
86 votes
6 answers
20k views

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 ...
Eames's user avatar
  • 885
53 votes
11 answers
19k views

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 ...
Sachin Shanbhag's user avatar
52 votes
4 answers
20k views

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 ...
Ellen Spertus's user avatar
51 votes
3 answers
81k views

Why is 2^16 a "special" number? [closed]

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 ...
javamonkey79's user avatar
48 votes
4 answers
11k views

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 ...
Max's user avatar
  • 599
48 votes
2 answers
8k views

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 ...
Profpatsch's user avatar
36 votes
1 answer
3k views

Where does paypal's 92233720368547800 number come from? [closed]

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 ...
shamp00's user avatar
  • 2,759
30 votes
5 answers
8k views

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 ...
rghome's user avatar
  • 678
28 votes
6 answers
18k views

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/...
SomeKittens's user avatar
  • 4,220
28 votes
6 answers
117k views

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's user avatar
22 votes
3 answers
20k views

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<DateTime, double>. Now I would like to create unit tests to make sure that this module is always ...
SRKX's user avatar
  • 1,939
20 votes
7 answers
3k views

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? ...
yitzih's user avatar
  • 993
17 votes
3 answers
65k views

Is there something special about the number 65535?

2¹⁶-1 & 2⁵ = 2⁵ (or? obviously ?) A developer asked me today what is bitwise 65535 & 32 i.e. 2¹⁶-1 & 2⁵ = ? I thought at first spontaneously 32 but it seemed to easy whereupon I thought ...
Niklas Rosencrantz's user avatar
17 votes
9 answers
16k views

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 ...
Fabian Zeindl's user avatar
16 votes
4 answers
4k views

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 ...
user40989's user avatar
  • 2,930
14 votes
2 answers
16k views

Why '42' is the preferred number when indicating something random?

I have seen this on the SO on many times. Whenever a question is vague and the question is asking some magical answer somebody or the other leaves a comment saying answer is 42. Even a book I am ...
Naveen's user avatar
  • 1,957
13 votes
9 answers
5k views

Why are unsigned numbers implemented?

I can't figure out why microprocessor systems implement unsigned numbers. I guess the cost is just double the number of conditional branches, since greater than, less than, .etc, need a different ...
jtw's user avatar
  • 155
13 votes
1 answer
1k views

Which was the first language to allow underscore in numeric literals?

Java 7+ allows to use underscores in numeric literals,which do not affect the value of the literal, yet are useful for grouping. Examples from the Java 7 documentation, entitled "Underscores in ...
David Tonhofer's user avatar
12 votes
6 answers
4k views

any document which says exactly what range of numbers are .NET BigIntegers designed for?

I'm playing around with the .NET BigInteger and basically I'm wondering what number --an estimated answer would be fine-- is the point of deviation of the curve of (the graph of (increase of time ...
Pacerier's user avatar
  • 5,023
10 votes
8 answers
2k views

A good schema to represent integer numbers from 0 to infinity, assuming you have infinite linear binary storage?

I would like a schema to represent integer numbers starting with 0, without any limit (assuming access to infinite linear storage). Here's a schema that can represent numbers from 0 to 255: Use the ...
Dmitri Shuralyov's user avatar
10 votes
7 answers
15k views

Why are there so many numeric types (bit, int, float, double, long)?

I've learned PHP, Java, and C. Now I'm curious as to why there are so many types of numerical data types like bit, int, float, double, and long. Why not make only one type for numericals? Is there ...
GusDeCooL's user avatar
  • 212
10 votes
3 answers
511 views

Why is the minimum value of ints, doubles, etc 1 farther from zero than the positive value?

I know it has something to do with 2's complement and adding 1, but I don't really get how you can encode one more number with the same amount of bits when it comes to negative numbers.
papercuts's user avatar
  • 203
9 votes
2 answers
25k views

complex numbers in programming?

Are complex numbers used in programming? If they are what is their significance? What IDEs and languages use them? And would it be recommended to learn how to implement them for a programming job (if ...
Bored915's user avatar
  • 295
9 votes
3 answers
57k views

What is the time complexity of the algorithm to check if a number is prime?

What is the time complexity of the algorithm to check if a number is prime? This is the algorithm : bool isPrime (int number) { if (number < 2) return false; if (number == 2) return ...
MD. Mohiuddin Ahmed's user avatar
8 votes
1 answer
3k views

Exclusive upper bound in random number range

Why do languages' random number generators tend to return a value exclusive of the upper bound of the range? For example, an implicit range - JavaScript's random() method "Return a random number ...
John K's user avatar
  • 1,406
7 votes
2 answers
1k views

Why are angles usually represented by floats in programming?

My idea is that, if an unsigned 32-bit integer is used for representing angles in a way that 0 is 0°, 231 is 180° and 232−1 is just under 360°, all possible values are used and evenly distributed ...
matj1's user avatar
  • 187
6 votes
4 answers
487 views

Why is there (practically) no 6-byte integer in common usage?

In Postgres, it used to be quite common to use a 4-byte integer auto field for primary keys, until it started becomming somewhat common to run into the 2147483647 limit of 4-byte integers. Now, it's ...
orokusaki's user avatar
  • 1,093
5 votes
1 answer
987 views

how to stop denormals from messing up a real-time process (like an audio app or plug)

So there is an Intel page about what to do about denorms. (BTW, I know exactly what denormals are and am quite familiar with IEEE-754 floating point standard.) I have two questions: What are the ...
robert bristow-johnson's user avatar
5 votes
2 answers
505 views

Trivially Sortable Encoding for Arbitrary-Precision Decimals

I'm looking for a reasonably(*) space-efficient way to encode arbitrary-precision decimals (e.g. BigDecimal), such that when sorting the bit-pattern of the encodings lexicographically, the numbers ...
Dexter's user avatar
  • 293
5 votes
2 answers
16k views

binary representation in Python and keeping leading zeros

I wanted to use the base64 Python library to encode a sequence of binary digits into base 64. Would it be possible to do it without converting to a string? If not, what is the best way to do it, ...
user60143's user avatar
4 votes
3 answers
5k views

When is a number a magic number?

Over the last couple of months I have been diving into coding standard IfSQ. As a part of this IfSQ standard, a rule is to not use Magic Numbers. While I don't have a problem with building this rule ...
Matthijs's user avatar
  • 167
4 votes
5 answers
3k views

sets of numbers that sum to a certain number?

I was asked this in an interview, and I'm not sure what the answer is or how to approach the problem. Find a pair of numbers that sum up to zero (or any other number), then find three (and then ...
user20598's user avatar
  • 141
4 votes
3 answers
3k views

Is there any programming language(s) which has mathematical number types? [closed]

I may not have worded the title correctly. Is there any programming language(s) which deals with natural mathematical number types rather than the typical data types we see like Int32, Int64, Float, ...
nawfal's user avatar
  • 157
3 votes
3 answers
3k views

In programming light, what is special about the number 1223? [closed]

Just came across this post on reddit, where someone was charged $1,223 for a "FREE STEAK SALAD". I was trying to figure out 'Why 1223?' thinking that this might be a programming error somewhere. But ...
Lazer's user avatar
  • 309
3 votes
2 answers
2k views

Should arbitrary numbers be stored as strings in a database?

I'm using Google as a login system for my latest project. So, my users table has a column named googleid, which stores Google's unique user id, which is a number, with a few dozen digits. For example, ...
markasoftware's user avatar
3 votes
2 answers
2k views

How widespread is the use of arabic numerals in Japanese and Chinese?

I'm localising a times-table app aimed at children. I understand that the Japanese and Chinese languages have a variety of ways to represent numbers. I also have read that, particularly with Japanese, ...
glenstorey's user avatar
2 votes
4 answers
6k views

Understanding binary numbers in terms of real world objects [closed]

When I represent a number in the decimal system, I have an intuitive knowledge of what it amounts to. For example take the number '10': I understand that it means 10 apples or 10 people... i.e I can ...
Kaushik's user avatar
  • 1,195
2 votes
2 answers
1k views

Why are floats called "real numbers" in some languages?

Some programing languages, notably Pascal, have a type of numbers called "real". However, mathematically speaking, these types aren't real. For them to be "real", these types have to be able to ...
Niccolo M.'s user avatar
2 votes
2 answers
431 views

Which numeral systems are useful in computer science?

I am wondering which numeral system different programmers are using, or would use if their language has support for them. As an example, in C++ we can use: Octal by prefixing with 0 (e.g. 0377) ...
authchir's user avatar
  • 370
2 votes
3 answers
319 views

Are all pseudo randomly generated numbers within a given period unique?

This will of course depend on the algorithm generating the pseudo random numbers, but what I'm wondering is whether practical, usable pseudo-random number generating algorithms exist that never repeat ...
Peter Berg's user avatar
2 votes
1 answer
125 views

If you can store BigInts in a more optimal way than an array

This suggests just storing BigInts as an array: // n = -123 var n = { sign: -1, digits: [3, 2, 1] }; However, if you have "big ints", that array will get large: var n = { sign: -1, ...
Lance Pollard's user avatar
2 votes
1 answer
1k views

BigDecimal for Calculation with and Display of Stock Shares and Prices?

I'm receiving flat files with the stock price and number of held shares for an account. One file contains the price and shares to 6 decimal places. The other file contains the price and shares to 12 ...
James's user avatar
  • 285
2 votes
3 answers
341 views

What implementation do you use when dealing with operators in .Net?

My question is in a sense a follow up of this post on Time Series in .Net. Ideally, you would like to expand the basic TimeSeries<T> class by something like NumericTimeSeries<T>, where ...
SRKX's user avatar
  • 1,939
2 votes
1 answer
582 views

How does this function for calculating modular exponentiation work ?

I know that the rule in maths for modulus is this: ab mod n =(a mod n ) (b mod n) mod n I have found the following code for computing the modular exponentiation: pow(base,exponent,modulus){ if (...
user avatar
2 votes
1 answer
722 views

Filling array with numbers from given range so that sum of adjacent numbers is square number

Problem: Fill all the cells using distinct numbers from <1,25> set, so that sum of two adjacent cells is a square number. (source: http://grymat.im.pwr.wroc.pl/etap1/zad1etp1213.pdf; numbers 20 ...
syntagma's user avatar
  • 309
1 vote
2 answers
3k views

The number of characters of hexadecimal numbers

0xF, 0x0000000F (total width is 10 characters), and 0x0000000000000000F (total width is 18 characters) all mean 15 (decimal). Is it correct to say that there is no reason to add leading zeros to a ...
user90726's user avatar
  • 205
1 vote
5 answers
6k views

Generate unique integer from two integers with algorithm

I am currently trying to come up with an algoritm which would take at least two numbers (say user IDs) and then come up with an unique number which is generated based on these two numbers. Each ...
Purple Dragon's user avatar
1 vote
3 answers
213 views

Does printing out numbers involve converting the numbers into characters?

In programming languages, when numbers (either integer, or real) are printed out, are they firstly converted to the codes of the readable characters that are meant to represent the numbers, and then ...
Tim's user avatar
  • 5,495
1 vote
3 answers
361 views

Generate random numbers with certain spikes? [duplicate]

I am trying to generate random numbers to simulate steering wheel angles and velocities of a car. So for steering wheels the random numbers could go like 1,2,4,6,4,3,40,0. I can generate random ...
zzzzz's user avatar
  • 227