21
votes
Accepted
Huge integer class using base 256
General
I like the presentation. It's easy to read, with good use of whitespace and useful comments.
Width
It's inconvenient to have to recompile to use a larger width ...
21
votes
Standard C overflow-safe arithmetic functions
This code has a lot of problems.
#define err exit(1)
exit(1) is not a good way to terminate the program when trapping a bug. <...
17
votes
Standard C overflow-safe arithmetic functions
I'm pretty sure this causes undefined behaviour:
#define long int64_t
Even if I'm wrong, it's extremely confusing to readers, and will break any subsequently-...
15
votes
Accepted
'StrictInt' Python object class that prohibits casting numbers with non-integer components to `int`
Converting everything via a float means that you get the wrong result whenever the input cannot be represented exactly as a double-precision floating-point number. ...
15
votes
128-bit unsigned integer
std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use ...
15
votes
Accepted
Swapping bytes of an integer in Java
Historical Note
The very first comment on the OP's question -- long before any answers were posted -- was a question asked by myself, and it very pointedly asked:
Without using Integer.reverseBytes(...
15
votes
Accepted
Minimal `printf` for integer types in x86 assembly
If, for the sake of this exercise, codesize is the only thing that you care about, then I would dare suggest the following:
For divq10, not having to reload the constant will save 5 bytes, and using <...
14
votes
Accepted
C++17 saturating integer (arithmetic) type library
Refactor the limits
There's quite a lot of repetition of std::numeric_limits<T>::max() and ...
13
votes
Accepted
Int to Enum Extension Method
As Jason Tyler said in a comment, this has a problem that [Flags] enumerations won't be properly handled.
I ran this in C# interactive, and it threw an exception (...
13
votes
Accepted
C function to read only numeric values
Definition of function main
The definition of the function main should start with
...
12
votes
Infinite precision integer in C++20
I don’t have time to do a full review, unfortunately, but while skimming your code, I did notice a lurking bug:
...
11
votes
Format an integer with thousands separator (recursive implementation)
ArtemyVysotsky has provided you with a improved solution, so I am just going to give some general advice:
Do not use using namespace std; since it not only lowers ...
11
votes
Accepted
Haskell BigInt addition
Your code contains at least one bug, therefore this review is a little bit shorter than usual since you need to fix that bug either way.
So let's instead have a look at testing and how you can find ...
11
votes
Accepted
Huge integer class using base 2^32 (was 256) follow up
I recommend including your internal headers first, before any standard headers. This helps expose any accidental dependencies that make it hard to use your types in another program.
So in HugeInt....
11
votes
Find all ways to express each of a list of even numbers as the sum of 2 positive, even numbers
You can divide any even number A into 2 (even) and A-2 (even).
Do you want all the solutions instead? There's infinitely many. Negative numbers can also be even. Are we talking about just natural ...
10
votes
Accepted
An exception-safe wrapper for std::stoi
This wrapper effectively removes some of the available functionality from std::stoi() because its signature is
...
10
votes
Huge integer class using base 256
In addition to the good suggestions from @TobySpeight, I noted the following (in source order):
HugeInt.h:
#include <ostream> instead of ...
10
votes
Huge integer class using base 2^32 (was 256) follow up
API
In HugeInt(long long int), there's no need for the const. In the function declaration it is ignored, at least by the GNU C++ ...
10
votes
Fast 16-bit x 16-bit unsigned integer division algorithm for ATMEGA1284
Have mercy on the maintenance programmer -
It may be your older self.
Separate documentation tends to become
separated, as in not always readily accessible
(Murphy: when direly needed)
out of sync -...
10
votes
Accepted
Represent integer as binary in C
More specific types are available for an eight bit number, such as uint8_t
static keyword is dangerous (when used as an output ...
9
votes
Accepted
Format an integer with thousands separator (recursive implementation)
Seems to me you overcomplicate the things.
IF you split your number into "beginning" and "last 3 digits" inside your RecursiveCommas you have to ...
9
votes
Accepted
reinterpret_cast vs bit shifts for extracting 32-bit integers from bytes, with either endianness
You are correct to be concerned about the first method. It generates UB if buf[offset] doesn't happen to be at the right alignment boundary for a 32-bit value.
The ...
9
votes
9
votes
Minimal `printf` for integer types in x86 assembly
Make Your Function Modular and Re-Usable
Let’s say you want to call fprintf( stderr, "Error opening file: s.\n", errMsg ); Your function can’t do that, ...
9
votes
Converting int values in Java to human readable strings in English
For the input -2147483648, your program outputs just minus . Also, you never handle 0 ...
9
votes
Accepted
Sum numbers digit by digit
Types
When I hear one-dimensional array, I think first think of a list [3, 4, 5], not a tuple (3, 4, 5). If your method is ...
9
votes
Infinite precision integer in C++20
all bitwise operators require both operands to be nonnegative.
This is a possible design trade-off, personally I prefer Python's style of "as-if infinitely sign-extended", in which negative ...
9
votes
Positive Integer Class Supporting Arbitrary Number of Digits
Use
First of all, I'll assume that this is an exercise, as java.math.BigInteger already provides most of the functionality that is specified.
The class is not ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
integer × 396c++ × 90
c × 83
java × 82
performance × 51
strings × 43
bitwise × 43
python × 41
beginner × 36
algorithm × 28
c# × 25
reinventing-the-wheel × 24
formatting × 24
parsing × 21
mathematics × 21
array × 19
converting × 18
assembly × 18
javascript × 16
python-3.x × 16
c++11 × 16
number-systems × 15
serialization × 14
floating-point × 12
interview-questions × 11