arithmetic

Arithmetic with IPv6 addresses (large integers)

I'm working with IPv6 addresses in the form: FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF Internally, I store them in an array: TIp6Bytes = array [0..15] of Byte; I need to manipulate the IPv6 addresses in a number of ways including adding, dividing, multiplying etc. Can anyone suggest a good way to do this? I guess I should have mentio...

Date arithmetic in Java

My program is about generating (producing) a Kurosawa and making the customers produce it. Every time we generate a Kurosawa, we have to print its id, its production date and expiration date, which is 3 months from the production date. My problem is: How can I calculate the date after 3 months? Thank you. ...

fixnum and prime numbers in ruby

Before I set about to writing this myself, has anyone seen a ruby implementation of the following behavior? puts 7.nextprime(); #=> 11 puts 7.previousprime(); #=> 5 puts 7.isprime(); #=> true Obviously this kind of thing would be ugly for large numbers but for integers never exceeding a few thousand (the common instance fo...

Arithmetic operator overloading for a generic class in C#

Given a generic class definition like public class ConstrainedNumber<T>: IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>, IComparable where T:struct, IComparable, IComparable<T>, IEquatable<T>, how can I define arithmetic operators for it? The following does not compile, because the '+'...

PHP - Large Interger mod calculation

I need to calculate modulus with large number like : <?php $largenum = 95635000009453274121700; echo $largenum % 97; ?> It's not working... beacause $largenum is too big for an int in PHP. Any idea how to do this ? ...

How do I force 64 bit integer arithmetic on OS X?

I am trying to force 64 bit long integers on OS X 10.5.6. running on an Apple MacBook Intel Core 2 Duo. Here is my c code: #include<stdio.h> int main() { long a = 2147483647; /*== 2^32 - 1*/ long aplus1; printf("a== %d. sizeof(a) == %d \n", a, sizeof(a)); aplus1 = a+1; printf("aplus1 = %d \n", aplus1); } Compi...

Why do my .net Int64's behaves as if they were Int32's ?

I'm witnessing a strange behavior in a .net program : Console.WriteLine(Int64.MaxValue.ToString()); // displays 9223372036854775807, which is 2^63-1, as expected Int64 a = 256*256*256*127; // ok Int64 a = 256*256*256*128; // compile time error : //"The operation overflows at compile time in checked mode" // If i do this at runtime, I...

Splitting Probabilities

I've the following code in PHP which works fine (returns more or less 10 results each time it runs): function GetAboutTenRandomNumbers() { $result = array(); for ($i = 0; $i < 240; $i++) { if (Chance(10, 240) === true) { $result[] = $i; } } echo '<pre>'; print_r($result); echo '</pre>';...

Why do double and float exist?

Duplicate http://stackoverflow.com/questions/803225/when-should-i-use-double-instead-of-decimal .. and many more... We use the C# and SQL Server decimal datatypes throughout our apps because of their accuracy. Never had any of those irritating problems where the total doesn't add up to the detail, etc. I was wonderi...

Image Arithmetic functions in C++

I'm trying to find/write a function that would perform the same operation as imlincomb(). However, I am having trouble finding such functions in C++ without using any Matlab API functions other than Intel Performance Primitiives library, and I don't really want to purchase a license for it unless my application really has to take advanta...

POD low dimensional vector in boost

I'm looking for POD low dimension vectors (2,3 and 4D let say) with all the necessary arithmetic niceties (operator +, - and so on). POD low dimension matrices would be great as well. boost::ublas vectors are not POD, there's a pointer indirection somewhere (vector are resizeable). Can I find that anywhere in boost? Using boost::array ...

OR-multiplication on big integers.

Multiplication of two n-bit numbers A and B can be understood as a sum of shifts: (A << i1) + (A << i2) + ... where i1, i2, ... are numbers of bits that are set to 1 in B. Now lets replace PLUS with OR to get new operation I actually need: (A << i1) | (A << i2) | ... This operation is quite similar to regular multiplication for ...

getting the bottom 16 bits of a Java int as a signed 16-bit value

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer. public class SignExtend16 { public static int get16Bits(int x) { return (x & 0xffff) - ((x & 0x8000) << 1); } public static int get16Bits0(int x) { return (int)(short)(x); } public static...

Can dbms_utility.get_time rollover?

I'm having problems with a mammoth legacy PL/SQL procedure which has the following logic: l_elapsed := dbms_utility.get_time - l_timestamp; where l_elapsed and l_timestamp are of type PLS_INTEGER and l_timestamp holds the result of a previous call to get_time This line suddenly started failing during a batch run with a ORA-01426: num...

Why won't postgresql store my entire (PHP) float value?

I try to store the PHP floating point value 63.59072952118762 into a double precision column in postgres. Postgres stores the value as 63.59073. Does anyone know why? 8 byte should be more than enough for that value. I've tried with the data type numeric, which works when specifying the precision, but that shouldn't really be necessary. ...

Why is two's complement used to represent negative numbers?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1 with first bit as negative flag. Disclaimer: I don't rely on binary arithmetic for my job! ...

Representing 128-bit numbers in C++

What's the best way to represent a 128-bit number in C++? It should behave as closely to the built-in numeric types as possible (i.e. support all the arithmetic operators, etc). I was thinking of building a class that had 2 64 bit or 4 32 bit numbers. Or possibly just creating a 128 bit block of memory and doing everything myself. Is t...

What is easiest way to calculate an infix expression using C language?

Suppose the user inputs an infix expression as a string? What could be the easiest ( By easiest I mean the shortest) way to evaluate the result of that expression using C language? Probable ways are converting it to a postfix then by using stacks.But its rather a long process. Is there any way of using functions such as atoi() or eval()...

Has arbitrary-precision arithmetic affected numerical analysis software?

Has arbitrary-precision arithmetic affected numerical analysis software? I feel that most numerical analysis software keeps on using the same floats and doubles. If I'm right, I'd love to know the reason, as in my opinion there are some calculations that can benefit from the use of arbitrary-precision arithmetic, particularly when it i...

C#: Order of function evaluation (vs C)

Take the following C code (K&R pg. 77) : push(pop() - pop()); /* WRONG */ The book says that since - and / are not commutative operators, the order in which the 2 pop functions are evaluated is necessary (obviously, to get the correct result)...and thus you have to put the result of the first function in a variable first and then pro...