integer

How can I fit a curve to a histogram distribution?

Someone asked me a question via e-mail about integer partitions the other day (as I had released a Perl module, Integer::Partition, to generate them), that I was unable to answer. Background: here are all the integer partitions of 7 (the sum of each row equals 7). 7 6 1 5 2 5 1 1 4 3 4 2 1 4 1 1 1 3 3 1 3 2 2 3 2 1 1 3 1 1 1 1 2 2 2 1 ...

Which is the best way to compare the integer part of two non-integer numbers?

I need to compare the integer part of two doubles for inequality and I'm currently doing this: int iA = (int)dA; int iB = (int)dB; if( iA != iB ) { ... } but I wonder if there's a better approach than this. Thanks. If I used Math.Truncate() instead of a cast to int, would it still be accurate to compare the two resulting double...

Signed versus UnSigned Integers

Am I correct to say the difference between a signed and unsigned integer is: UnSigned can hold a larger positive value, and no negative value. Unsigned uses the leading bit, while the signed version uses the left-most-bit to identify if the number is positive or negative. signed integers can hold both positive and negative numbers. A...

Read 64 bit integer string from file

We have a file that has a 64 bit integer as a string in it. How do we scanf() or otherwise parse this numeric string into an unsigned 64 bit integer type in C++ ? We are aware of things like %lld etc., but a lot of ways to do this parse seem to break compiles under different compilers and stdlibs. The code should compile under gcc and ...

How to parse a month name (string) to an integer for comparison in C#?

I need to be able to compare some month names I have in an array. It would be nice if there were some direct way like: Month.toInt("January") > Month.toInt("May") My Google searching seems to suggest the only way is to write your own method, but this seems like a common enough problem that I would think it would have been already imp...

How do you do maths (or math) with numbers bigger than MaxValue in C#?

I want to be able to process arbitrarily large numbers in C#. I can live with just integers. Are there established algorithms for this kind of thing? Or is there a good 3rd-party library? I might consider changing language if there really is no good way to do it in C#. Thanks for any help / suggestions. ...

c++ integer->std::string conversion. Simple function?

Problem: I have an integer; this integer needs to be converted to a stl::string type. In the past, I've used stringstream to do a conversion, and that's just kind of cumbersome. I know the C way is to do a sprintf, but I'd much rather do a C++ method that is typesafe(er). Is there a better way to do this? Here is the stringstream ap...

Format integer to string with 5 digits

I need to have a string, based on an integer, which should always have 5 digits. Example: myInteger = 999 formatedInteger = "00999" What is the best way of doing this in classic ASP? ...

c++ enum to unsigned int comparison

I found this in the code I'm working on at the moment and thought it was the cause of some problems I'm having. In a header somewhere: enum SpecificIndexes{ //snip INVALID_INDEX = -1 }; Then later - initialization: nextIndex = INVALID_INDEX; and use if(nextIndex != INVALID_INDEX) { //do stuff } Debugging the code, t...

How to handle arbitrarily large integers

I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is factorial(12). What are some techniques for handling integers of an arbitrary maximum size. The language currently works by translating code to ...

How do I convert Unicode to an integer value and not raise an exception if the text is not really an integer

I have some HTML I am trying to parse. There are cases where the html attributes alone are not going to help me identify the row type (header versus data). Fortunately, if my row is a data row then it should have some values that can be converted to integers. I have figured out how to convert the unicode to an integer for those cases ...

boolean operations with integers

Hi guys, This is probably pretty basic... but I don't seem to get it: How does (2 & 1) = 0 (3 & 1) = 1 (4 & 1) = 0 etc.. This pattern above seems to help find even numbers or (0 | 1) = 1 (1 | 1) = 1 (2 | 1) = 3 (3 | 1) = 4 (4 | 1) = 5 (5 | 1) = 5 I know how boolean algebra works between bits. But I don't understand how Boolea...

Using java to encrypt integers

Hi all, I'm trying to encrypt some integers in java using java.security and javax.crypto. The problem seems to be that the Cipher class only encrypts byte arrays. I can't directly convert an integer to a byte string (or can I?). What is the best way to do this? Should I convert the integer to a string and the string to byte[]? Th...

Converting Integer value into AnsiString in Delphi 2009

IntToStr() function returns string which is Unicode now. I want converting to AnsiString. Can I use AnsiString(IntToStr(I)) safely? ...

append an int to char*

How would you append an integer to a char* in c++? ...

Regex to find an integer within a string

I'm new to using regex and I'd like to use it with Java. What I want to do is find the first integer in a string. Example: String = "the 14 dogs ate 12 bones" Would return 14. String = "djakld;asjl14ajdka;sdj" Would also return 14. This is what I have so far. Pattern intsOnly = Pattern.compile("\\d*"); Matcher makeMatch = intsOnly....

How do you do *integer* exponentiation in C#?

The built-in Math.Pow() function in .NET raises a double base to a double exponent and returns a double result. What's the best way to do the same with integers? Added: It seems that one can just cast Math.Pow() result to (int), but will this always produce the correct number and no rounding errors? ...

Should integer divide by zero halt execution?

I know that modern languages handle integer divide by zero as an error just like the hardware does, but what if we could design a whole new language? Ignoring existing hardware, what should a programming language does when an integer divide by zero occurs? Should it return a NaN of type integer? Or should it mirror IEEE 754 float and ...

how to convert string to binary integer file using command line under linux

What i want is to take an integer represented as a string, for example "1234", and convert it to a file called int, containing a 32-bit big endian integer, with the value 1234. The only way I have figured out to do this is something like echo 1234 | awk '{printf "0: %08X", $1}' | xxd -r > int which is a bit nasty! Does anyone know a...

How to portably convert a string into an uncommon integer type?

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from <inttypes.h>, like this: #include <stdio.h> #include <inttypes.h> uint16_t x; char *xs = "17"; sscanf(xs, "%" SCNu16, &x); But a more uncommon integer type like pid_t does not have any suc...