Finding the length of an integer in C

C: You could take the base-10 log of the absolute value of the number, round it down, and add one. This works for positive and negative numbers that aren’t 0, and avoids having to use any string conversion functions. The log10, abs, and floor functions are provided by math.h. For example: int nDigits = floor(log10(abs(the_integer))) … Read more

What happens if I assign a negative value to an unsigned variable?

For the official answer – Section 4.7 conv.integral “If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there … Read more

Is there a built-in or more Pythonic way to try to parse a string to an integer

This is a pretty regular scenario so I’ve written an “ignore_exception” decorator that works for all kinds of functions which throw exceptions instead of failing gracefully: def ignore_exception(IgnoreException=Exception,DefaultVal=None): “”” Decorator for ignoring exception from a function e.g. @ignore_exception(DivideByZero) e.g.2. ignore_exception(DivideByZero)(Divide)(2/0) “”” def dec(function): def _dec(*args, **kwargs): try: return function(*args, **kwargs) except IgnoreException: return DefaultVal return … Read more

Why does the most negative int value cause an error about ambiguous function overloads?

This is a very subtle error. What you are seeing is a consequence of there being no negative integer literals in C++. If we look at [lex.icon] we get that a integer-literal, integer-literal         decimal-literal integer-suffixopt         […] can be a decimal-literal, decimal-literal:         nonzero-digit         decimal-literal ’ opt digit where digit is [0-9] and nonzero-digit is [1-9] and … Read more